Solve() public method

Solves a set of equation systems of type A * X = B.
Matrix dimensions do not match. Matrix is not symmetric and positive definite.
public Solve ( double value ) : ].double[
value double Right hand side matrix with as many rows as A and any number of columns.
return ].double[
        public void InverseTestNaN()
        {
            int n = 5;

            var I = Matrix.Identity(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    double[,] value = Matrix.Magic(n);

                    // Make symmetric
                    value = Matrix.Multiply(value, value.Transpose());

                    value[i, j] = double.NaN;
                    value[j, i] = double.NaN;

                    Assert.IsTrue(value.IsSymmetric());

                    bool thrown = false;

                    var target = new CholeskyDecomposition(value);

                    try
                    {
                        target.Solve(I);
                    }
                    catch (Exception)
                    {
                        thrown = true;
                    }

                    Assert.IsTrue(thrown);
                }
            }
        }
        /// <summary>
        ///   Gets the probability density function (pdf) for
        ///   this distribution evaluated at point <c>x</c>.
        /// </summary>
        /// 
        /// <param name="x">A single point in the distribution range.
        ///   For a matrix distribution, such as the Wishart's, this
        ///   should be a positive-definite matrix or a matrix written
        ///   in flat vector form.
        /// </param>
        ///   
        /// <returns>
        ///   The probability of <c>x</c> occurring
        ///   in the current distribution.
        /// </returns>
        /// 
        /// <remarks>
        ///   The Probability Density Function (PDF) describes the
        ///   probability that a given value <c>x</c> will occur.
        /// </remarks>
        /// 
        public double LogProbabilityDensityFunction(double[,] x)
        {
            var chol = new CholeskyDecomposition(x);

            double det = chol.Determinant;
            double[,] Vx = chol.Solve(inverseScaleMatrix);

            double z = -0.5 * Vx.Trace();

            return Math.Log(constant) + power * Math.Log(det) + z;
        }
        /// <summary>
        ///   Gets the probability density function (pdf) for
        ///   this distribution evaluated at point <c>x</c>.
        /// </summary>
        /// 
        /// <param name="x">A single point in the distribution range.
        ///   For a matrix distribution, such as the Wishart's, this
        ///   should be a positive-definite matrix or a matrix written
        ///   in flat vector form.
        /// </param>
        ///   
        /// <returns>
        ///   The probability of <c>x</c> occurring
        ///   in the current distribution.
        /// </returns>
        /// 
        /// <remarks>
        ///   The Probability Density Function (PDF) describes the
        ///   probability that a given value <c>x</c> will occur.
        /// </remarks>
        /// 
        public double ProbabilityDensityFunction(double[,] x)
        {
            var chol = new CholeskyDecomposition(x);

            double det = chol.Determinant;
            double[,] Vx = chol.Solve(inverseScaleMatrix);

            double z = -0.5 * Vx.Trace();
            double a = Math.Pow(det, power);
            double b = Math.Exp(z);

            return constant * a * b;
        }
        public void SolveTest4()
        {
            double[,] value = // not positive-definite
            {
               {  6, -1,  2,  6 },
               { -1,  3, -3, -2 },
               {  2, -3,  2,  0 },
               {  6, -2,  0,  0 },
            };

            CholeskyDecomposition chol = new CholeskyDecomposition(value, true);
            double[,] L = chol.LeftTriangularFactor;

            double[] B = new double[] { 1, 2, 3, 4 };

            double[] expected = { 5, 13, 16, -8 };
            double[] actual = chol.Solve(B);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001));
        }
        public void SolveTest2()
        {
            double[,] value = // not positive-definite
            {
               {  6, -1,  2,  6 },
               { -1,  3, -3, -2 },
               {  2, -3,  2,  0 },
               {  6, -2,  0,  0 },
            };

            CholeskyDecomposition chol = new CholeskyDecomposition(value, true);
            double[,] L = chol.LeftTriangularFactor;

            double[,] B = Matrix.Identity(4);

            double[,] expected = 
            {
                { 0.4000,    1.2000,    1.4000,   -0.5000 },
                { 1.2000,    3.6000,    4.2000,   -2.0000 },
                { 1.4000,    4.2000,    5.4000,   -2.5000 },
                { -0.5000,  -2.0000,   -2.5000,    1.0000 }, 
            };
            double[,] actual = chol.Solve(B);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001));
        }
        public void SolveTest3()
        {
            double[,] value = // positive-definite
            {
               {  2, -1,  0 },
               { -1,  2, -1 },
               {  0, -1,  2 }
            };

            CholeskyDecomposition chol = new CholeskyDecomposition(value);
            double[,] L = chol.LeftTriangularFactor;

            double[] B = new double[] { 1, 2, 3 };

            double[] expected = new double[] { 2.5, 4.0, 3.5 };
            double[] actual = chol.Solve(B);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001));

            actual = chol.Solve(B, true);
            Assert.AreEqual(actual, B);
            Assert.IsTrue(Matrix.IsEqual(expected, B, 0.0000000000001));
        }
        public void InverseTest1()
        {
            double[,] value = // positive-definite
            {
                {  2, -1,  0 },
                { -1,  2, -1 },
                {  0, -1,  2 }
            };

            var chol = new CholeskyDecomposition(value, robust: false);
            Assert.IsTrue(chol.IsPositiveDefinite);
            var L = chol.LeftTriangularFactor;

            float[][] expected =
            {
                new float[] { 0.750f, 0.500f, 0.250f },
                new float[] { 0.500f, 1.000f, 0.500f },
                new float[] { 0.250f, 0.500f, 0.750f },
            };

            double[,] actual = chol.Inverse();
            Assert.IsTrue(actual.IsEqual(expected, 1e-6));

            double[,] inv = chol.Solve(Matrix.Identity(3));
            Assert.IsTrue(inv.IsEqual(expected, 1e-6));
        }
        public void SolveTest()
        {
            double[,] value = // positive-definite
            {
               {  2, -1,  0 },
               { -1,  2, -1 },
               {  0, -1,  2 }
            };

            CholeskyDecomposition chol = new CholeskyDecomposition(value);
            double[,] L = chol.LeftTriangularFactor;

            double[,] B = Matrix.ColumnVector(new double[] { 1, 2, 3 });

            double[,] expected = Matrix.ColumnVector(new double[] { 2.5, 4.0, 3.5 });

            double[,] actual = chol.Solve(B);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.AreNotEqual(actual, B);

            actual = chol.Solve(B, true);
            Assert.AreEqual(actual, B);
            Assert.IsTrue(Matrix.IsEqual(expected, B, 1e-10));

            Assert.IsTrue(Matrix.IsEqual(chol.Reverse(), value, 1e-6));
        }
        /// <summary>
        ///   Gets the probability density function (pdf) for
        ///   this distribution evaluated at point <c>x</c>.
        /// </summary>
        /// 
        /// <param name="x">A single point in the distribution range.</param>
        /// 
        /// <returns>
        ///   The probability of <c>x</c> occurring
        ///   in the current distribution.
        /// </returns>
        /// 
        /// <remarks>
        ///   The Probability Density Function (PDF) describes the
        ///   probability that a given value <c>x</c> will occur.
        /// </remarks>
        /// 
        public override double ProbabilityDensityFunction(double[] x)
        {
            // http://www.buch-kromann.dk/tine/nonpar/Nonparametric_Density_Estimation_multidim.pdf
            // http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/ebooks/html/spm/spmhtmlnode18.html

            double sum = 0;

            CholeskyDecomposition chol = new CholeskyDecomposition(smoothing);

            double[] delta = new double[Dimension];
            for (int i = 0; i < samples.Length; i++)
            {
                for (int j = 0; j < x.Length; j++)
                    delta[j] = (x[j] - samples[i][j]);

                sum += kernel.Function(chol.Solve(delta));
            }

            return sum / (samples.Length * determinant);
        }