/// <summary>Matrix subtraction.</summary> public static GenericMatrix <T> Subtract(GenericMatrix <T> left, GenericMatrix <T> right) { if (left == null) { throw new ArgumentNullException("left"); } if (right == null) { throw new ArgumentNullException("right"); } if (!DimensionEquals(left, right)) { throw new ArgumentException("Matrix dimension do not match."); } Matrix r = new Matrix(left.m_rows, left.m_columns); for (int i = 0; i < left.m_rows; i++) { for (int j = 0; j < left.m_columns; j++) { r[i][j] = left.m_data[i][j] - right.m_data[i][j]; } } return(r); }
/// <summary>Raise every member of the matrix to the given power.</summary> public static GenericMatrix <T> Pow(GenericMatrix <T> m, double power) { GenericMatrix <T> r = m.Clone(); for (int i = 0; i < r.m_rows; i++) { for (int j = 0; j < r.m_columns; j++) { r.m_data[i][j] = System.Math.Pow(r.m_data[i][j], power); } } return(r); }
//--------------------------------------------- #region Static Methods /// <summary>Returns a matrix filled with random values.</summary> public static GenericMatrix <T> Random(int rows, int columns) { Matrix X = new GenericMatrix <T>(rows, columns); double[][] x = X.baseArray; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { x[i][j] = random.NextDouble(); } } return(X); }
/// <summary>Matrix-matrix multiplication.</summary> public static GenericMatrix <T> Multiply(GenericMatrix <T> left, GenericMatrix <T> right) { if (left == null) { throw new ArgumentNullException("left"); } if (right == null) { throw new ArgumentNullException("right"); } int rows = left.Rows; double[][] data = left.baseArray; if (right.Rows != left.m_columns) { throw new ArgumentException("Matrix dimensions are not valid."); } int columns = right.Columns; GenericMatrix <T> X = new GenericMatrix <T>(rows, columns); double[][] x = X.baseArray; int size = left.m_columns; double[] column = new double[size]; for (int j = 0; j < columns; j++) { for (int k = 0; k < size; k++) { column[k] = right[k, j]; } for (int i = 0; i < rows; i++) { double[] row = data[i]; double s = 0; for (int k = 0; k < size; k++) { s += row[k] * column[k]; } x[i][j] = s; } } return(X); }
/// <summary>Creates a new copy of a matrix object</summary> /// <param name="matrix">The matrix to be copied.</param> public GenericMatrix(GenericMatrix <T> matrix) { this.m_data = new T[matrix.Rows][]; for (int i = 0; i < matrix.Rows; i++) { this.m_data[i] = new T[matrix.Columns]; for (int j = 0; j < matrix.Columns; j++) { this.m_data[i][j] = matrix.m_data[i][j]; } } // this.m_data = (double[][])matrix.m_data.Clone(); this.m_columns = matrix.m_columns; this.m_rows = matrix.m_rows; }
/// <summary>Matrix-scalar multiplication.</summary> public static GenericMatrix <T> Multiply(GenericMatrix <T> left, double right) { if (left == null) { throw new ArgumentNullException("left"); } GenericMatrix <T> r = new GenericMatrix <T>(left.m_rows, left.m_columns); for (int i = 0; i < left.m_rows; i++) { for (int j = 0; j < left.m_columns; j++) { r.m_data[i][j] = left.m_data[i][j] * right; } } return(r); }
/// <summary>Returns a square diagonal matrix of the given dimension.</summary> public static GenericMatrix <T> Diagonal(int dimension, T value) { return(GenericMatrix <T> .Diagonal(dimension, dimension, value)); }
/// <summary> Returns the identity matrix of the given dimension. </summary> public static GenericMatrix <T> Identity(int dimension) { return(GenericMatrix <T> .Diagonal(dimension, 1.0)); }
/// <summary>Returns a square matrix filled with random values.</summary> public static GenericMatrix <T> Random(int dimension) { return(GenericMatrix <T> .Random(dimension, dimension)); }