Esempio n. 1
0
        private static void TestMatrixMatrixMultiplication(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A1 = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                var A2 = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix);
                var expectedA1TimesA2 = Matrix.CreateFromArray(
                    MatrixOperations.MatrixTimesMatrix(SquareSingular10by10.Matrix, RectangularFullRank10by5.Matrix));
                var expectedTransposeA2TimesA1 = Matrix.CreateFromArray(
                    MatrixOperations.MatrixTimesMatrix(
                        MatrixOperations.Transpose(RectangularFullRank10by5.Matrix), SquareSingular10by10.Matrix));

                // MultiplyRight() without transposition
                comparer.AssertEqual(expectedA1TimesA2, A1.MultiplyRight(A2, false, false));

                // operator*
                comparer.AssertEqual(expectedA1TimesA2, A1 * A2);

                // MultiplyRight() with transposition
                comparer.AssertEqual(expectedTransposeA2TimesA1, A2.MultiplyRight(A1, true, false));

                // MultiplyRight() with incorrect dimensions
                Assert.Throws <NonMatchingDimensionsException>(() => A2.MultiplyRight(A1, false, false));
            });
        }
Esempio n. 2
0
        private static void TestMatrixVectorMultiplicationIntoResult(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // The result vectors will first be set to some non zero values to make sure that the result overwrites
                // them instead of being added to them.

                // MultiplyIntoResult() - untransposed
                var A = CscMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols,
                                                   SparseRectangular10by5.CscValues, SparseRectangular10by5.CscRowIndices, SparseRectangular10by5.CscColOffsets,
                                                   true);
                var x5             = Vector.CreateFromArray(SparseRectangular10by5.Lhs5);
                var b10Expected    = Vector.CreateFromArray(SparseRectangular10by5.Rhs10);
                Vector b10Computed = Vector.CreateWithValue(SparseRectangular10by5.NumRows, 1.0);
                //Vector bComputed = Vector.CreateZero(SparseRectangular10by5.NumRows);
                A.MultiplyIntoResult(x5, b10Computed, false);
                comparer.AssertEqual(b10Expected, b10Computed);

                // MultiplyIntoResult() - transposed
                var x10           = Vector.CreateFromArray(SparseRectangular10by5.Lhs10);
                var b5Expected    = Vector.CreateFromArray(SparseRectangular10by5.Rhs5);
                Vector b5Computed = Vector.CreateWithValue(SparseRectangular10by5.NumCols, 1.0);
                A.MultiplyIntoResult(x10, b5Computed, true);
                comparer.AssertEqual(b5Expected, b5Computed);
            });
        }
Esempio n. 3
0
        private static void TestSystemSolution(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // invertible (rank = 10)
                var A1         = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);
                var b1         = Vector.CreateFromArray(SquareInvertible10by10.Rhs);
                var x1Expected = Vector.CreateFromArray(SquareInvertible10by10.Lhs);
                LUFactorization factorization1 = A1.FactorLU();
                Vector x1Computed = factorization1.SolveLinearSystem(b1);
                comparer.AssertEqual(x1Expected, x1Computed);

                // singular (rank = 8)
                var A2 = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                var b2 = Vector.CreateFromArray(SquareSingular10by10.Rhs);
                LUFactorization factorization2 = A2.FactorLU();
                Assert.Throws <SingularMatrixException>(() => factorization2.SolveLinearSystem(b2));

                // singular (rank = 9)
                var A3 = Matrix.CreateFromArray(SquareSingularSingleDeficiency10by10.Matrix);
                var b3 = Vector.CreateFromArray(SquareSingularSingleDeficiency10by10.Rhs);
                LUFactorization factorization3 = A3.FactorLU();
                Assert.Throws <SingularMatrixException>(() => factorization3.SolveLinearSystem(b3));
            });
        }
