    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.math3.stat.descriptive.moment;
    018
    019    import java.io.Serializable;
    020    import java.util.Arrays;
    021
    022    import org.apache.commons.math3.exception.DimensionMismatchException;
    023
    024    /**
    025     * Returns the arithmetic mean of the available vectors.
    026     * @since 1.2
    027     * @version $Id: VectorialMean.java 1416643 2012-12-03 19:37:14Z tn $
    028     */
    029    public class VectorialMean implements Serializable {
    030
    031        /** Serializable version identifier */
    032        private static final long serialVersionUID = 8223009086481006892L;
    033
    034        /** Means for each component. */
    035        private final Mean[] means;
    036
    037        /** Constructs a VectorialMean.
    038         * @param dimension vectors dimension
    039         */
    040        public VectorialMean(int dimension) {
    041            means = new Mean[dimension];
    042            for (int i = 0; i < dimension; ++i) {
    043                means[i] = new Mean();
    044            }
    045        }
    046
    047        /**
    048         * Add a new vector to the sample.
    049         * @param v vector to add
    050         * @throws DimensionMismatchException if the vector does not have the right dimension
    051         */
    052        public void increment(double[] v) throws DimensionMismatchException {
    053            if (v.length != means.length) {
    054                throw new DimensionMismatchException(v.length, means.length);
    055            }
    056            for (int i = 0; i < v.length; ++i) {
    057                means[i].increment(v[i]);
    058            }
    059        }
    060
    061        /**
    062         * Get the mean vector.
    063         * @return mean vector
    064         */
    065        public double[] getResult() {
    066            double[] result = new double[means.length];
    067            for (int i = 0; i < result.length; ++i) {
    068                result[i] = means[i].getResult();
    069            }
    070            return result;
    071        }
    072
    073        /**
    074         * Get the number of vectors in the sample.
    075         * @return number of vectors in the sample
    076         */
    077        public long getN() {
    078            return (means.length == 0) ? 0 : means[0].getN();
    079        }
    080
    081        /** {@inheritDoc} */
    082        @Override
    083        public int hashCode() {
    084            final int prime = 31;
    085            int result = 1;
    086            result = prime * result + Arrays.hashCode(means);
    087            return result;
    088        }
    089
    090        /** {@inheritDoc} */
    091        @Override
    092        public boolean equals(Object obj) {
    093            if (this == obj) {
    094                return true;
    095            }
    096            if (!(obj instanceof VectorialMean)) {
    097                return false;
    098            }
    099            VectorialMean other = (VectorialMean) obj;
    100            if (!Arrays.equals(means, other.means)) {
    101                return false;
    102            }
    103            return true;
    104        }
    105
    106    }