Esempio n. 1
0
        /// <summary>
        /// Gets the Cholesky decomposition of the matrix (L*L' = A), replacing its contents.
        /// </summary>
        /// <param name="isPosDef">True if <c>this</c> is positive definite, otherwise false.</param>
        /// <returns>The Cholesky decomposition L.  If <c>this</c> is positive semidefinite,
        /// then L will satisfy L*L' = A.
        /// Otherwise, L will only approximately satisfy L*L' = A.</returns>
        /// <remarks>
        /// <c>this</c> must be symmetric, but need not be positive definite.
        /// </remarks>
        public LowerTriangularMatrix CholeskyInPlace(out bool isPosDef)
        {
            LowerTriangularMatrix L = new LowerTriangularMatrix(rows, cols, data);

#if LAPACK
            isPosDef = Lapack.CholeskyInPlace(this);
#else
            isPosDef = L.SetToCholesky(this);
#endif
            return(L);
        }
Esempio n. 2
0
        public static void Normal(Vector mean, PositiveDefiniteMatrix variance, Vector result, IPolyrand random = null)
        {
            LowerTriangularMatrix L = new LowerTriangularMatrix(variance.Rows, variance.Rows);
            bool isPosDef           = L.SetToCholesky(variance);

            if (!isPosDef)
            {
                throw new ArgumentException("PROB.Random.Normal(Vector,Matrix,Vector): variance must be positive definite", nameof(variance));
            }

            NormalChol(mean, L, result, random);
        }
Esempio n. 3
0
        public static void NormalP(Vector mean, PositiveDefiniteMatrix precision, Vector result, IPolyrand random = null)
        {
            LowerTriangularMatrix L = new LowerTriangularMatrix(precision.Rows, precision.Rows);
            bool isPosDef           = L.SetToCholesky(precision);

            if (!isPosDef)
            {
                throw new ArgumentException("PROB.Random.NormalP(Vector,Matrix,Vector): precision must be positive definite", nameof(precision));
            }
            UpperTriangularMatrix U = UpperTriangularMatrix.TransposeInPlace(L);

            NormalPChol(mean, U, result, random);
        }
Esempio n. 4
0
        /// <summary>
        /// Sets this positive-definite matrix to inverse of a given positive-definite matrix
        /// where a lower triangular workspace is passed.
        /// </summary>
        /// <param name="A">A symmetric positive-definite matrix, same size as <c>this</c>.  Can be the same object as <c>this</c>.</param>
        /// <param name="L">A workspace, same size as <paramref name="A"/>.</param>
        /// <returns><c>this</c></returns>
        /// <exception cref="PositiveDefiniteMatrixException">If <paramref name="A"/> is not positive definite.</exception>
        public PositiveDefiniteMatrix SetToInverse(PositiveDefiniteMatrix A, LowerTriangularMatrix L)
        {
            CheckCompatible(L, nameof(L));
            // Algorithm:
            // A = L*L'
            // inv(A) = inv(L')*inv(L)
            bool isPD = L.SetToCholesky(A);

            if (!isPD)
            {
                throw new PositiveDefiniteMatrixException();
            }
            L.SetToInverse(L);
            SetToOuterTranspose(L);
            return(this);
        }
Esempio n. 5
0
 public double LogDeterminant(LowerTriangularMatrix L, bool ignoreInfinity = false)
 {
     L.SetToCholesky(this);
     return(2 * L.LogDeterminant(ignoreInfinity));
 }