Пример #1
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var temp = A.RowNorms(1);
                var a    = DenseVector.Create(cols, i => temp[i]);
                var b    = Util.GetData(a);
                var D    = SparseMatrix.OfDiagonalVector(a);

                var result = new BenchmarkResult();

                Util.Tic();
                var B = A.Multiply(D);

                result.Time1 = Util.Toc();

                Util.Tic();
                A.FastScaleColumns(b, A);
                result.Time2 = Util.Toc();

                result.Success = MatrixComparer.Equals(A, B, EPS);

                return(result);
            }
Пример #2
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var result = new BenchmarkResult();

                var a = DenseVector.Create(rows, (i) => 1.0 + i);
                var D = DiagonalMatrix.OfDiagonal(rows, cols, a);
                var C = (SparseMatrix)A.Clone();

                Util.Tic();
                var B = A.Add(D);

                result.Time1 = Util.Toc();

                Util.Tic();
                C.FastAddDiagonalMatrix(Util.GetData(a), C);
                result.Time2 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
Пример #3
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var B = Create.SparseMatrix(rows, cols, density, symmetric);

                var result = new BenchmarkResult();

                Util.Tic();
                var C = A.FastAdd(B);

                result.Time2 = Util.Toc();

                Util.Tic();
                //var D = A.Add(B);
                var D = A + B;

                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(C, D, EPS);

                return(result);
            }