Esempio n. 4
0
        private static void TestFactorsLU(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // invertible (rank = 10)
                var A1         = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);
                var expectedL1 = Matrix.CreateFromArray(SquareInvertible10by10.FactorL);
                var expectedU1 = Matrix.CreateFromArray(SquareInvertible10by10.FactorU);
                LUFactorization factorization1 = A1.FactorLU();
                Matrix computedL1 = factorization1.GetFactorL();
                Matrix computedU1 = factorization1.GetFactorU();
                comparer.AssertEqual(expectedL1, computedL1);
                comparer.AssertEqual(expectedU1, computedU1);

                // singular (rank = 8)
                var A2         = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                var expectedL2 = Matrix.CreateFromArray(SquareSingular10by10.FactorL);
                var expectedU2 = Matrix.CreateFromArray(SquareSingular10by10.FactorU);
                LUFactorization factorization2 = A2.FactorLU();
                Matrix computedL2 = factorization2.GetFactorL();
                Matrix computedU2 = factorization2.GetFactorU();
                comparer.AssertEqual(expectedL2, computedL2);
                comparer.AssertEqual(expectedU2, computedU2);

                // singular (rank = 9)
                var A3         = Matrix.CreateFromArray(SquareSingularSingleDeficiency10by10.Matrix);
                var expectedL3 = Matrix.CreateFromArray(SquareSingularSingleDeficiency10by10.FactorL);
                var expectedU3 = Matrix.CreateFromArray(SquareSingularSingleDeficiency10by10.FactorU);
                LUFactorization factorization3 = A3.FactorLU();
                Matrix computedL3 = factorization3.GetFactorL();
                Matrix computedU3 = factorization3.GetFactorU();
                comparer.AssertEqual(expectedL3, computedL3);
                comparer.AssertEqual(expectedU3, computedU3);
            });
        }
        private static void TestSystemSolution(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // invertible
                var A1            = TriangularUpper.CreateFromArray(UpperInvertible10by10.Matrix);
                var b1            = Vector.CreateFromArray(UpperInvertible10by10.Rhs);
                var x1Expected    = Vector.CreateFromArray(UpperInvertible10by10.Lhs);
                Vector x1Computed = A1.SolveLinearSystem(b1);
                comparer.AssertEqual(x1Expected, x1Computed);

                // singular
                var A2            = TriangularUpper.CreateFromArray(UpperSingular10by10.Matrix);
                var b2            = Vector.CreateFromArray(UpperSingular10by10.Rhs);
                var x2Expected    = Vector.CreateFromArray(UpperSingular10by10.Lhs);
                Vector x2Computed = A2.SolveLinearSystem(b2);
                Assert.False(comparer.AreEqual(x2Expected, x2Computed));

                // invertible - solve transposed (forward substitution)
                Matrix A3         = Matrix.CreateFromArray(UpperInvertible10by10.Matrix).Invert().Transpose();
                Vector x3Expected = A3 * b1;
                Vector x3Computed = A1.SolveLinearSystem(b1, true);
                comparer.AssertEqual(x3Expected, x3Computed);
            });
        }
Esempio n. 6
0
        internal static void TestInvertAndDeterminant(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var matrix = Matrix.CreateFromArray(new double[, ]
                {
                    { 87.5, 0, 0, 1 },
                    { 90, 0, 0, 1 },
                    { 90, 2.5, 225, 1 },
                    { 87.5, 2.5, 218.75, 1 }
                });
                (Matrix inverse, double determinant) = matrix.InvertAndDeterminant();

                double detExpected  = 39.0625;
                var inverseExpected = Matrix.CreateFromArray(new double[, ]
                {
                    { -0.4, 0.4, 0, 0 },
                    { -14.4, 14, -14, 14.4 },
                    { 0.16, -0.16, 0.16, -0.16 },
                    { 36, -35, 0, 0 }
                });

                // operator+
                Assert.Equal(detExpected, determinant, 9);
                comparer.AssertEqual(inverseExpected, inverse);
            });
        }
Esempio n. 7
0
 private static void TestNorm2(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         var vector = Vector.CreateFromArray(TestVectors.Vector1);
         comparer.AssertEqual(TestVectors.Norm2OfVector1, vector.Norm2());
     });
 }
