    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.distribution;
    018
    019    import org.apache.commons.math3.exception.NotStrictlyPositiveException;
    020    import org.apache.commons.math3.exception.util.LocalizedFormats;
    021    import org.apache.commons.math3.random.RandomGenerator;
    022
    023    /**
    024     * Base class for multivariate probability distributions.
    025     *
    026     * @version $Id: AbstractMultivariateRealDistribution.java 1416643 2012-12-03 19:37:14Z tn $
    027     * @since 3.1
    028     */
    029    public abstract class AbstractMultivariateRealDistribution
    030        implements MultivariateRealDistribution {
    031        /** RNG instance used to generate samples from the distribution. */
    032        protected final RandomGenerator random;
    033        /** The number of dimensions or columns in the multivariate distribution. */
    034        private final int dimension;
    035
    036        /**
    037         * @param rng Random number generator.
    038         * @param n Number of dimensions.
    039         */
    040        protected AbstractMultivariateRealDistribution(RandomGenerator rng,
    041                                                       int n) {
    042            random = rng;
    043            dimension = n;
    044        }
    045
    046        /** {@inheritDoc} */
    047        public void reseedRandomGenerator(long seed) {
    048            random.setSeed(seed);
    049        }
    050
    051        /** {@inheritDoc} */
    052        public int getDimension() {
    053            return dimension;
    054        }
    055
    056        /** {@inheritDoc} */
    057        public abstract double[] sample();
    058
    059        /** {@inheritDoc} */
    060        public double[][] sample(final int sampleSize) {
    061            if (sampleSize <= 0) {
    062                throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
    063                                                       sampleSize);
    064            }
    065            final double[][] out = new double[sampleSize][dimension];
    066            for (int i = 0; i < sampleSize; i++) {
    067                out[i] = sample();
    068            }
    069            return out;
    070        }
    071    }