public static Matrixd Identity(int row, int column) { if (row == 0 || column == 0) { throw new ArgumentException("Can not set one or zero dimension matrixd to identity.", nameof(row)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(row, column); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if (i == j) { md.matrixd[i, j] = 1.0; } else { md.matrixd[i, j] = 0.0; } } } return(md); }
public static Matrixd Mul(Matrixd a, Matrixd b) { if (a.columns != b.rows) { throw new ArgumentException("Wrong match, can not multiply two matrixd with different columns or rows.", nameof(a)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.rows, b.columns); double sum = 0.0; for (int i = 0; i < a.rows; i++) { for (int j = 0; j < b.columns; j++) { for (int k = 0; k < a.columns; k++) { sum = sum + a.matrixd[i, k] * b.matrixd[k, j]; // a Mul b = matrix product(a,b) } md.matrixd[i, j] = sum; sum = 0.0; } } return(md); }
// set identity method public static Matrixd Identity(Matrixd a) { if (a.rows == 0 || a.columns == 0) { throw new ArgumentException("Can not set one or zero dimension matrixd to identity.", nameof(a)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.rows, a.columns); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < a.columns; j++) { if (i == j) { md.matrixd[i, j] = 1.0; } else { md.matrixd[i, j] = 0.0; } } } return(md); }
public static Matrixd Div(Matrixd a, Matrixd b) { if (b.rows != b.columns) { throw new ArgumentException("Can not divide the matrixd b because it's non-square matrixd.", nameof(b)); } if (a.columns != b.rows) { throw new ArgumentException("Wrong match, can not divide two matrixd with different columns or rows.", nameof(a)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.rows, a.columns); Matrixd mInverse = Inverse(b); double sum = 0.0; for (int i = 0; i < a.rows; i++) { for (int j = 0; j < mInverse.columns; j++) { for (int k = 0; k < a.columns; k++) { sum = sum + a.matrixd[i, k] * mInverse.matrixd[k, j]; // a Div b = a Mul Inverse(b) } md.matrixd[i, j] = sum; sum = 0.0; } } return(md); }
// divide method public static Matrixd operator /(Matrixd a, Matrixd b) { if (a.rows != b.rows || a.columns != b.columns) { throw new ArgumentException("Can not divide two matrixd with different rows or columns.", nameof(a)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.rows, a.columns); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < a.columns; j++) { md.matrixd[i, j] = a.matrixd[i, j] / b.matrixd[i, j]; } } return(md); }
public static Matrixd operator *(Matrixd a, double b) { if (b.GetType() != typeof(double)) { throw new ArgumentException("Wrong number, the matrixd must multiply double type number.", nameof(b)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.rows, a.columns); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < a.columns; j++) { md.matrixd[i, j] = a.matrixd[i, j] * b; } } return(md); }
// get the transpose method public static Matrixd Transpose(Matrixd a) { if (a.rows == 0 || a.columns == 0) { throw new ArgumentException("Wrong matrixd, can not transpose one or zero dimension matrixd.", nameof(a)); } // Matrixd md = new Matrixd(); Matrixd md = new Matrixd(a.columns, a.rows); // define the transpose matrixd for (int i = 0; i < a.columns; i++) { for (int j = 0; j < a.rows; j++) { md.matrixd[i, j] = a.matrixd[j, i]; } } return(md); }
// get the inverse method public static Matrixd Inverse(Matrixd a) { if (a.rows != a.columns) { throw new ArgumentException("Can not get the inverse of the non-square matrixd.", nameof(a)); } // construct the transform matrix int row = a.rows; int col = a.columns; Matrixd mTrans = new Matrixd(row, 2 * col); Matrixd mTemp = new Matrixd(row, 2 * col); Matrixd md = new Matrixd(row, col); // initialize the transform matrix for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { mTrans.matrixd[i, j] = a.matrixd[i, j]; if (i == j) { mTrans.matrixd[i, j + col] = 1.0; } else { mTrans.matrixd[i, j + row] = 0.0; } } } // process the elementary row transformation int rankFlag = 0; double scaleTemp = 0.0; for (int q = 0; q < col; q++) { // set the elements to ones in row order for every row of the matrixd for (int p = q; p < row; p++) { if (mTrans.matrixd[p, q] != 0.0) { if (mTrans.matrixd[p, q] != 1.0) { scaleTemp = mTrans.matrixd[p, q]; for (int r = q; r < 2 * col; r++) { mTrans.matrixd[p, r] = mTrans.matrixd[p, r] / scaleTemp; } } // reset the scaleTemp scaleTemp = 0.0; } else { rankFlag++; continue; } } // check the rank of the matrixd if (rankFlag == (row - q)) { throw new ArgumentException("The matrixd can not be transposed because of its non-full rank.", nameof(rankFlag)); } // check whether the main element equals zero and change it to one by exchanging the rows if (mTrans.matrixd[q, q] == 0.0) { for (int s = q + 1; s < row; s++) { // check relative column elements for exchange if (mTrans.matrixd[s, q] != 0.0) { // mTemp = mTrans; mTemp.matrixd = (double[, ])Clone(mTrans.matrixd); for (int t = 0; t < 2 * col; t++) { mTrans.matrixd[q, t] = mTemp.matrixd[s, t]; mTrans.matrixd[s, t] = mTemp.matrixd[q, t]; } break; } else { continue; } } // clear the mTemp matrixd buffer mTemp = Zeros(mTemp); } // set the non-main element to zeros in relative column of the matrixd for (int u = q + 1; u < row; u++) { if (mTrans.matrixd[u, q] != 0.0) { for (int v = 0; v < 2 * col; v++) { mTrans.matrixd[u, v] = mTrans.matrixd[u, v] - mTrans.matrixd[q, v]; } } else { continue; } } // reset the rankFlag rankFlag = 0; } // set the non-main element to zeros in right-up area of the matrixd in column order for (int x = col - 1; x > 0; x--) // check the columns from the last column to second column { // check the rows from second-to-last row to first row for (int y = x - 1; y >= 0; y--) { scaleTemp = mTrans.matrixd[y, x] / mTrans.matrixd[x, x]; for (int z = 0; z < 2 * col; z++) // set the every element of the picked row { mTrans.matrixd[y, z] = mTrans.matrixd[y, z] - scaleTemp * mTrans.matrixd[x, z]; } // reset the scaleTemp scaleTemp = 0.0; } } // get the inverse of the original matrixd a for (int m = 0; m < row; m++) { for (int n = 0; n < col; n++) { md.matrixd[m, n] = mTrans.matrixd[m, n + col]; } } return(md); }