Esempio n. 8
0
        private static void TestNorm2(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var vector = SparseVector.CreateFromArrays(SparseVector10.Length, SparseVector10.NonZeroValues,
                                                           SparseVector10.NonZeroIndices, true, false);

                // Norm2()
                comparer.AssertEqual(SparseVector10.Norm2OfThis, vector.Norm2());
            });
        }
Esempio n. 9
0
 private static void TestDeterminantInvertiblePositive(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // invertible (rank = 10) with positive det
         var A = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);
         LUFactorization factorization = A.FactorLU();
         double det = factorization.CalcDeterminant();
         comparer.AssertEqual(SquareInvertible10by10.Determinant, det);
     });
 }
Esempio n. 10
0
 private static void TestDeterminantSingular2(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // singular (rank = 9)
         var A = Matrix.CreateFromArray(SquareSingularSingleDeficiency10by10.Matrix);
         LUFactorization factorization = A.FactorLU();
         double det = factorization.CalcDeterminant();
         comparer.AssertEqual(SquareSingularSingleDeficiency10by10.Determinant, det);
     });
 }
 private static void TestDeterminant(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // positive definite
         var A = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
         CholeskyFull factorization = A.FactorCholesky(true);
         double detComputed         = factorization.CalcDeterminant();
         comparer.AssertEqual(SymmPosDef10by10.Determinant, detComputed);
     });
 }
Esempio n. 12
0
 private static void TestIndefiniteSystem(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         var comparer             = new MatrixComparer(1E-4);
         double residualTolerance = 1e-8;
         (Matrix A, Vector b, Vector xExpected, IPreconditioner M) = DiagonalIndefinite.BuildIndefiniteSystem(20);
         var minres = new MinRes(A.NumRows, residualTolerance, 0, false, false);
         (IVector xComputed, MinresStatistics stats) = minres.Solve(A, b);
         comparer.AssertEqual(xExpected, xComputed);
     });
 }
 private static void TestInversion(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // positive definite
         var A = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
         var inverseAExpected       = Matrix.CreateFromArray(SymmPosDef10by10.Inverse);
         CholeskyFull factorization = A.FactorCholesky(true);
         Matrix inverseAComputed    = factorization.Invert(true);
         comparer.AssertEqual(inverseAExpected, inverseAComputed);
     });
 }
Esempio n. 14
0
        private static void TestSubtraction(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A1       = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                var A2       = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
                var expected = Matrix.CreateFromArray(
                    MatrixOperations.LinearCombination(1.0, SquareSingular10by10.Matrix, -1.0, SymmPosDef10by10.Matrix));

                // operator+
                comparer.AssertEqual(expected, A1 - A2);
            });
        }
        private static void TestMinNormSolution(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A         = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix).Transpose();
                var b         = Vector.CreateFromArray(RectangularFullRank10by5.RhsMinNorm);
                var xExpected = Vector.CreateFromArray(RectangularFullRank10by5.LhsMinNorm);

                LQFactorization factorization = A.FactorLQ();
                Vector xComputed = factorization.SolveMinNorm(b);
                comparer.AssertEqual(xExpected, xComputed);
            });
        }
 private static void TestSystemSolution(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // positive definite
         var A         = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
         var b         = Vector.CreateFromArray(SymmPosDef10by10.Rhs);
         var xExpected = Vector.CreateFromArray(SymmPosDef10by10.Lhs);
         CholeskyFull factorization = A.FactorCholesky(true);
         Vector xComputed           = factorization.SolveLinearSystem(b);
         comparer.AssertEqual(xExpected, xComputed);
     });
 }
        private static void TestEigensystemCalledFromPackedMatrix(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A = SymmetricMatrix.CreateFromArray(SymmPosDef10by10.Matrix);
                var eigenvaluesExpected  = Vector.CreateFromArray(SymmPosDef10by10.Eigenvalues);
                var eigenvectorsExpected = Matrix.CreateFromArray(SymmPosDef10by10.Eigenvectors);
                (Vector eigenvalues, Matrix eigenvectors) = A.CalcEigensystem();

                // Check
                comparer.AssertEqual(eigenvaluesExpected, eigenvalues);
                comparer.AssertEqual(eigenvectorsExpected, eigenvectors);
            });
        }
