/// <summary>Check if size(A) == size(B) *</summary> private void CheckMatrixDimensions(NNMatrix B) { if (B.m != m || B.n != n) { throw new System.ArgumentException("GeneralMatrix dimensions must agree."); } }
/// <summary>Linear algebraic matrix multiplication, A * B</summary> /// <param name="B"> another matrix /// </param> /// <returns> Matrix product, A * B /// </returns> /// <exception cref="System.ArgumentException"> Matrix inner dimensions must agree. /// </exception> public virtual NNMatrix Multiply(NNMatrix B) { if (B.n != m) { throw new System.ArgumentException("GeneralMatrix inner dimensions must agree."); } NNMatrix X = new NNMatrix(this.n, B.m); double[][] C = X.Array; double[] Bcolj = new double[m]; for (int j = 0; j < B.m; j++) { for (int k = 0; k < m; k++) { Bcolj[k] = B.A[k][j]; } for (int i = 0; i < n; i++) { double[] Arowi = A[i]; double s = 0; for (int k = 0; k < m; k++) { s += Arowi[k] * Bcolj[k]; } C[i][j] = s; } } return(X); }
/// <summary>Element-by-element left division in place, A = A.\B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.\B /// </returns> public virtual NNMatrix ArrayLeftDivideEquals(NNMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = B.A[i][j] / A[i][j]; } } return(this); }
/// <summary>Element-by-element multiplication in place, A = A.*B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.*B /// </returns> public virtual NNMatrix ArrayMultiplyEquals(NNMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = A[i][j] * B.A[i][j]; } } return(this); }
/// <summary>A = A - B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A - B /// </returns> public virtual NNMatrix SubtractEquals(NNMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = A[i][j] - B.A[i][j]; } } return(this); }
public virtual NNMatrix Transpose() { NNMatrix X = new NNMatrix(this.m, this.n); for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { X[j, i] = this[i, j]; } } return(X); }
/// <summary>Generate identity matrix</summary> /// <param name="m"> Number of rows. /// </param> /// <param name="n"> Number of colums. /// </param> /// <returns> An m-by-n matrix with ones on the diagonal and zeros elsewhere. /// </returns> public static NNMatrix Identity(int m, int n) { NNMatrix A = new NNMatrix(m, n); double[][] X = A.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { X[i][j] = (i == j ? 1.0 : 0.0); } } return(A); }
/// <summary>Multiply a matrix by a scalar, C = s*A</summary> /// <param name="s"> scalar /// </param> /// <returns> s*A /// </returns> public virtual NNMatrix Multiply(double s) { NNMatrix X = new NNMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = s * A[i][j]; } } return(X); }
/// <summary>Unary minus</summary> /// <returns> -A /// </returns> public virtual NNMatrix UnaryMinus() { NNMatrix X = new NNMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { C[i][j] = -A[i][j]; } } return(X); }
/// <summary>Make a deep copy of a matrix</summary> public virtual NNMatrix Copy() { NNMatrix X = new NNMatrix(n, m); double[][] C = X.Array; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { C[i][j] = A[i][j]; } } return(X); }
/// <summary>Element-by-element left division, C = A.\B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.\B /// </returns> public virtual NNMatrix ArrayLeftDivide(NNMatrix B) { CheckMatrixDimensions(B); NNMatrix X = new NNMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = B.A[i][j] / A[i][j]; } } return(X); }
/// <summary>C = A - B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A - B /// </returns> public virtual NNMatrix Subtract(NNMatrix B) { CheckMatrixDimensions(B); NNMatrix X = new NNMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = A[i][j] - B.A[i][j]; } } return(X); }
/// <summary>Generate matrix with random elements</summary> /// <param name="m"> Number of rows. /// </param> /// <param name="n"> Number of colums. /// </param> /// <returns> An m-by-n matrix with uniformly distributed random elements. /// </returns> public static NNMatrix Random(int m, int n) { System.Random random = new System.Random(); NNMatrix A = new NNMatrix(m, n); double[][] X = A.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { X[i][j] = random.NextDouble(); } } return(A); }
public bool Equals(NNMatrix B) { if ((this.m != B.m) || (this.n != B.n)) { return(false); } for (int i = 0; i < this.n; i++) { for (int j = 0; j < this.m; j++) { if (this[i, j] != B[i, j]) { return(false); } } } return(true); }
/// <summary>Construct a matrix from a copy of a 2-D array.</summary> /// <param name="A"> Two-dimensional array of doubles. /// </param> /// <exception cref="System.ArgumentException"> All rows must have the same length /// </exception> public static NNMatrix Create(double[][] A) { int n = A.Length; int m = A[0].Length; NNMatrix X = new NNMatrix(n, m); double[][] C = X.Array; for (int i = 0; i < n; i++) { if (A[i].Length != m) { throw new System.ArgumentException("All rows must have the same length."); } for (int j = 0; j < m; j++) { C[i][j] = A[i][j]; } } return(X); }