~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/stat/descriptive/summary/SumOfSquares.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.commons.math.stat.descriptive.summary;
 
18
 
 
19
import java.io.Serializable;
 
20
 
 
21
import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
 
22
 
 
23
/**
 
24
 * Returns the sum of the squares of the available values.
 
25
 * <p>
 
26
 * If there are no values in the dataset, or any of the values are 
 
27
 * <code>NaN</code>, then <code>NaN</code> is returned.</p>
 
28
 * <p>
 
29
 * <strong>Note that this implementation is not synchronized.</strong> If 
 
30
 * multiple threads access an instance of this class concurrently, and at least
 
31
 * one of the threads invokes the <code>increment()</code> or 
 
32
 * <code>clear()</code> method, it must be synchronized externally.</p>
 
33
 * 
 
34
 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
 
35
 */
 
36
public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable {
 
37
 
 
38
    /** Serializable version identifier */
 
39
    private static final long serialVersionUID = 1460986908574398008L;  
 
40
      
 
41
    /** */
 
42
    private long n;
 
43
    
 
44
    /**
 
45
     * The currently running sumSq
 
46
     */
 
47
    private double value;
 
48
 
 
49
    /**
 
50
     * Create a SumOfSquares instance
 
51
     */
 
52
    public SumOfSquares() {
 
53
        n = 0;
 
54
        value = Double.NaN;
 
55
    }
 
56
    
 
57
    /**
 
58
     * Copy constructor, creates a new {@code SumOfSquares} identical
 
59
     * to the {@code original}
 
60
     * 
 
61
     * @param original the {@code SumOfSquares} instance to copy
 
62
     */
 
63
    public SumOfSquares(SumOfSquares original) {
 
64
        copy(original, this);
 
65
    }
 
66
    
 
67
    /**
 
68
     * {@inheritDoc}
 
69
     */
 
70
    @Override
 
71
    public void increment(final double d) {
 
72
        if (n == 0) {
 
73
            value = d * d;
 
74
        } else {
 
75
            value += d * d;
 
76
        }
 
77
        n++;
 
78
    }
 
79
 
 
80
    /**
 
81
     * {@inheritDoc}
 
82
     */
 
83
    @Override
 
84
    public double getResult() {
 
85
        return value;
 
86
    }
 
87
 
 
88
    /**
 
89
     * {@inheritDoc}
 
90
     */
 
91
    public long getN() {
 
92
        return n;
 
93
    }
 
94
    
 
95
    /**
 
96
     * {@inheritDoc}
 
97
     */
 
98
    @Override
 
99
    public void clear() {
 
100
        value = Double.NaN;
 
101
        n = 0;
 
102
    }
 
103
 
 
104
    /**
 
105
     * Returns the sum of the squares of the entries in the specified portion of
 
106
     * the input array, or <code>Double.NaN</code> if the designated subarray
 
107
     * is empty.
 
108
     * <p>
 
109
     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 
110
     * 
 
111
     * @param values the input array
 
112
     * @param begin index of the first array element to include
 
113
     * @param length the number of elements to include
 
114
     * @return the sum of the squares of the values or Double.NaN if length = 0
 
115
     * @throws IllegalArgumentException if the array is null or the array index
 
116
     *  parameters are not valid
 
117
     */
 
118
    @Override
 
119
    public double evaluate(final double[] values,final int begin, final int length) {
 
120
        double sumSq = Double.NaN;
 
121
        if (test(values, begin, length)) {
 
122
            sumSq = 0.0;
 
123
            for (int i = begin; i < begin + length; i++) {
 
124
                sumSq += values[i] * values[i];
 
125
            }
 
126
        }
 
127
        return sumSq;
 
128
    }
 
129
    
 
130
    /**
 
131
     * {@inheritDoc}
 
132
     */
 
133
    @Override
 
134
    public SumOfSquares copy() {
 
135
        SumOfSquares result = new SumOfSquares();
 
136
        copy(this, result);
 
137
        return result;
 
138
    }
 
139
    
 
140
    /**
 
141
     * Copies source to dest.
 
142
     * <p>Neither source nor dest can be null.</p>
 
143
     * 
 
144
     * @param source SumOfSquares to copy
 
145
     * @param dest SumOfSquares to copy to
 
146
     * @throws NullPointerException if either source or dest is null
 
147
     */
 
148
    public static void copy(SumOfSquares source, SumOfSquares dest) {
 
149
        dest.n = source.n;
 
150
        dest.value = source.value;
 
151
    }
 
152
 
 
153
}