public static AnyRectangularMatrix Repmat(AnyRectangularMatrix source, int verticalTiles, int horizontalTiles) { RectangularMatrix target = new RectangularMatrix( (int)Math.Floor(source.RowCount * (double)verticalTiles), (int)Math.Floor(source.ColumnCount * (double)horizontalTiles)); int rows = source.RowCount; int cols = source.ColumnCount; for (int v = 0; v < verticalTiles; v++) { for (int h = 0; h < horizontalTiles; h++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { target[(v * rows) + r, (h * cols) + c] = source[r, c]; } } } } return(target); }
public void RepmatTest() { AnyRectangularMatrix source = this.ThreeByThreeSquare; int verticalTiles = 2; int horizontalTiles = 3; AnyRectangularMatrix expected = new RectangularMatrix(new[, ] { { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0 }, { 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0 }, { 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } }); AnyRectangularMatrix actual = Utilities.Repmat(source, verticalTiles, horizontalTiles); for (int r = 0; r < expected.RowCount; r++) { for (int c = 0; c < expected.ColumnCount; c++) { Assert.AreEqual(expected[r, c], actual[r, c]); } } }
public static bool IsNearlyEqual(AnyRectangularMatrix x, AnyRectangularMatrix y, EvaluationSettings s) { double m = x.FrobeniusNorm() + y.FrobeniusNorm(); double e = s.AbsolutePrecision + m * s.RelativePrecision; AnyRectangularMatrix D = x - y; return(D.FrobeniusNorm() <= e); }
public static void PrintMatrix(AnyRectangularMatrix M) { for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Console.Write("{0,12:g8} ", M[r, c]); } Console.WriteLine(); } Console.WriteLine("--"); }
private void WriteMatrix(AnyRectangularMatrix A) { Console.WriteLine("--"); for (int r = 0; r < A.RowCount; r++) { for (int c = 0; c < A.ColumnCount; c++) { Console.Write("{0} ", A[r, c]); } Console.WriteLine(); } }
public static void PrintMatrix(string name, AnyRectangularMatrix M) { Console.WriteLine($"{name}="); for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Console.Write("{0,10:g4}", M[r, c]); } Console.WriteLine(); } }
public void AnyRectangularMatrixOperations() { AnyRectangularMatrix M1 = R; AnyRectangularMatrix M2 = -R; RectangularMatrix MS = M1 + M2; Assert.IsTrue(MS == 0.0 * M1); RectangularMatrix MD = M1 - M2; Assert.IsTrue(MD == 2.0 * M1); }
/* * public static bool IsNearlyEigenvalue (ISquareMatrix M, ColumnVector v, double c) { * double n = MatrixNorm(M); * double ep = e; * if (Math.Abs(n / c) < 0.1) ep = e / n; * * * } */ private static double MatrixNorm(AnyRectangularMatrix M) { double n = 0.0; for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { n += Math.Abs(M[r, c]); } } return(n); }
public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B, double e) { double nA = MatrixNorm(A); double nB = MatrixNorm(B); for (int r = 0; r < A.RowCount; r++) { for (int c = 0; c < A.ColumnCount; c++) { if (Math.Abs(A[r, c] - B[r, c]) > e * (nA + nB)) { return(false); } } } return(true); }
public void GetColumnsTest() { AnyRectangularMatrix matrix = this.ThreeByThreeSquare; IEnumerable <int> columnIndices = new[] { 0, 2 }; AnyRectangularMatrix expected = new RectangularMatrix(new[, ] { { 0.0, 2.0 }, { 3.0, 5.0 }, { 0.0, 0.0 } }); AnyRectangularMatrix actual = Utilities.GetColumns(matrix, columnIndices); for (int r = 0; r < expected.RowCount; r++) { for (int c = 0; c < expected.ColumnCount; c++) { Assert.AreEqual(expected, actual); } } }
public static AnyRectangularMatrix GetColumns(AnyRectangularMatrix matrix, IEnumerable <int> columnIndices) { RectangularMatrix target = new RectangularMatrix(matrix.RowCount, columnIndices.Count()); int columnIndex = 0; foreach (int col in columnIndices) { for (int r = 0; r < matrix.RowCount; r++) { target[r, columnIndex] = matrix[r, col]; } columnIndex++; } return(target); }
public static SquareMatrix ConvertToSquare(AnyRectangularMatrix matrix, int dimension) { if (matrix.RowCount != matrix.ColumnCount) { return(null); } SquareMatrix square = new SquareMatrix(dimension); for (int r = 0; r < dimension; r++) { for (int c = 0; c < dimension; c++) { if ((r < matrix.RowCount) && (c < matrix.ColumnCount)) { square[r, c] = matrix[r, c]; } } } return(square); }
public static bool IsNearlyEqual(AnyRectangularMatrix x, AnyRectangularMatrix y, EvaluationSettings s) { double m = x.FrobeniusNorm() + y.FrobeniusNorm(); double e = s.AbsolutePrecision + m * s.RelativePrecision; AnyRectangularMatrix D = x - y; return (D.FrobeniusNorm() <= e); }
public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B) { return(IsNearlyEqual(A, B, TargetPrecision)); }
private static void WriteMatrix(AnyRectangularMatrix A) { for (int r = 0; r < A.RowCount; r++) { for (int c = 0; c < A.ColumnCount; c++) { Console.Write("{0} ", A[r, c]); } Console.WriteLine(); } }
public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B, double e) { double nA = MatrixNorm(A); double nB = MatrixNorm(B); for (int r = 0; r < A.RowCount; r++) { for (int c = 0; c < A.ColumnCount; c++) { if (Math.Abs(A[r, c] - B[r, c]) > e * (nA + nB)) return (false); } } return (true); }
public static bool IsNearlyEqual(AnyRectangularMatrix A, AnyRectangularMatrix B) { return (IsNearlyEqual(A, B, TargetPrecision)); }
/* public static bool IsNearlyEigenvalue (ISquareMatrix M, ColumnVector v, double c) { double n = MatrixNorm(M); double ep = e; if (Math.Abs(n / c) < 0.1) ep = e / n; } */ private static double MatrixNorm(AnyRectangularMatrix M) { double n = 0.0; for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { n += Math.Abs(M[r, c]); } } return (n); }