Exemplo n.º 1
0
        /**
         * <p>
         * Creates a new vector which is drawn from a multivariate normal distribution with zero mean
         * and the provided covariance.
         * </p>
         *
         * @see CovarianceRandomDraw_DDRM
         *
         * @param covariance Covariance of the multivariate normal distribution
         * @return Vector randomly drawn from the distribution
         */
        public static SimpleMatrix <T> randomNormal(SimpleMatrix <T> covariance, IMersenneTwister random)
        {
            SimpleMatrix <T> found = new SimpleMatrix <T>(covariance.numRows(), 1);

            var m    = covariance.getMatrix();
            var bits = covariance.bits();

            if (bits == 64)
            {
                // Check type here to make sure
                if (typeof(T) != typeof(DMatrixRMaj))
                {
                    throw new InvalidOperationException("This operation is only valid for type DMatrixRMaj.");
                }

                var draw = new CovarianceRandomDraw_DDRM(random, m as DMatrixRMaj);
                draw.next(found.getMatrix() as DMatrixRMaj);
            }
            else if (bits == 32)
            {
                // Check type here to make sure
                if (typeof(T) != typeof(FMatrixRMaj))
                {
                    throw new InvalidOperationException("This operation is only valid for type FMatrixRMaj.");
                }

                var draw = new CovarianceRandomDraw_FDRM(random, m as FMatrixRMaj);
                draw.next(found.getMatrix() as FMatrixRMaj);
            }
            else // Unknown data type
            {
                throw new InvalidOperationException("Unknown data type: must be double (64 bits) or float (32 bits).");
            }
            return(found);
        }