VectorialMean.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.  *      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. import java.util.Arrays;

  19. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;

  20. /**
  21.  * Returns the arithmetic mean of the available vectors.
  22.  * @since 1.2
  23.  */
  24. public class VectorialMean {
  25.     /** Means for each component. */
  26.     private final Mean[] means;

  27.     /** Constructs a VectorialMean.
  28.      * @param dimension vectors dimension
  29.      */
  30.     public VectorialMean(int dimension) {
  31.         means = new Mean[dimension];
  32.         for (int i = 0; i < dimension; ++i) {
  33.             means[i] = new Mean();
  34.         }
  35.     }

  36.     /**
  37.      * Add a new vector to the sample.
  38.      * @param v vector to add
  39.      * @throws DimensionMismatchException if the vector does not have the right dimension
  40.      */
  41.     public void increment(double[] v) throws DimensionMismatchException {
  42.         if (v.length != means.length) {
  43.             throw new DimensionMismatchException(v.length, means.length);
  44.         }
  45.         for (int i = 0; i < v.length; ++i) {
  46.             means[i].increment(v[i]);
  47.         }
  48.     }

  49.     /**
  50.      * Get the mean vector.
  51.      * @return mean vector
  52.      */
  53.     public double[] getResult() {
  54.         double[] result = new double[means.length];
  55.         for (int i = 0; i < result.length; ++i) {
  56.             result[i] = means[i].getResult();
  57.         }
  58.         return result;
  59.     }

  60.     /**
  61.      * Get the number of vectors in the sample.
  62.      * @return number of vectors in the sample
  63.      */
  64.     public long getN() {
  65.         return (means.length == 0) ? 0 : means[0].getN();
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public int hashCode() {
  70.         final int prime = 31;
  71.         int result = 1;
  72.         result = prime * result + Arrays.hashCode(means);
  73.         return result;
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public boolean equals(Object obj) {
  78.         if (this == obj) {
  79.             return true;
  80.         }
  81.         if (!(obj instanceof VectorialMean)) {
  82.             return false;
  83.         }
  84.         VectorialMean other = (VectorialMean) obj;
  85.         return Arrays.equals(means, other.means);
  86.     }
  87. }