Пример #4
0
        public void TestDropZeros(int size)
        {
            var A = MatrixLoader.A(size);

            double threshold = 1.0e-5;

            var B = A.Clone() as SparseMatrix;

            foreach (var item in A.EnumerateIndexed(Zeros.AllowSkip))
            {
                int i = item.Item1;
                int j = item.Item2;

                if (i != j)
                {
                    // Make all off-diagonal entries small.
                    B[i, j] = threshold / 10;
                }
            }

            var C = B.Clone() as SparseMatrix;

            B.CoerceZero(threshold);
            C.DropZeros(threshold);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #5
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var S = CreateSparse.RandomSymmetric(4, 0.5);

                var result = new BenchmarkResult();

                Util.Tic();
                var B = S.FastKroneckerProduct(A);

                result.Time2 = Util.Toc();

                Util.Tic();
                var C = S.KroneckerProduct(A);

                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
Пример #6
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var p = Util.Permutation(A.RowCount, rand);

                var B = (SparseMatrix)A.Clone();
                var C = (SparseMatrix)A.Clone();

                var result = new BenchmarkResult();

                Util.Tic();
                C.FastPermuteColumns(p);
                result.Time2 = Util.Toc();

                Util.Tic();
                B.PermuteColumns(new Permutation(p));
                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
Пример #7
0
        public void TestAdd(int size)
        {
            var A = MatrixLoader.A(size);
            var B = MatrixLoader.B(size, false);

            var C = A.Add(B);
            var D = A.FastAdd(B);

            Assert.IsTrue(MatrixComparer.Equals(C, D, EPS));
        }
Пример #8
0
        public void TestNormalizeColumns(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.NormalizeColumns(1);

            A.FastNormalizeColumns(1);

            Assert.IsTrue(MatrixComparer.Equals(A, B, EPS));
        }
Пример #9
0
        public void TestUpperTriangle(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.UpperTriangle() as SparseMatrix;
            var C = A.Clone() as SparseMatrix;

            C.FastUpperTriangle();

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #10
0
        public void TestTranspose(int size)
        {
            var A = MatrixLoader.A(size);

            var B = A.Transpose();
            var C = CreateSparse.Zeros(A.ColumnCount, A.RowCount, A.NonZerosCount);

            A.FastTranspose(C);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #11
0
        public void TestInvertLowerTriangle()
        {
            var A = DenseMatrix.Build.Random(N, N, RAND_SEED);

            var L = (DenseMatrix)A.LowerTriangle();
            var S = (DenseMatrix)L.Inverse();

            L.InvertLowerTriangle();

            // Shouldn't use random matrix: can be ill-conditioned.
            Assert.IsTrue(MatrixComparer.Equals(S, L, 1000 * EPS));
        }
Пример #12
0
        public void TestKroneckerProduct(int size)
        {
            var A = MatrixLoader.A(size);

            var E = DenseMatrix.OfColumnMajor(2, 2, new[] { 1.0, 1.0, 1.0, 1.0 });
            var S = SparseMatrix.OfMatrix(E);

            var B = S.KroneckerProduct(A);
            var C = S.FastKroneckerProduct(A);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #13
0
        public void TestScaleColumns(int size)
        {
            var A = MatrixLoader.A(size);

            var a = A.ColumnNorms(1);
            var b = Util.GetData(a);
            var D = SparseMatrix.OfDiagonalVector(a);

            var B = A.Multiply(D);

            A.FastScaleColumns(b, A);

            Assert.IsTrue(MatrixComparer.Equals(A, B, EPS));
        }
Пример #14
0
        public void TestPermuteRows(int size)
        {
            var A = MatrixLoader.A(size);

            var p = Util.Permutation(A.RowCount, RAND_SEED);

            var B = A.Clone() as SparseMatrix;
            var C = A.Clone() as SparseMatrix;

            B.PermuteRows(new Permutation(p));
            C.FastPermuteRows(p);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #15
0
        public void TestScaleRows(int size)
        {
            var A = MatrixLoader.A(size);

            var a = Helper.ToComplexVector(A.RowNorms(1));
            var b = Util.GetData(a);
            var D = SparseMatrix.OfDiagonalVector(a);

            var B = D.Multiply(A);

            A.FastScaleRows(b, A);

            Assert.IsTrue(MatrixComparer.Equals(A, B, EPS));
        }
Пример #16
0
        public void TestAddDiagonalMatrix(int size)
        {
            var A = MatrixLoader.A(size);

            int rows = A.RowCount;
            int cols = A.ColumnCount;

            var a = DenseVector.Create(rows, (i) => 1.0 + i);
            var D = DiagonalMatrix.OfDiagonal(rows, cols, a);

            var B = A.Add(D);
            var C = A.Clone() as SparseMatrix;

            C.FastAddDiagonalMatrix(Util.GetData(a), C);

            Assert.IsTrue(MatrixComparer.Equals(B, C, EPS));
        }
Пример #17
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var B = (SparseMatrix)A.Clone();

                var result = new BenchmarkResult();

                Util.Tic();
                B.FastUpperTriangle();
                result.Time2 = Util.Toc();

                Util.Tic();
                var C = (SparseMatrix)A.UpperTriangle();

                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                // Strict upper.
                B = (SparseMatrix)A.Clone();

                Util.Tic();
                B.FastUpperTriangle(true);
                result.Time2 += Util.Toc();

                Util.Tic();
                C             = (SparseMatrix)A.StrictlyUpperTriangle();
                result.Time1 += Util.Toc();

                result.Success &= MatrixComparer.Equals(B, C, EPS);

                return(result);
            }
Пример #18
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);
                var B = (SparseMatrix)A.Clone();

                var S = Create.SparseMatrix(repeat, cols, 2 * density, false);

                var result = new BenchmarkResult();

                var rowIndices = new int[repeat];

                for (int i = 0; i < repeat; i++)
                {
                    rowIndices[i] = rand.Next(0, rows - 1);
                }

                Util.Tic();
                A.FastSetRows(rowIndices, S);
                result.Time2 = Util.Toc();


                Util.Tic();
                for (int i = 0; i < repeat; i++)
                {
                    B.SetRow(rowIndices[i], S.Row(i));
                }
                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(A, B, EPS);

                return(result);
            }
Пример #19
0
            public override BenchmarkResult Run(BenchmarkSetup config)
            {
                int    rows      = config.RowCount;
                int    cols      = config.ColumnCount;
                int    repeat    = config.Repeat;
                double density   = config.Density;
                bool   symmetric = config.Symmetric;

                var A = Create.SparseMatrix(rows, cols, density, symmetric);

                var result = new BenchmarkResult();

                var columnIndices = new int[repeat];

                for (int i = 0; i < repeat; i++)
                {
                    columnIndices[i] = rand.Next(0, cols - 1);
                }

                Util.Tic();
                var B = A.FastGetColumns(columnIndices);

                result.Time2 = Util.Toc();


                Util.Tic();
                var C = new SparseMatrix(rows, repeat);

                for (int i = 0; i < repeat; i++)
                {
                    C.SetColumn(i, A.Column(columnIndices[i]));
                }
                result.Time1 = Util.Toc();

                result.Success = MatrixComparer.Equals(B, C, EPS);

                return(result);
            }