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 */
017package org.apache.commons.math3.stat.descriptive.moment;
018
019import java.io.Serializable;
020import java.util.Arrays;
021
022import org.apache.commons.math3.exception.DimensionMismatchException;
023
024/**
025 * Returns the arithmetic mean of the available vectors.
026 * @since 1.2
027 */
028public class VectorialMean implements Serializable {
029
030    /** Serializable version identifier */
031    private static final long serialVersionUID = 8223009086481006892L;
032
033    /** Means for each component. */
034    private final Mean[] means;
035
036    /** Constructs a VectorialMean.
037     * @param dimension vectors dimension
038     */
039    public VectorialMean(int dimension) {
040        means = new Mean[dimension];
041        for (int i = 0; i < dimension; ++i) {
042            means[i] = new Mean();
043        }
044    }
045
046    /**
047     * Add a new vector to the sample.
048     * @param v vector to add
049     * @throws DimensionMismatchException if the vector does not have the right dimension
050     */
051    public void increment(double[] v) throws DimensionMismatchException {
052        if (v.length != means.length) {
053            throw new DimensionMismatchException(v.length, means.length);
054        }
055        for (int i = 0; i < v.length; ++i) {
056            means[i].increment(v[i]);
057        }
058    }
059
060    /**
061     * Get the mean vector.
062     * @return mean vector
063     */
064    public double[] getResult() {
065        double[] result = new double[means.length];
066        for (int i = 0; i < result.length; ++i) {
067            result[i] = means[i].getResult();
068        }
069        return result;
070    }
071
072    /**
073     * Get the number of vectors in the sample.
074     * @return number of vectors in the sample
075     */
076    public long getN() {
077        return (means.length == 0) ? 0 : means[0].getN();
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public int hashCode() {
083        final int prime = 31;
084        int result = 1;
085        result = prime * result + Arrays.hashCode(means);
086        return result;
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public boolean equals(Object obj) {
092        if (this == obj) {
093            return true;
094        }
095        if (!(obj instanceof VectorialMean)) {
096            return false;
097        }
098        VectorialMean other = (VectorialMean) obj;
099        if (!Arrays.equals(means, other.means)) {
100            return false;
101        }
102        return true;
103    }
104
105}