public void MapTest1() { int N = 6; var m1 = new SparseDoubleMatrix(N, N); var m2 = new SparseDoubleMatrix(N, N); // Fill matrix m2 diagonal elements with 66 m1.MapSparseIncludingDiagonal((a, r, c) => a + (r == c ? 66 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { Assert.AreEqual(i == j ? 66 : 0, m2[i, j]); } } // the same should work with the now already used matrix // fill diagonal now with 77 m1.MapSparseIncludingDiagonal((a, r, c) => a + (r == c ? 77 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { Assert.AreEqual(i == j ? 77 : 0, m2[i, j]); } } }
public void MapTest5() { int N = 6; var m1 = new SparseDoubleMatrix(N, N); var m2 = new SparseDoubleMatrix(N, N); // Pre-fill m1 elements right of the diagonal with values for (int i = 0; i < N - 1; ++i) { m1[i, i + 1] = i * 13; } // Fill matrix m2 diagonal elements with 66 m1.MapSparseIncludingDiagonal((a, r, c) => 3 * a + (r == c ? 66 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { double expected = 0; if (i == j) { expected = 66; } else if (i + 1 == j) { expected = i * 13 * 3; } Assert.AreEqual(expected, m2[i, j]); } } // the same should work with the now already used matrix // fill diagonal now with 77 m1.MapSparseIncludingDiagonal((a, r, c) => 5 * a + (r == c ? 77 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { double expected = 0; if (i == j) { expected = 77; } else if (i + 1 == j) { expected = i * 13 * 5; } Assert.AreEqual(expected, m2[i, j]); } } }
public void MapTest3() { int N = 6; var m1 = new SparseDoubleMatrix(N, N); var m2 = new SparseDoubleMatrix(N, N); // Pre-fill m2 elements right of the diagonal with values, these values should be discarded; for (int i = 0; i < N - 1; ++i) { m2[i, i + 1] = i * 13; } // Fill matrix m2 diagonal elements with 66 m1.MapSparseIncludingDiagonal((a, r, c) => a + (r == c ? 66 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { Assert.AreEqual(i == j ? 66 : 0, m2[i, j]); } } // the same should work with the now already used matrix // fill diagonal now with 77 m1.MapSparseIncludingDiagonal((a, r, c) => a + (r == c ? 77 : 0), m2); for (int i = 0; i < m2.RowCount; ++i) { for (int j = 0; j < m2.ColumnCount; ++j) { Assert.AreEqual(i == j ? 77 : 0, m2[i, j]); } } }