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)); }
public void TestGetColumn(int rows, int columns) { var data = MatrixHelper.LoadDense(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]); } } }
public void TestFrobeniusNorm() { var A = DenseMatrix.CreateDiagonal(9, 2.0); var actual = A.FrobeniusNorm(); var expected = 6.0; Assert.AreEqual(expected, actual); var data = MatrixHelper.LoadDense(2, 2); actual = data.A.FrobeniusNorm(); expected = 5.477225575; Assert.AreEqual(expected, actual); }
public void TestInfinityNorm() { var A = DenseMatrix.CreateDiagonal(9, 2.0); var actual = A.InfinityNorm(); var expected = 2.0; Assert.AreEqual(expected, actual); var data = MatrixHelper.LoadDense(2, 2); actual = data.A.InfinityNorm(); expected = 7.0; Assert.AreEqual(expected, actual); }
public void TestGetRow(int rows, int columns) { var data = MatrixHelper.LoadSparse(rows, columns); var A = data.A; for (int i = 0; i < rows; i++) { var y = A.Row(i); for (int j = 0; j < columns; j++) { Assert.AreEqual(A.At(i, j), y[j]); } } }
public void TestL1Norm() { var A = SparseMatrix.CreateDiagonal(9, 2.0); var actual = A.L1Norm(); var expected = 2.0; Assert.AreEqual(expected, actual); var data = MatrixHelper.LoadSparse(2, 2); actual = data.A.L1Norm(); expected = 6.0; Assert.AreEqual(expected, actual); }
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)); }
public void TestMatrixMultiply(int rows, int columns) { var data = MatrixHelper.LoadDense(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); }
public void TestMatrixVectorTransposeMultiply(int rows, int columns) { var data = MatrixHelper.LoadDense(rows, columns); var A = data.A; var y = data.y; var actual = Vector.Create(A.ColumnCount, 0.0); A.TransposeMultiply(y, actual); CollectionAssert.AreEqual(data.ATy, actual); var AT = data.AT; AT.Multiply(y, actual); CollectionAssert.AreEqual(data.ATy, actual); }
public void TestSetColumn(int rows, int columns) { var data = MatrixHelper.LoadDense(rows, columns); var x = new Complex(-1.1, 0); var A = data.A.Clone(); var z = Vector.Create(rows, x); for (int j = 0; j < columns; j++) { A.SetColumn(j, z); for (int i = 0; i < rows; i++) { Assert.AreEqual(x, A.At(i, j)); } } }
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); } }
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); } }
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)); }
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); }