// numerics public MatrixNd IdentityMatrix(int r, int c) { MatrixNd mat = new MatrixNd(r, c); for (int i = 0; i < r; ++i) { mat[i, i] = 1.0; } return(mat); }
static public MatrixNd operator *(double factor, MatrixNd m) { MatrixNd mat = new MatrixNd(m); for (int i = 0; i < m.length; ++i) { mat[i] *= factor; } return(mat); }
public MatrixNd(MatrixNd mat) { M = mat.Row; N = mat.Col; length = M * N; arr = new double[length]; for (int i = 0; i < length; ++i) { arr[i] = mat.arr[i]; } }
public MatrixNd Transpose() { MatrixNd mat = new MatrixNd(N, M); for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { mat[j, i] = this[i, j]; } } return(mat); }
static public MatrixNd operator /(double factor, MatrixNd m) { MatrixNd mat = new MatrixNd(m); if (factor == 0) { throw new DivideByZeroException(); } for (int i = 0; i < m.length; ++i) { mat[i] /= factor; } return(mat); }
static public MatrixNd operator -(MatrixNd m1, MatrixNd m2) { if (m1.M != m2.M || m1.N != m2.N) { return(null); } MatrixNd m = new MatrixNd(m1); for (int i = 0; i < m1.length; ++i) { m[i] -= m2[i]; } return(m); }
static public MatrixNd operator *(MatrixNd m1, MatrixNd m2) { if (m1.Col != m2.Row) { return(null); } MatrixNd mat = new MatrixNd(m1.Row, m2.Col); for (int i = 0; i < m1.Row; ++i) { for (int j = 0; j < m2.Col; ++j) { for (int k = 0; k < m1.Col; ++k) { mat[i, j] += m1[i, k] * m2[k, j]; } } } return(mat); }