KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > descriptive > summary > SumOfSquares


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.stat.descriptive.summary;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
21
22 /**
23  * Returns the sum of the squares of the available values.
24  * <p>
25  * If there are no values in the dataset, or any of the values are
26  * <code>NaN</code>, then <code>NaN</code> is returned.
27  * <p>
28  * <strong>Note that this implementation is not synchronized.</strong> If
29  * multiple threads access an instance of this class concurrently, and at least
30  * one of the threads invokes the <code>increment()</code> or
31  * <code>clear()</code> method, it must be synchronized externally.
32  *
33  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
34  */

35 public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable JavaDoc {
36
37     /** Serializable version identifier */
38     static final long serialVersionUID = 1460986908574398008L;
39       
40     /** */
41     private long n;
42     
43     /**
44      * The currently running sumSq
45      */

46     private double value;
47
48     /**
49      * Create a SumOfSquares instance
50      */

51     public SumOfSquares() {
52         n = 0;
53         value = Double.NaN;
54     }
55     
56     /**
57      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
58      */

59     public void increment(final double d) {
60         if (n == 0) {
61             value = d * d;
62         } else {
63             value += d * d;
64         }
65         n++;
66     }
67
68     /**
69      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
70      */

71     public double getResult() {
72         return value;
73     }
74
75     /**
76      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
77      */

78     public long getN() {
79         return n;
80     }
81     
82     /**
83      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
84      */

85     public void clear() {
86         value = Double.NaN;
87         n = 0;
88     }
89
90     /**
91      * Returns the sum of the squares of the entries in the specified portion of
92      * the input array, or <code>Double.NaN</code> if the designated subarray
93      * is empty.
94      * <p>
95      * Throws <code>IllegalArgumentException</code> if the array is null.
96      *
97      * @param values the input array
98      * @param begin index of the first array element to include
99      * @param length the number of elements to include
100      * @return the sum of the squares of the values or Double.NaN if length = 0
101      * @throws IllegalArgumentException if the array is null or the array index
102      * parameters are not valid
103      */

104     public double evaluate(final double[] values,final int begin, final int length) {
105         double sumSq = Double.NaN;
106         if (test(values, begin, length)) {
107             sumSq = 0.0;
108             for (int i = begin; i < begin + length; i++) {
109                 sumSq += values[i] * values[i];
110             }
111         }
112         return sumSq;
113     }
114
115 }
Popular Tags