private void CheckDimensions(BaseMatrix matrixA) { if (matrixA.IsSquare != true) { throw new System.ArgumentException("Matrix A is not a square matrix."); } }
/// <summary>Check if size(this) == size(B) </summary> internal protected void CheckMatrixDimensions(BaseMatrix B) { if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount) { throw new System.ArgumentException("Matrix dimensions must agree."); } }
/// <summary> /// In place matrix subtraction, A=A-B /// </summary> /// <param name="B">The Matrix</param> public void SubtractInplace(BaseMatrix B) { CheckMatrixDimensions(B); double[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i] -= BData[i]; } }
/// <summary> /// Element-by-element multiplication: aij = aij*bij /// </summary> /// <param name="B">The B Matrix.</param> public virtual void ElemntsMult(BaseMatrix B) { CheckMatrixDimensions(B); double[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i] *= BData[i]; } }
/// <summary> /// Sets the matrix to show. /// </summary> /// <param name="matrix">A complex matrix.</param> public void Matrix(ComplexMatrix matrix) { this._ComplexMatrix = matrix; this._IsRealMatrix = false; this._RealMatrix = new Matrix(1); this.ShowMatrix(); }
/// <summary> /// Sets the matrix to show. /// </summary> /// <param name="matrix">A real matrix.</param> public void Matrix(BaseMatrix matrix) { this._RealMatrix = matrix; this._IsRealMatrix = true; this._ComplexMatrix = new ComplexMatrix(1); this.ShowMatrix(); }
/// <summary> /// In place addition A=A+B /// </summary> /// <param name="B">The Matrix</param> public void AddInplace(BaseMatrix B) { base.CheckMatrixDimensions(B); double[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i] += BData[i]; } }
private void CheckDimensions(BaseMatrix matrixA, BaseMatrix matrixB) { if (matrixA.IsSquare != true) { throw new System.ArgumentException("Matrix A is not a square matrix."); } if (matrixA.RowCount != matrixB.RowCount) { throw new System.ArgumentException("Matrix dimensions are not valid."); } }
/// <summary> /// Matrix subtraction, C=A-B /// </summary> /// <param name="B">The Matrix</param> /// <returns>C=A-B</returns> public Matrix Subtract(BaseMatrix B) { CheckMatrixDimensions(B); Matrix C = new Matrix(this._RowCount, this._ColumnCount); double[] BData = B.Data; double[] dataC = C.Data; for (int i = 0; i < this._Data.Length; i++) { dataC[i] = this._Data[i] - BData[i]; } return(C); }
private void CheckDimensions(BaseMatrix matrixA, Vector vectorB) { if (matrixA.IsSquare != true) { throw new System.ArgumentException("Matrix A is not a square matrix."); } if (vectorB.Type != VectorType.Column) { throw new System.ArgumentException("The vector should be a column vector."); } if (matrixA.RowCount != vectorB.Length) { throw new System.ArgumentException("Matrix dimensions are not valid."); } }
/// <summary> /// Matrix-Matrix multiplication, C=A*B /// </summary> /// <param name="B">The matrix.</param> /// <returns>C=A*B</returns> public Matrix Multiply(BaseMatrix B) { if (B.RowCount != this.ColumnCount) { throw new System.ArgumentException("Matrix dimensions are not valid."); } Matrix C = new Matrix(this.RowCount, B.ColumnCount); double[] AData = this.Data; double[] BData = B.Data; double[] CData = C.Data; int ARows = this.RowCount; int AColumns = this.ColumnCount; int BRows = B.RowCount; int BColumns = B.ColumnCount; double Sum = 0.0; int indexBJ; int indexAJ; for (int j = 0; j < BColumns; j++) { indexBJ = j * BRows; indexAJ = j * ARows; for (int i = 0; i < ARows; i++) { Sum = 0.0; for (int k = 0; k < AColumns; k++) { Sum += AData[i + k * ARows] * BData[k + indexBJ]; } CData[i + indexAJ] = Sum; } } return(C); //To reading every time elements from array , why we are taking some group of element i.e. Block size, then no need to read every element. A groups of element will be on catche and we can do fast as given above algo. This algorithm called " Block Algorithm". This Block algorithm can be applied many place where this type of situation will come. //Block Algorithm for Matrix Multiplication: //Code: C // #define n 1000 //int main() //{ // int a[n][n],b[n][n],c[n][n]; // c[0][0]=0; // for( i=0;i<n;++i) // { // for(j=0;j<n;++j) // { // for(k=0;k<n;++k) // { // c[i][j] = c[i][j] + a[i][k] * b[k][j] // } // } // } // return 0; //} //To reading every time elements from array , why we are taking some group of element i.e. Block size, then no need to read every element. A groups of element will be on catche and we can do fast as given above algo. This algorithm called " Block Algorithm". This Block algorithm can be applied many place where this type of situation will come. //Block Algorithm for Matrix Multiplication: //Code: C //#define n 1000 //#define BlockSize 100 //int main() //{ // int a[n][n],b[n][n],c[n][n]; // c[0][0]=0; // for( i1=0;i1<(n/BlockSize);++i1) // { // for(j1=0;j1<(n/BlockSize);++j1) // { // for(k1=0;k1<(n/BlockSize);++k1) // { // for(i=i1=0;i<min(i1+BlockSize-1);++i) // { // for(j=j1=0;j<min(j1+BlockSize-1);++j) // { // for(k=k1;k<min(k1+BlockSize-1);++k) // { // c[i][j] = c[i][j] + a[i][k] * b[k][j] // } // } // } // } // } // } // return 0; //} }
public MatrixDebuggerDisplay(BaseMatrix matrix) { this.MeMatrix = matrix; }