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