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.moment;
18
19 import java.io.Serializable;
20 import java.util.Arrays;
21
22 import org.apache.commons.math.DimensionMismatchException;
23
24 /**
25 * Returns the arithmetic mean of the available vectors.
26 * @since 1.2
27 * @version $Revision: 780645 $ $Date: 2009-06-01 09:24:19 -0400 (Mon, 01 Jun 2009) $
28 */
29 public class VectorialMean implements Serializable {
30
31 /** Serializable version identifier */
32 private static final long serialVersionUID = 8223009086481006892L;
33
34 /** Means for each component. */
35 private Mean[] means;
36
37 /** Constructs a VectorialMean.
38 * @param dimension vectors dimension
39 */
40 public VectorialMean(int dimension) {
41 means = new Mean[dimension];
42 for (int i = 0; i < dimension; ++i) {
43 means[i] = new Mean();
44 }
45 }
46
47 /**
48 * Add a new vector to the sample.
49 * @param v vector to add
50 * @exception DimensionMismatchException if the vector does not have the right dimension
51 */
52 public void increment(double[] v) throws DimensionMismatchException {
53 if (v.length != means.length) {
54 throw new DimensionMismatchException(v.length, means.length);
55 }
56 for (int i = 0; i < v.length; ++i) {
57 means[i].increment(v[i]);
58 }
59 }
60
61 /**
62 * Get the mean vector.
63 * @return mean vector
64 */
65 public double[] getResult() {
66 double[] result = new double[means.length];
67 for (int i = 0; i < result.length; ++i) {
68 result[i] = means[i].getResult();
69 }
70 return result;
71 }
72
73 /**
74 * Get the number of vectors in the sample.
75 * @return number of vectors in the sample
76 */
77 public long getN() {
78 return (means.length == 0) ? 0 : means[0].getN();
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public int hashCode() {
84 final int prime = 31;
85 int result = 1;
86 result = prime * result + Arrays.hashCode(means);
87 return result;
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj)
94 return true;
95 if (obj == null)
96 return false;
97 if (!(obj instanceof VectorialMean))
98 return false;
99 VectorialMean other = (VectorialMean) obj;
100 if (!Arrays.equals(means, other.means))
101 return false;
102 return true;
103 }
104
105 }