/// <summary>Check if size(this) == size(B) </summary> private void CheckMatrixDimensions(ComplexMatrix B) { if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount) { throw new System.ArgumentException("Matrix dimensions must agree."); } }
private void CheckDimensions(ComplexMatrix matrixA) { if (matrixA.IsSquare != true) { throw new System.ArgumentException("Matrix A is not a square matrix."); } }
/// <summary> /// In place addition A=A+B /// </summary> /// <param name="B">The B MatrixComplex</param> public virtual void Add(ComplexMatrix B) { CheckMatrixDimensions(B); Complex[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[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 matrix subtraction, A=A-B. /// </summary> /// <param name="B">The B MatrixComplex.</param> public virtual void Subtract(ComplexMatrix B) { CheckMatrixDimensions(B); Complex[] BData = B.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i].Real = this._Data[i].Real - BData[i].Real; this._Data[i].Imaginary = this._Data[i].Imaginary - BData[i].Imaginary; } }
public ComplexMatrix CopyToComplex() { ComplexMatrix complexMatrix = new ComplexMatrix(this._RowCount, this._ColumnCount); Complex[] data = complexMatrix.Data; for (int i = 0; i < this._Data.Length; i++) { data[i].Real = this._Data[i]; } return(complexMatrix); }
/// <summary>complex-Matrix multiplication.</summary> /// <param name="c"> The left side scalar of the multiplication operator.</param> /// <param name="B">The right side matrix of the multiplication operator.</param> /// <returns>A matrix that represents the result of the multiplication.</returns> public static ComplexMatrix operator *(Complex c, ComplexMatrix B) { ComplexMatrix C = new ComplexMatrix(B.RowCount, B.ColumnCount); Complex[] BData = B.Data; Complex[] CData = C.Data; for (int i = 0; i < BData.Length; i++) { CData[i] = c * BData[i]; } return(C); }
///// <summary>Matrix Subtraction</summary> /// <summary> /// Matrix subtraction. /// </summary> /// <param name="A"> The left side matrix of the subtraction operator.</param> /// <param name="B">The right side matrix of the subtraction operator.</param> /// <returns>A matrix that represents the result of the matrix subtraction.</returns> public static ComplexMatrix operator -(ComplexMatrix A, ComplexMatrix B) { if (B.RowCount != A.RowCount || B.ColumnCount != A.ColumnCount) { throw new System.ArgumentException("Matrix dimensions are not valid."); } ComplexMatrix C = new ComplexMatrix(A.RowCount, A.ColumnCount); Complex[] AData = A.Data; Complex[] BData = B.Data; Complex[] CData = C.Data; for (int i = 0; i < AData.Length; i++) { CData[i] = AData[i] - BData[i]; } return(C); }
/// <summary> /// Matrix multiplication. /// </summary> /// <param name="A"> The left side matrix of the multiplication operator.</param> /// <param name="B">The right side matrix of the multiplication operator.</param> /// <returns>A matrix that represents the result of the matrix multiplication.</returns> public static ComplexMatrix operator *(BaseMatrix A, ComplexMatrix B) { if (B.RowCount != A.ColumnCount) { throw new System.ArgumentException("Matrix dimensions are not valid."); } ComplexMatrix C = new ComplexMatrix(A.RowCount, B.ColumnCount); double[] AData = A.Data; Complex[] BData = B.Data; Complex[] CData = C.Data; int ARows = A.RowCount; int AColumns = A.ColumnCount; int BRows = B.RowCount; int BColumns = B.ColumnCount; Complex Sum = new Complex(0.0, 0.0); for (int j = 0; j < BColumns; j++) { for (int i = 0; i < ARows; i++) { Sum.Imaginary = 0.0; Sum.Real = 0.0; for (int k = 0; k < AColumns; k++) { Sum += AData[i + k * ARows] * BData[k + j * BRows]; } CData[i + j * ARows] = Sum; } } return(C); }
/// <summary> /// Computes the eigenvalues and eigenvectors for an complex general matrix A. /// </summary> /// <param name="A">The complex general matrix A.</param> /// <param name="EigenVectors">The eigenvectors.</param> /// <returns>The eigenvalues.</returns> public ComplexMatrix GetEigenvalues(ComplexMatrix A, out ComplexMatrix EigenVectors) { //Fortran Ejemplo //CG(NM,N,AR,AI,WR,WI,1,ZR,ZI,SCALE,ORTR,ORTI,ERROR) //C //C THIS DRIVER TESTS EISPACK FOR THE CLASS OF COMPLEX GENERAL //C MATRICES SUMMARIZING THE FIGURES OF MERIT FOR ALL PATHS. //C //C THIS DRIVER IS CATALOGUED AS EISPDRV4(CGSUMARY). //C //C THE DIMENSION OF AR,AI,ZR,ZI,ASAVER,ASAVEI,RM1, AND RM2 SHOULD //C BE NM BY NM. //C THE DIMENSION OF WR,WI,WR1,WI1,SELECT,SLHOLD,INT,SCALE,ORTR,ORTI, //C RV1 AND RV2 SHOULD BE NM. //C THE DIMENSION OF ARHOLD AND AIHOLD SHOULD BE NM BY NM. //C HERE NM = 20. if (this._cg == null) { this._cg = new CG(); } this.CheckDimensions(A); Matrix AReal = A.GetReal(); double[] ARealData = AReal.Data; Matrix AImag = A.GetImag(); double[] AImagData = AImag.Data; ComplexMatrix EigenVals = new ComplexMatrix(A.RowCount, 1); Matrix RealEigenVals = new Matrix(A.RowCount, 1); double[] RealEigenValsData = RealEigenVals.Data; Matrix ImagEigenVals = new Matrix(A.RowCount, 1); double[] ImagEigenValsData = ImagEigenVals.Data; EigenVectors = new ComplexMatrix(A.RowCount, A.ColumnCount); Matrix RealEigVect = new Matrix(A.RowCount); double[] RealEigVectData = RealEigVect.Data; Matrix ImagEigVect = new Matrix(A.RowCount); double[] ImagEigVectData = ImagEigVect.Data; double[] SCALE = new double[A.RowCount]; double[] ORTR = new double[A.RowCount]; double[] ORTI = new double[A.RowCount]; int Info = 0; int matz = 1; //Se calculan los eigenvalores y los eigenvectores _cg.Run(A.RowCount, A.RowCount, ref ARealData, 0, ref AImagData, 0, ref RealEigenValsData, 0, ref ImagEigenValsData, 0, matz, ref RealEigVectData, 0, ref ImagEigVectData, 0, ref SCALE, 0, ref ORTR, 0, ref ORTI, 0, ref Info); #region Error /// is set to /// zero for normal return, /// j if the limit of 30*n iterations is exhausted /// while the j-th eigenvalue is being sought. if (Info != 0) { throw new ArgumentException("The limit of 30*n iterations is exhausted"); } #endregion EigenVals.SetReal(RealEigenVals); EigenVals.SetImag(ImagEigenVals); EigenVectors.SetReal(RealEigVect); EigenVectors.SetImag(ImagEigVect); return(EigenVals); }
/// <summary> /// Computes the eigenvalues for an N-by-N real nonsymmetric matrix A. /// </summary> /// <param name="A">N-by-N real nonsymmetric matrix A.</param> /// <returns>The eigenvalues.</returns> public ComplexMatrix GetEigenvalues(Matrix A) { if (this._dgeev == null) { this._dgeev = new DGEEV(); } this.CheckDimensions(A); Matrix ACopy = A.Clone(); double[] ACopyData = ACopy.Data; Matrix RealEVectors = new Matrix(1, 1); double[] EigenVectsData = RealEVectors.Data; ComplexMatrix EigenVals = new ComplexMatrix(A.RowCount, 1); double[] REigVal = new double[A.RowCount]; double[] IEigVal = new double[A.RowCount]; //double[] EigenValsData = EigenVals.Data; int Info = 0; double[] VL = new double[A.RowCount]; double[] Work = new double[1]; int LWork = -1; //Calculamos LWORK _dgeev.Run("N", "N", A.RowCount, ref ACopyData, 0, ACopy.RowCount, ref REigVal, 0, ref IEigVal, 0, ref VL, 0, 1, ref EigenVectsData, 0, A.RowCount, ref Work, 0, LWork, ref Info); LWork = Convert.ToInt32(Work[0]); if (LWork > 0) { Work = new double[LWork]; _dgeev.Run("N", "N", A.RowCount, ref ACopyData, 0, ACopy.RowCount, ref REigVal, 0, ref IEigVal, 0, ref VL, 0, 1, ref EigenVectsData, 0, A.RowCount, ref Work, 0, LWork, ref Info); } else { //Error } #region Error //= 0: successful exit //.LT. 0: if INFO = -i, the i-th argument had an illegal value. //.GT. 0: if INFO = i, the QR algorithm failed to compute all the // eigenvalues, and no eigenvectors have been computed; // elements i+1:N of WR and WI contain eigenvalues which // have converged. if (Info < 0) { string infoSTg = Math.Abs(Info).ToString(); throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value"); } else if (Info > 0) { string infoSTg = Math.Abs(Info).ToString(); throw new Exception("The QR algorithm failed to compute all the eigenvalues."); } #endregion for (int i = 0; i < EigenVals.RowCount; i++) { EigenVals[i, 0] = new Complex(REigVal[i], IEigVal[i]); } return(EigenVals); }
public MatrixComplexDebuggerDisplay(ComplexMatrix matrix) { this.MeMatrix = matrix; }
/// <summary> /// Remove the matrix. /// </summary> public void RemoveMatrix() { this._ComplexMatrix = new ComplexMatrix(1); this._ComplexMatrix = new ComplexMatrix(1); this.dataGridView1.DataSource = null; }