예제 #1
0
        public void TestOfIndexed(int rows, int columns)
        {
            var sparseData = MatrixHelper.LoadSparse(rows, columns);

            var sparseA = sparseData.A;
            var sparseB = SparseMatrix.OfIndexed(rows, columns, sparseA.EnumerateIndexed());

            Assert.IsTrue(sparseA.Equals(sparseB));
        }
예제 #2
0
        public void TestMatrixSum(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;
            var B = data.B;

            var actual = A.Add(B);

            CollectionAssert.AreEqual(data.ApB.Values, actual.Values);
        }
예제 #3
0
        public void TestKeep()
        {
            var data = MatrixHelper.LoadSparse(2, 2);

            var A = data.A.Clone();

            // Keep strict upper part of the matrix.
            int nnz = A.Keep((i, j, a) => i < j);

            Assert.AreEqual(nnz, 1);
            Assert.AreEqual(A.NonZerosCount, 1);
        }
예제 #4
0
        public void TestEnumerateIndexed(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;

            double sum = 0;

            A.EnumerateIndexed((i, j, a) => sum += a.Real * a.Real + a.Imaginary * a.Imaginary);

            Assert.AreEqual(A.FrobeniusNorm(), Math.Sqrt(sum));
        }
예제 #5
0
        public void TestOfColumnMajor(int rows, int columns)
        {
            var denseData = MatrixHelper.LoadDense(rows, columns);
            var denseA    = denseData.A;

            var sparseData = MatrixHelper.LoadSparse(rows, columns);
            var sparseA    = sparseData.A;

            var sparseB = SparseMatrix.OfColumnMajor(rows, columns, denseA.Values);

            Assert.IsTrue(sparseA.Equals(sparseB));
        }
예제 #6
0
        public void TestMatrixVectorTransposeMultiplyUpdate(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;
            var y = data.y;

            var actual = Vector.Create(A.ColumnCount, 1.0);

            A.TransposeMultiply(1.0, y, 0.0, actual);

            CollectionAssert.AreEqual(data.ATy, actual);
        }
예제 #7
0
        public void TestMatrixVectorMultiplyUpdate(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;
            var x = data.x;

            var actual = Vector.Create(A.RowCount, 1.0);

            A.Multiply(1.0, x, 0.0, actual);

            CollectionAssert.AreEqual(data.Ax, actual);
        }
예제 #8
0
        public void TestMatrixTranspose(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;
            var B = data.B;

            var actualA = A.Transpose();
            var actualB = B.Transpose();

            CollectionAssert.AreEqual(data.AT.Values, actualA.Values);
            CollectionAssert.AreEqual(data.BT.Values, actualB.Values);
        }
예제 #9
0
        public void TestDropZeros()
        {
            var data = MatrixHelper.LoadSparse(2, 2);

            var A = data.A.Clone();

            A.Values[0] = Complex.Zero;

            int nnz = A.DropZeros();

            Assert.AreEqual(nnz, 3);
            Assert.AreEqual(A.NonZerosCount, 3);
        }
예제 #10
0
        public void TestOfMatrix(int rows, int columns)
        {
            var sparseData = MatrixHelper.LoadSparse(rows, columns);

            var sparseA = sparseData.A;
            var sparseB = SparseMatrix.OfMatrix(sparseA);

            Assert.IsTrue(sparseA.Equals(sparseB));

            var denseData = MatrixHelper.LoadDense(rows, columns);

            sparseB = SparseMatrix.OfMatrix(denseData.A);

            Assert.IsTrue(sparseA.Equals(sparseB));
        }
예제 #11
0
        public void TestInfinityNorm()
        {
            var A = SparseMatrix.CreateDiagonal(9, 2.0);

            var actual   = A.InfinityNorm();
            var expected = 2.0;

            Assert.AreEqual(expected, actual);

            var data = MatrixHelper.LoadSparse(2, 2);

            actual   = data.A.InfinityNorm();
            expected = 7.0;

            Assert.AreEqual(expected, actual);
        }