Esempio n. 18
0
        private static void TestDotProduct(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var v1 = Vector.CreateFromArray(TestVectors.Vector1);
                var v2 = Vector.CreateFromArray(TestVectors.Vector2);

                // DotProduct()
                comparer.AssertEqual(TestVectors.DotProduct, v1.DotProduct(v2));

                // operator*
                comparer.AssertEqual(TestVectors.DotProduct, v1 * v2);
            });
        }
Esempio n. 19
0
 private static void TestPosDefSparseSystem(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         var comparer  = new MatrixComparer(1E-6);
         int n         = SparsePosDef10by10.Order;
         var A         = Matrix.CreateFromArray(SparsePosDef10by10.Matrix);
         var b         = Vector.CreateFromArray(SparsePosDef10by10.Rhs);
         var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs);
         var minres    = new MinRes(n, 1e-10, 0, false, false);
         (IVector xComputed, MinresStatistics stats) = minres.Solve(A, b);
         comparer.AssertEqual(xExpected, xComputed);
     });
 }
Esempio n. 20
0
 private static void TestIndefiniteSystem(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         (Matrix A, Vector b, Vector xExpected, IPreconditioner M) = DiagonalIndefinite.BuildIndefiniteSystem(20);
         var builder = new CGAlgorithm.Builder();
         builder.ResidualTolerance     = 1E-6;
         builder.MaxIterationsProvider = new PercentageMaxIterationsProvider(1.0);
         var cg                    = builder.Build();
         Vector xComputed          = Vector.CreateZero(A.NumRows);
         IterativeStatistics stats = cg.Solve(A, b, xComputed, true);
         Assert.False(comparer.AreEqual(xExpected, xComputed));
     });
 }
Esempio n. 21
0
        public static void RunMultiproviderTest(LinearAlgebraProviderChoice providers, Action test)
        {
            LinearAlgebraProviderChoice defaultProviders = LibrarySettings.LinearAlgebraProviders; // Store it for later

            LibrarySettings.LinearAlgebraProviders = providers;

            try
            {
                test();
            }
            finally
            {
                LibrarySettings.LinearAlgebraProviders = defaultProviders; // Once finished, reset the default providers
            }
        }
        private static void TestMatrixVectorMultiplicationIntoResult(LinearAlgebraProviderChoice providers)
        {
            // The result vectors will first be set to some non zero values to make sure that the result overwrites
            // them instead of being added to them.

            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A            = TriangularLower.CreateFromArray(LowerInvertible10by10.Matrix);
                var x            = Vector.CreateFromArray(LowerInvertible10by10.Lhs);
                var bExpected    = Vector.CreateFromArray(LowerInvertible10by10.Rhs);
                Vector bComputed = Vector.CreateWithValue(A.NumRows, 1.0);
                A.MultiplyIntoResult(x, bComputed, false);
                comparer.AssertEqual(bExpected, bComputed);
            });
        }
Esempio n. 23
0
 private static void TestDeterminantInvertibleNegative(LinearAlgebraProviderChoice providers)
 {
     TestSettings.RunMultiproviderTest(providers, delegate()
     {
         // Switch 2 rows to make the det negative
         var A       = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);
         Vector row0 = A.GetRow(0);
         Vector row9 = A.GetRow(9);
         A.SetSubrow(0, row9);
         A.SetSubrow(9, row0);
         LUFactorization factorization = A.FactorLU();
         double det = factorization.CalcDeterminant();
         comparer.AssertEqual(-SquareInvertible10by10.Determinant, det);
     });
 }
