VectorialStorelessStatistic.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.vector;

  22. import java.io.Serializable;
  23. import java.util.Arrays;

  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.stat.descriptive.StorelessMultivariateStatistic;
  27. import org.hipparchus.stat.descriptive.StorelessUnivariateStatistic;
  28. import org.hipparchus.util.MathUtils;

  29. /**
  30.  * Uses an independent {@link StorelessUnivariateStatistic} instance
  31.  * for each component of a vector.
  32.  */
  33. public class VectorialStorelessStatistic
  34.     implements StorelessMultivariateStatistic, Serializable {

  35.     /** Serializable UID */
  36.     private static final long serialVersionUID = 20160413L;

  37.     /** Statistic for each component */
  38.     private final StorelessUnivariateStatistic[] stats;

  39.     /**
  40.      * Create a new VectorialStorelessStatistic with the given dimension
  41.      * and statistic implementation. A copy of the provided statistic
  42.      * will be created for each component of the vector.
  43.      *
  44.      * @param dimension the vector dimension
  45.      * @param univariateStatistic the prototype statistic
  46.      * @throws MathIllegalArgumentException if dimension < 1
  47.      */
  48.     public VectorialStorelessStatistic(int dimension,
  49.                                        StorelessUnivariateStatistic univariateStatistic) {
  50.         if (dimension < 1) {
  51.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
  52.                                                    dimension, 1);
  53.         }
  54.         stats = new StorelessUnivariateStatistic[dimension];
  55.         for (int i = 0; i < dimension; i++) {
  56.             stats[i] = univariateStatistic.copy();
  57.         }
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public void increment(double[] d) {
  62.         MathUtils.checkDimension(d.length, stats.length);
  63.         for (int i = 0; i < stats.length; i++) {
  64.             stats[i].increment(d[i]);
  65.         }
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public double[] getResult() {
  70.         double[] result = new double[stats.length];
  71.         for (int i = 0; i < result.length; ++i) {
  72.             result[i] = stats[i].getResult();
  73.         }
  74.         return result;
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public long getN() {
  79.         return stats[0].getN();
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public void clear() {
  84.         for (StorelessUnivariateStatistic stat : stats) {
  85.             stat.clear();
  86.         }
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public int getDimension() {
  91.         return stats.length;
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public int hashCode() {
  96.         final int prime = 31;
  97.         int result = 1;
  98.         result = prime * result + Arrays.hashCode(stats);
  99.         return result;
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public boolean equals(Object obj) {
  104.         if (this == obj) {
  105.             return true;
  106.         }
  107.         if (!(obj instanceof VectorialStorelessStatistic)) {
  108.             return false;
  109.         }
  110.         VectorialStorelessStatistic other = (VectorialStorelessStatistic) obj;
  111.         if (!Arrays.equals(stats, other.stats)) {
  112.             return false;
  113.         }
  114.         return true;
  115.     }

  116. }