public void TestRandomA()
        {
            RandomHelper.Random = new Random(1);

              for (int i = 0; i < 100; i++)
              {
            // Create A.
            MatrixD a = new MatrixD(3, 3);
            RandomHelper.Random.NextMatrixD(a, 0, 1);

            LUDecompositionD d = new LUDecompositionD(a);

            if (d.IsNumericallySingular == false)
            {
              // Check solving of linear equations.
              MatrixD b = new MatrixD(3, 2);
              RandomHelper.Random.NextMatrixD(b, 0, 1);

              MatrixD x = d.SolveLinearEquations(b);
              MatrixD b2 = a * x;
              Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01));

              MatrixD aPermuted = d.L * d.U;
              Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
            }
              }
        }
        public void TestRandomA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                // Create A.
                MatrixD a = new MatrixD(3, 3);
                RandomHelper.Random.NextMatrixD(a, 0, 1);

                LUDecompositionD d = new LUDecompositionD(a);

                if (d.IsNumericallySingular == false)
                {
                    // Check solving of linear equations.
                    MatrixD b = new MatrixD(3, 2);
                    RandomHelper.Random.NextMatrixD(b, 0, 1);

                    MatrixD x  = d.SolveLinearEquations(b);
                    MatrixD b2 = a * x;
                    Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01));

                    MatrixD aPermuted = d.L * d.U;
                    Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
                }
            }
        }
        public void SolveLinearEquationsException()
        {
            MatrixD a = new MatrixD(new double[, ] {
                { 1, 2 }, { 5, 6 }, { 0, 1 }
            });
            LUDecompositionD d = new LUDecompositionD(a);

            d.SolveLinearEquations(null);
        }
        public void TestRandomRegularA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                VectorD column1 = new VectorD(3);
                RandomHelper.Random.NextVectorD(column1, 1, 2);

                VectorD column2 = new VectorD(3);
                RandomHelper.Random.NextVectorD(column2, 1, 2);

                // Make linearly independent.
                if (column1 / column1[0] == column2 / column2[0])
                {
                    column2[0]++;
                }

                // Create linearly independent third column.
                VectorD column3 = column1 + column2;
                column3[1]++;

                // Create A.
                MatrixD a = new MatrixD(3, 3);
                a.SetColumn(0, column1);
                a.SetColumn(1, column2);
                a.SetColumn(2, column3);

                LUDecompositionD d = new LUDecompositionD(a);

                MatrixD aPermuted = d.L * d.U;
                Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
                aPermuted = d.L * d.U; // Repeat with to test cached values.
                Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));

                Assert.AreEqual(false, d.IsNumericallySingular);

                // Check solving of linear equations.
                MatrixD b = new MatrixD(3, 2);
                RandomHelper.Random.NextMatrixD(b, 0, 1);

                MatrixD x  = d.SolveLinearEquations(b);
                MatrixD b2 = a * x;
                Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f));
            }
        }
        public void TestRandomRegularA()
        {
            RandomHelper.Random = new Random(1);

              for (int i = 0; i < 100; i++)
              {
            VectorD column1 = new VectorD(3);
            RandomHelper.Random.NextVectorD(column1, 1, 2);

            VectorD column2 = new VectorD(3);
            RandomHelper.Random.NextVectorD(column2, 1, 2);

            // Make linearly independent.
            if (column1 / column1[0] == column2 / column2[0])
              column2[0]++;

            // Create linearly independent third column.
            VectorD column3 = column1 + column2;
            column3[1]++;

            // Create A.
            MatrixD a = new MatrixD(3, 3);
            a.SetColumn(0, column1);
            a.SetColumn(1, column2);
            a.SetColumn(2, column3);

            LUDecompositionD d = new LUDecompositionD(a);

            MatrixD aPermuted = d.L * d.U;
            Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
            aPermuted = d.L * d.U; // Repeat with to test cached values.
            Assert.IsTrue(MatrixD.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));

            Assert.AreEqual(false, d.IsNumericallySingular);

            // Check solving of linear equations.
            MatrixD b = new MatrixD(3, 2);
            RandomHelper.Random.NextMatrixD(b, 0, 1);

            MatrixD x = d.SolveLinearEquations(b);
            MatrixD b2 = a * x;
            Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f));
              }
        }
 public void SolveLinearEquationsException()
 {
     MatrixD a = new MatrixD(new double[,] { { 1, 2 }, { 5, 6 }, { 0, 1 } });
       LUDecompositionD d = new LUDecompositionD(a);
       d.SolveLinearEquations(null);
 }