Esempio n. 24
0
        private static void TestDotProduct(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var sparse = SparseVector.CreateFromArrays(SparseVector10.Length, SparseVector10.NonZeroValues,
                                                           SparseVector10.NonZeroIndices, true, false);
                var dense = Vector.CreateFromArray(SparseVector10.OtherVector);

                // DotProduct() with dense vector
                comparer.AssertEqual(SparseVector10.DotThisTimesOther, sparse.DotProduct(dense));

                // DotProduct() with itsedlf
                comparer.AssertEqual(SparseVector10.Norm2OfThis * SparseVector10.Norm2OfThis, sparse.DotProduct(sparse));
            });
        }
        private static void TestFactorization(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // positive definite
                var A1         = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
                var expectedU1 = Matrix.CreateFromArray(SymmPosDef10by10.FactorU);
                CholeskyFull factorization1 = A1.FactorCholesky(true);
                Matrix computedU1           = factorization1.GetFactorU();
                comparer.AssertEqual(expectedU1, computedU1);

                // singular
                var A2 = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                Assert.Throws <IndefiniteMatrixException>(() => A2.FactorCholesky(true));
            });
        }
        private static void TestFactorsLQ(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A            = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix).Transpose();
                Matrix expectedL = Matrix.CreateFromArray(RectangularFullRank10by5.LQFactorL);
                Matrix expectedQ = Matrix.CreateFromArray(RectangularFullRank10by5.LQFactorQ);

                LQFactorization factorization = A.FactorLQ();
                Matrix computedL = factorization.GetFactorL();
                Matrix computedQ = factorization.GetFactorQ();

                comparer.AssertEqual(expectedL, computedL);
                comparer.AssertEqual(expectedQ, computedQ);
            });
        }
        private static void TestFactorsQR(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A            = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix);
                Matrix expectedQ = Matrix.CreateFromArray(RectangularFullRank10by5.QRFactorQ);
                Matrix expectedR = Matrix.CreateFromArray(RectangularFullRank10by5.QRqrFactorR);

                QRFactorization factorization = A.FactorQR();
                Matrix computedQ = factorization.GetFactorQ();
                Matrix computedR = factorization.GetFactorR();

                comparer.AssertEqual(expectedQ, computedQ);
                comparer.AssertEqual(expectedR, computedR);
            });
        }
        private static void TestEigenvaluesOnly(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);
                var eigenvaluesRealExpected      = Vector.CreateFromArray(SquareInvertible10by10.EigenvaluesRealPart);
                var eigenvaluesImaginaryExpected = Vector.CreateFromArray(SquareInvertible10by10.EigenvaluesImaginaryPart);
                var eigensystem = NonSymmetricEigensystemFull.Create(A.NumRows, A.RawData, false, false);

                // Check
                comparer.AssertEqual(eigenvaluesRealExpected, eigensystem.EigenvaluesReal);
                comparer.AssertEqual(eigenvaluesImaginaryExpected, eigensystem.EigenvaluesImaginary);
                Assert.True(eigensystem.EigenvectorsLeft == null);
                Assert.True(eigensystem.EigenvectorsRight == null);
            });
        }
        private static void TestEigenvaluesAndEigenvectors(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var A = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
                var eigenvaluesExpected  = Vector.CreateFromArray(SymmPosDef10by10.Eigenvalues);
                var eigenvectorsExpected = Matrix.CreateFromArray(SymmPosDef10by10.Eigenvectors);
                var eigensystem          = SymmetricEigensystemFull.Create(A.NumRows, A.RawData, true);

                // Check
                comparer.AssertEqual(eigenvaluesExpected, eigensystem.EigenvaluesReal);
                comparer.AssertEqual(eigenvectorsExpected, eigensystem.EigenvectorsRight);
                Assert.True(eigensystem.EigenvaluesImaginary == null);
                Assert.True(eigensystem.EigenvectorsLeft == null);
            });
        }
Esempio n. 30
0
        private static void TestAxpy(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                var v1       = Vector.CreateFromArray(TestVectors.Vector1);
                var v2       = Vector.CreateFromArray(TestVectors.Vector2);
                var expected = Vector.CreateFromArray(TestVectors.Vector1PlusVector2Times3);

                // Axpy()
                comparer.AssertEqual(expected, v1.Axpy(v2, TestVectors.Scalar2));

                // AxpyIntoThis
                var temp = Vector.CreateFromVector(v1);
                temp.AxpyIntoThis(v2, TestVectors.Scalar2);
                comparer.AssertEqual(expected, temp);
            });
        }