1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.stat.descriptive.moment;
18
19 import java.util.Arrays;
20
21 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
22
23
24
25
26
27 public class VectorialMean {
28
29 private final Mean[] means;
30
31
32
33
34 public VectorialMean(int dimension) {
35 means = new Mean[dimension];
36 for (int i = 0; i < dimension; ++i) {
37 means[i] = new Mean();
38 }
39 }
40
41
42
43
44
45
46 public void increment(double[] v) throws DimensionMismatchException {
47 if (v.length != means.length) {
48 throw new DimensionMismatchException(v.length, means.length);
49 }
50 for (int i = 0; i < v.length; ++i) {
51 means[i].increment(v[i]);
52 }
53 }
54
55
56
57
58
59 public double[] getResult() {
60 double[] result = new double[means.length];
61 for (int i = 0; i < result.length; ++i) {
62 result[i] = means[i].getResult();
63 }
64 return result;
65 }
66
67
68
69
70
71 public long getN() {
72 return (means.length == 0) ? 0 : means[0].getN();
73 }
74
75
76 @Override
77 public int hashCode() {
78 final int prime = 31;
79 int result = 1;
80 result = prime * result + Arrays.hashCode(means);
81 return result;
82 }
83
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90 if (!(obj instanceof VectorialMean)) {
91 return false;
92 }
93 VectorialMean other = (VectorialMean) obj;
94 return Arrays.equals(means, other.means);
95 }
96 }