/// <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);
        }
Exemplo n.º 2
0
        public static void Normal(Vector mean, PositiveDefiniteMatrix variance, Vector result)
        {
            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);
        }
Exemplo n.º 3
0
        public static void NormalP(Vector mean, PositiveDefiniteMatrix precision, Vector result)
        {
            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);
        }
        /// <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);
        }
 public double LogDeterminant(LowerTriangularMatrix L, bool ignoreInfinity = false)
 {
     L.SetToCholesky(this);
     return(2 * L.LogDeterminant(ignoreInfinity));
 }