예제 #12
0
        public void TestFrobeniusNorm()
        {
            var A = SparseMatrix.CreateDiagonal(9, 2.0);

            var actual   = A.FrobeniusNorm();
            var expected = 6.0;

            Assert.AreEqual(expected, actual);

            var data = MatrixHelper.LoadSparse(2, 2);

            actual   = data.A.FrobeniusNorm();
            expected = 5.477225575;

            Assert.AreEqual(expected, actual);
        }
예제 #13
0
        public void TestGetColumn(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;

            for (int j = 0; j < columns; j++)
            {
                var y = A.Column(j);

                for (int i = 0; i < rows; i++)
                {
                    Assert.AreEqual(A.At(i, j), y[i]);
                }
            }
        }
예제 #14
0
        public void TestOfArray(int rows, int columns)
        {
            var sparseData = MatrixHelper.LoadSparse(rows, columns);

            var sparseA = sparseData.A;

            var array = new Complex[rows, columns];

            foreach (var a in sparseA.EnumerateIndexed())
            {
                array[a.Item1, a.Item2] = a.Item3;
            }

            var sparseB = SparseMatrix.OfArray(array);

            Assert.IsTrue(sparseA.Equals(sparseB));
        }
예제 #15
0
        public void TestMatrixMultiply(int rows, int columns)
        {
            var data = MatrixHelper.LoadSparse(rows, columns);

            var A = data.A;
            var B = data.B;

            var AT = data.AT;
            var BT = data.BT;

            var actual = AT.Multiply(B);

            CollectionAssert.AreEqual(data.ATmB.Values, actual.Values, ComplexNumberComparer.Default);

            actual = A.Multiply(BT);

            CollectionAssert.AreEqual(data.AmBT.Values, actual.Values, ComplexNumberComparer.Default);
        }
예제 #16
0
        public void TestMatrixPermuteColumns(int rows, int columns)
        {
            var p = Permutation.Create(columns, -1);

            var data = MatrixHelper.LoadSparse(rows, columns);

            var A  = data.A;
            var Ap = A.PermuteColumns(p);

            var actualColumn   = new Complex[rows];
            var expectedColumn = new Complex[rows];

            for (int i = 0; i < columns; i++)
            {
                A.Column(p[i], expectedColumn);
                Ap.Column(i, actualColumn);

                CollectionAssert.AreEqual(expectedColumn, actualColumn);
            }
        }
예제 #17
0
        public void TestMatrixPermuteRows(int rows, int columns)
        {
            var p = Permutation.Create(rows, -1);

            var data = MatrixHelper.LoadSparse(rows, columns);

            var A  = data.A;
            var Ap = A.Clone();

            var actualRow   = new Complex[columns];
            var expectedRow = new Complex[columns];

            Ap.PermuteRows(p);

            for (int i = 0; i < rows; i++)
            {
                A.Row(p[i], expectedRow);
                Ap.Row(i, actualRow);

                CollectionAssert.AreEqual(expectedRow, actualRow);
            }
        }
예제 #18
0
        public void TestOfJaggedArray(int rows, int columns)
        {
            var sparseData = MatrixHelper.LoadSparse(rows, columns);

            var sparseA = sparseData.A;

            var array = new Complex[rows][];

            for (int i = 0; i < rows; i++)
            {
                var values = new Complex[columns];

                for (int j = 0; j < columns; j++)
                {
                    values[j] = sparseA.At(i, j);
                }

                array[i] = values;
            }

            var sparseB = SparseMatrix.OfJaggedArray(array);

            Assert.IsTrue(sparseA.Equals(sparseB));
        }
예제 #19
0
        public void TestOfIndexed_Coo(int rows, int columns)
        {
            var sparseData = MatrixHelper.LoadSparse(rows, columns);

            var sparseA = sparseData.A;

            var coo = new CoordinateStorage <Complex>(rows, columns, sparseA.NonZerosCount);

            foreach (var a in sparseA.EnumerateIndexed())
            {
                coo.At(a.Item1, a.Item2, a.Item3);
            }

            var sparseB = SparseMatrix.OfIndexed(coo);

            Assert.IsTrue(sparseA.Equals(sparseB));

            var sparseC = SparseMatrix.OfIndexed(coo, true);

            Assert.IsTrue(sparseA.Equals(sparseC));
            Assert.IsNull(coo.Values);
            Assert.IsNull(coo.RowIndices);
            Assert.IsNull(coo.ColumnIndices);
        }