/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2020 Barry DeZonia
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package nom.bdezonia.zorbage.algorithm;

import nom.bdezonia.zorbage.algebra.*;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;

/**
 * @author Barry DeZonia
 */
public class Variance {

    /**
     *
     * @param alg
     * @param source
     * @param result
     * @param <T>
     * @param <U>
     */
    public static <T extends Algebra<T,U> & Addition<U> & Multiplication<U> & Unity<U>, U>
        void compute(T alg, IndexedDataSource<U> source, U result)
    {
        if (source.size() <= 1) {
            alg.zero().call(result);
            return;
        }
        U mean = alg.construct();
        U sum = alg.construct();
        U tmp = alg.construct();
        U n = alg.construct();
        U one = alg.construct();
        alg.unity().call(one);
        Mean.compute(alg, source, mean);
        for (long i = 0; i < source.size(); i++) {
            source.get(i, tmp);
            alg.subtract().call(tmp, mean, tmp);
            alg.multiply().call(tmp, tmp, tmp);
            alg.add().call(sum, tmp, sum);
            alg.add().call(n, one, n);
        }
        alg.subtract().call(n, one, n);
        Divide.compute(alg, sum, n, result);
    }
}

@@ -1832,20 +1832,17 @@
  U%3E%0A    
-    
+%09
 void com
@@ -1902,20 +1902,17 @@
   %7B%0A    
-    
+%09
 if (sour
@@ -1929,32 +1929,26 @@
 %3C= 1) %7B%0A    
-        
+%09%09
 alg.zero().c
@@ -1964,24 +1964,18 @@
 t);%0A    
-        
+%09%09
 return;%0A
@@ -1978,30 +1978,24 @@
 rn;%0A    
-    
+%09
 %7D%0A    
-    
+%09
 U mean =
@@ -2016,20 +2016,17 @@
 ();%0A    
-    
+%09
 U sum = 
@@ -2046,20 +2046,17 @@
 ();%0A    
-    
+%09
 U tmp = 
@@ -2076,20 +2076,17 @@
 ();%0A    
-    
+%09
 U n = al
@@ -2104,20 +2104,17 @@
 ();%0A    
-    
+%09
 U one = 
@@ -2130,28 +2130,25 @@
 ruct();%0A    
-    
+%09
 alg.unity().
@@ -2162,20 +2162,17 @@
 e);%0A    
-    
+%09
 Mean.com
@@ -2200,20 +2200,17 @@
 n);%0A    
-    
+%09
 for (lon
@@ -2248,24 +2248,18 @@
 ) %7B%0A    
-        
+%09%09
 source.g
@@ -2270,32 +2270,26 @@
 , tmp);%0A    
-        
+%09%09
 alg.subtract
@@ -2313,32 +2313,26 @@
 , tmp);%0A    
-        
+%09%09
 alg.multiply
@@ -2355,32 +2355,26 @@
 , tmp);%0A    
-        
+%09%09
 alg.add().ca
@@ -2396,24 +2396,18 @@
 m);%0A    
-        
+%09%09
 alg.add(
@@ -2433,22 +2433,16 @@
     
-    
+%09
 %7D%0A    
-    
+%09
 alg.
@@ -2473,20 +2473,17 @@
 n);%0A    
-    
+%09
 Divide.c

Clean up some spacing in one file