public static void Testing() { Matrix<int> matrix1 = new Matrix<int>(5); Matrix<int> matrix2 = new Matrix<int>(5); for (int i = 0; i < matrix1.GetLength(0); i++) { for (int j = 0; j < matrix1.GetLength(1); j++) { matrix1[i, j] = j; } } for (int i = 0; i < matrix1.GetLength(0); i++) { for (int j = 0; j < matrix1.GetLength(1); j++) { matrix2[i, j] = j + 2; } } Console.WriteLine(matrix1); Console.WriteLine(); Console.WriteLine(matrix2); Console.WriteLine(); //testing true operator if (matrix2) { Console.WriteLine("No null elments"); } Console.WriteLine(); //testing + Console.WriteLine(matrix1 + matrix2); Console.WriteLine(); //testing - Console.WriteLine(matrix2 - matrix2); Console.WriteLine(); //testing * matrix2[3, 3] = 12345; Console.WriteLine(matrix1 * matrix2); Console.WriteLine(); }
public DualMatrix(Matrix values, Matrix[] gradients, Matrix[,] hessians) { if (values == null || gradients == null || hessians == null) { throw new ArgumentNullException(); } int rows = values.Rows; int columns = values.Columns; entries = new DualNumber[rows, columns]; int n = 0; if (gradients != null) { n = gradients.Length; for (int i = 0; i < n; i++) { if (gradients[i] == null) { throw new ArgumentNullException("gradients", "The gradients must be fully specified."); } if (gradients[i].Rows != rows || gradients[i].Columns != columns) { throw new ArgumentException("Inconsistent matrix sizes."); } } } if (hessians != null) { if (gradients == null) { throw new ArgumentException("The gradients must be specified if the Hessians are specified."); } if (hessians.GetLength(0) != n || hessians.GetLength(1) != n) { throw new ArgumentException("Inconsistent number of derivatives."); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (hessians[i, j] == null) { throw new ArgumentNullException("hessians", "The Hessians must be fully specified."); } if (hessians[i, j].Rows != rows || hessians[i, j].Columns != columns) { throw new ArgumentException("Inconsistent matrix sizes."); } } } } for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { double value = values[i, j]; Vector gradient = null; if (gradients != null) { double[] a = new double[n]; for (int k = 0; k < n; k++) { a[k] = gradients[k][i, j]; } gradient = new Vector(a); } Matrix hessian = null; if (hessians != null) { double[,] a = new double[n, n]; for (int k = 0; k < n; k++) { for (int l = 0; l < n; l++) { a[k, l] = hessians[k, l][i, j]; } } hessian = new Matrix(a); } entries[i, j] = new DualNumber(value, gradient, hessian); } } }