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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.descriptive.vector;
23  
24  import java.io.Serializable;
25  import java.util.Arrays;
26  
27  import org.hipparchus.exception.LocalizedCoreFormats;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  import org.hipparchus.stat.descriptive.StorelessMultivariateStatistic;
30  import org.hipparchus.stat.descriptive.StorelessUnivariateStatistic;
31  import org.hipparchus.util.MathUtils;
32  
33  /**
34   * Uses an independent {@link StorelessUnivariateStatistic} instance
35   * for each component of a vector.
36   */
37  public class VectorialStorelessStatistic
38      implements StorelessMultivariateStatistic, Serializable {
39  
40      /** Serializable UID */
41      private static final long serialVersionUID = 20160413L;
42  
43      /** Statistic for each component */
44      private final StorelessUnivariateStatistic[] stats;
45  
46      /**
47       * Create a new VectorialStorelessStatistic with the given dimension
48       * and statistic implementation. A copy of the provided statistic
49       * will be created for each component of the vector.
50       *
51       * @param dimension the vector dimension
52       * @param univariateStatistic the prototype statistic
53       * @throws MathIllegalArgumentException if dimension < 1
54       */
55      public VectorialStorelessStatistic(int dimension,
56                                         StorelessUnivariateStatistic univariateStatistic) {
57          if (dimension < 1) {
58              throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
59                                                     dimension, 1);
60          }
61          stats = new StorelessUnivariateStatistic[dimension];
62          for (int i = 0; i < dimension; i++) {
63              stats[i] = univariateStatistic.copy();
64          }
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public void increment(double[] d) {
70          MathUtils.checkDimension(d.length, stats.length);
71          for (int i = 0; i < stats.length; i++) {
72              stats[i].increment(d[i]);
73          }
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public double[] getResult() {
79          double[] result = new double[stats.length];
80          for (int i = 0; i < result.length; ++i) {
81              result[i] = stats[i].getResult();
82          }
83          return result;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public long getN() {
89          return stats[0].getN();
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void clear() {
95          for (StorelessUnivariateStatistic stat : stats) {
96              stat.clear();
97          }
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public int getDimension() {
103         return stats.length;
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public int hashCode() {
109         final int prime = 31;
110         int result = 1;
111         result = prime * result + Arrays.hashCode(stats);
112         return result;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public boolean equals(Object obj) {
118         if (this == obj) {
119             return true;
120         }
121         if (!(obj instanceof VectorialStorelessStatistic)) {
122             return false;
123         }
124         VectorialStorelessStatistic other = (VectorialStorelessStatistic) obj;
125         if (!Arrays.equals(stats, other.stats)) {
126             return false;
127         }
128         return true;
129     }
130 
131 }