public void QrDecompositionConstructorTest2()
        {
            double[][] value =
            {
                new double[] { 1 },
                new double[] { 2 },
                new double[] { 3 }
            };


            var target = new JaggedQrDecomposition(value);

            // Decomposition Identity
            var Q   = target.OrthogonalFactor;
            var QQt = Matrix.Dot(Q.Transpose(), Q);

            Assert.IsTrue(Matrix.IsEqual(QQt, Jagged.Identity(1), 1e-6));
            double[][] reverse = target.Reverse();
            Assert.IsTrue(Matrix.IsEqual(value, reverse, 1e-6));

            // Linear system solving
            double[][] B        = Matrix.ColumnVector(new double[] { 4, 5, 6 }).ToJagged();
            double[][] expected = Jagged.ColumnVector(new double[] { 2.285714285714286 });
            double[][] actual   = target.Solve(B);

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

            double[][] R = target.UpperTriangularFactor;
            actual = value.Dot(R.Inverse());
            Assert.IsTrue(Matrix.IsEqual(Q, actual, 0.0000000000001));
        }
        public void QrDecompositionConstructorTest()
        {
            double[][] value =
            {
                new double[] {  2, -1,  0 },
                new double[] { -1,  2, -1 },
                new double[] {  0, -1,  2 }
            };


            var target = new JaggedQrDecomposition(value);

            // Decomposition Identity
            var Q   = target.OrthogonalFactor;
            var QQt = Matrix.Dot(Q, Q.Transpose());

            Assert.IsTrue(Matrix.IsEqual(QQt, Jagged.Identity(3), 1e-6));
            Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6));

            // Linear system solving
            double[][] B        = Matrix.ColumnVector(new double[] { 1, 2, 3 }).ToJagged();
            double[][] expected = Matrix.ColumnVector(new double[] { 2.5, 4.0, 3.5 }).ToJagged();
            double[][] actual   = target.Solve(B);

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

            double[] b = { 1, 2, 3 };

            double[][] expected =
            {
                new double[] { 0.75, 1, 0.75 },
                new double[] { 0.50, 2, 1.50 },
                new double[] { 0.25, 1, 2.25 }
            };

            var target = new JaggedQrDecomposition(value);

            double[][] actual = target.SolveForDiagonal(b);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value.Transpose(), target.Reverse(), 1e-6));
        }
        public void InverseTest()
        {
            double[][] value =
            {
                new double[] {  2, -1,  0 },
                new double[] { -1,  2, -1 },
                new double[] {  0, -1,  2 }
            };

            double[][] expected =
            {
                new double[] { 0.7500, 0.5000, 0.2500 },
                new double[] { 0.5000, 1.0000, 0.5000 },
                new double[] { 0.2500, 0.5000, 0.7500 },
            };


            var target = new JaggedQrDecomposition(value);

            double[][] actual = target.Inverse();
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-4));

            target = new JaggedQrDecomposition(value.Transpose(), true);
            actual = target.Inverse();
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value.Transpose(), target.Reverse(), 1e-4));
        }
        public void full_decomposition()
        {
            double[][] value =
            {
                new double[] { 1 },
                new double[] { 2 },
                new double[] { 3 }
            };


            var target = new JaggedQrDecomposition(value, economy: false);

            // Decomposition Identity
            var Q   = target.OrthogonalFactor;
            var QtQ = Matrix.Dot(Q.Transpose(), Q);

            Assert.IsTrue(Matrix.IsEqual(QtQ, Jagged.Identity(3), 1e-6));
            double[][] reverse = target.Reverse();
            Assert.IsTrue(Matrix.IsEqual(value, reverse, 1e-6));

            // Linear system solving
            double[][] B        = Matrix.ColumnVector(new double[] { 4, 5, 6 }).ToJagged();
            double[][] expected = Jagged.ColumnVector(new double[] { 2.285714285714286 });
            double[][] actual   = target.Solve(B);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001));

            var QQt = Matrix.Dot(Q, Q.Transpose());

            Assert.IsTrue(Matrix.IsEqual(QQt, Jagged.Identity(3), 1e-6));
        }
        public void QrDecompositionConstructorTest()
        {
            double[][] value =
            {
               new double[] {  2, -1,  0 },
               new double[] { -1,  2, -1 },
               new double[] {  0, -1,  2 }
            };


            var target = new JaggedQrDecomposition(value);

            // Decomposition Identity
            var Q = target.OrthogonalFactor;
            var QQt = Matrix.Multiply(Q, Q.Transpose());
            Assert.IsTrue(Matrix.IsEqual(QQt, Matrix.JaggedIdentity(3), 1e-6));
            Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6));

            // Linear system solving
            double[][] B = Matrix.ColumnVector(new double[] { 1, 2, 3 }).ToJagged();
            double[][] expected = Matrix.ColumnVector(new double[] { 2.5, 4.0, 3.5 }).ToJagged();
            double[][] actual = target.Solve(B);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001));
        }
        public void SolveTest2()
        {
            // Example from Lecture notes for MATHS 370: Advanced Numerical Methods
            // http://www.math.auckland.ac.nz/~sharp/370/qr-solving.pdf

            double[,] value =
            {
                { 1,  0,    0 },
                { 1,  7,   49 },
                { 1, 14,  196 },
                { 1, 21,  441 },
                { 1, 28,  784 },
                { 1, 35, 1225 },
            };

            // Matrices
            {
                double[,] b =
                {
                    { 4 },
                    { 1 },
                    { 0 },
                    { 0 },
                    { 2 },
                    { 5 },
                };

                double[,] expected =
                {
                    {  3.9286 },
                    { -0.5031 },
                    {  0.0153 },
                };

                var target = new QrDecomposition(value);
                double[,] actual = target.Solve(b);

                Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4));
                Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6));


                var        target2 = new JaggedQrDecomposition(value.ToJagged());
                double[][] actual2 = target2.Solve(b.ToJagged());

                Assert.IsTrue(Matrix.IsEqual(expected, actual2, atol: 1e-4));
                Assert.IsTrue(Matrix.IsEqual(value, target2.Reverse(), 1e-6));
            }

            // Vectors
            {
                double[] b        = { 4, 1, 0, 0, 2, 5 };
                double[] expected = { 3.9286, -0.5031, 0.0153 };

                var      target = new QrDecomposition(value);
                double[] actual = target.Solve(b);

                Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4));
            }
        }
        public void SolveTest()
        {
            double[][] value =
            {
                new double[] {  2, -1,  0 },
                new double[] { -1,  2, -1 },
                new double[] {  0, -1,  2 }
            };

            double[] b = { 1, 2, 3 };

            double[] expected = { 2.5000, 4.0000, 3.5000 };

            var target = new JaggedQrDecomposition(value);

            double[] actual = target.Solve(b);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value.Transpose(), target.Reverse(), 1e-6));
        }
        public void SolveTest2()
        {
            // Example from Lecture notes for MATHS 370: Advanced Numerical Methods
            // http://www.math.auckland.ac.nz/~sharp/370/qr-solving.pdf

            double[,] value =
            {
                { 1,           0,           0 },
                { 1,           7,          49 },
                { 1,          14,         196 },
                { 1,          21,         441 },
                { 1,          28,         784 },
                { 1,          35,        1225 },
            };

            // Matrices
            {
                double[,] b = 
                {
                    { 4 },
                    { 1 },
                    { 0 },
                    { 0 }, 
                    { 2 },
                    { 5 },
                };

                double[,] expected =
                {
                    { 3.9286  },
                    { -0.5031 },
                    { 0.0153  },
                };

                var target = new QrDecomposition(value);
                double[,] actual = target.Solve(b);

                Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4));
                Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-6));


                var target2 = new JaggedQrDecomposition(value.ToJagged());
                double[][] actual2 = target2.Solve(b.ToJagged());

                Assert.IsTrue(Matrix.IsEqual(expected, actual2, atol: 1e-4));
                Assert.IsTrue(Matrix.IsEqual(value, target2.Reverse(), 1e-6));
            }

            // Vectors
            {
                double[] b = { 4, 1, 0, 0, 2, 5 };
                double[] expected = { 3.9286, -0.5031, 0.0153 };

                var target = new QrDecomposition(value);
                double[] actual = target.Solve(b);

                Assert.IsTrue(Matrix.IsEqual(expected, actual, atol: 1e-4));
            }
        }
        public void InverseTest()
        {
            double[][] value =
            {
               new double[] {  2, -1,  0 },
               new double[] { -1,  2, -1 },
               new double[] {  0, -1,  2 }
            };

            double[][] expected =
            {
                new double[] { 0.7500,    0.5000,    0.2500},
                new double[] { 0.5000,    1.0000,    0.5000},
                new double[] { 0.2500,    0.5000,    0.7500},
            };


            var target = new JaggedQrDecomposition(value);

            double[][] actual = target.Inverse();
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-4));

            target = new JaggedQrDecomposition(value.Transpose(), true);
            actual = target.Inverse();
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value.Transpose(), target.Reverse(), 1e-4));
        }
        public void SolveTest()
        {
            double[][] value =
            {
               new double[] {  2, -1,  0 },
               new double[] { -1,  2, -1 },
               new double[] {  0, -1,  2 }
            };

            double[] b = { 1, 2, 3 };

            double[] expected = { 2.5000, 4.0000, 3.5000 };

            var target = new JaggedQrDecomposition(value);
            double[] actual = target.Solve(b);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(value.Transpose(), target.Reverse(), 1e-6));
        }