View Javadoc
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.math4.legacy.stat.descriptive.moment;
18  
19  import java.util.Arrays;
20  
21  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
22  
23  /**
24   * Returns the arithmetic mean of the available vectors.
25   * @since 1.2
26   */
27  public class VectorialMean {
28      /** Means for each component. */
29      private final Mean[] means;
30  
31      /** Constructs a VectorialMean.
32       * @param dimension vectors dimension
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       * Add a new vector to the sample.
43       * @param v vector to add
44       * @throws DimensionMismatchException if the vector does not have the right dimension
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       * Get the mean vector.
57       * @return mean vector
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       * Get the number of vectors in the sample.
69       * @return number of vectors in the sample
70       */
71      public long getN() {
72          return (means.length == 0) ? 0 : means[0].getN();
73      }
74  
75      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }