#region Copyright © 2009, De Santiago-Castillo JA. All rights reserved. //Copyright © 2009 Jose Antonio De Santiago-Castillo //E-mail:JAntonioDeSantiago@gmail.com //Web: www.DotNumerics.com // #endregion using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; using DotNumerics.FortranLibrary; namespace DotNumerics.LinearAlgebra { //[DebuggerDisplay("[{RowCount},{ColumnCount}]", Name = "MatrixComplex")] /// /// Represents a Complex Matrix. /// [DebuggerDisplay("[{RowCount},{ColumnCount}]")] [DebuggerTypeProxy(typeof(MatrixComplexDebuggerDisplay))] public class ComplexMatrix : IMatrix { #region Fields /// /// Los datos de la matriz, los datos se almacenan en un un array unidimensional, /// Los elementos se almacenan por columnas, esto para que sean compatible con los Arrays de Fortran /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected Complex[] _Data; /// /// El numero de renglones /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected int _RowCount; /// /// El numero de columnas /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected int _ColumnCount; #endregion #region Public Constructors /// /// Initializes a new instance of the MatrixComplex class of the given size. /// /// Number of rows. /// Number of columns. public ComplexMatrix(int rows, int columns) { if (rows < 1) throw new System.ArgumentException("rows < 1"); if (columns < 1) throw new System.ArgumentException("columns < 1"); this._Data = new Complex[rows * columns]; this._RowCount = rows; this._ColumnCount = columns; } /// /// Initializes a new instance of the MatrixComplex class of the given size using a array /// /// Number of rows. /// Number of columns. /// The data internal ComplexMatrix(int rows, int columns, Complex[] Data) { if (rows < 1) throw new System.ArgumentException("rows < 1"); if (columns < 1) throw new System.ArgumentException("columns < 1"); this._Data = new Complex[rows * columns]; this._RowCount = rows; this._ColumnCount = columns; //Si incluye la posibilidad de que los datos tengan menos valores que la matriz a crear for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++) { this._Data[i] = Data[i]; } } /// /// Initializes a new instance of the MatrixComplex class of the given size. /// /// Size public ComplexMatrix(int size) { if (size < 1) throw new System.ArgumentException("size < 1"); this._Data = new Complex[size * size]; this._RowCount = size; this._ColumnCount = size; } /// /// Initializes a new instance of the MatrixComplex class of the given size using a array /// /// Size /// The data internal ComplexMatrix(int size, Complex[] Data) { if (size < 1) throw new System.ArgumentException("size < 1"); this._Data = new Complex[size * size]; this._RowCount = size; this._ColumnCount = size; //Si incluye la posibilidad de que los datos tengan menos valores que la matriz a crear for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++) { this._Data[i] = Data[i]; } } #endregion #region Public Properties /// /// Los datos de la matriz /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] internal Complex[] Data { get { return this._Data; } } /// /// Returns the number of rows. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] public int RowCount { get { return _RowCount; } set { _RowCount = value; } } /// /// Returns the number of columns. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] public int ColumnCount { get { return _ColumnCount; } set { _ColumnCount = value; } } /// /// Gets a value indicating if the matrix is square. /// public bool IsSquare { get { bool isSquare = false; if (this._ColumnCount == this.RowCount) isSquare = true; return isSquare; } } /// /// Returns the value of a element of the matrix. /// /// The row value (zero-based). /// The column value (zero-based). /// The matrix value at (row, column). public virtual Complex this[int row, int column] { get { if (column >= this._ColumnCount) { throw new ArgumentException("Index was outside the bounds of the matrix."); } return this._Data[row + column * this._RowCount]; } set { if (column >= this._ColumnCount) { throw new ArgumentException("Index was outside the bounds of the matrix."); } this._Data[row + column * this._RowCount] = value; } } #endregion #region Private Methods /// Check if size(this) == size(B) private void CheckMatrixDimensions(ComplexMatrix B) { if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount) { throw new System.ArgumentException("Matrix dimensions must agree."); } } /// Check if size(this) == size(B) private void CheckMatrixDimensions(Matrix B) { if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount) { throw new System.ArgumentException("Matrix dimensions must agree."); } } #endregion // Private Methods #region Elementary linear operations ///// ///// aij=Math.Abs(aij) ///// //public virtual void ElementsAbs() //{ // for (int i = 0; i < this.MeData.Length; i++) // { // this.MeData[i] =Complex. Math.Abs(this.MeData[i]); // } //} ///// ///// Element-by-element division: aij = aij/bij ///// ///// //public virtual void ElemntsDiv(MatrixComplex B) //{ // CheckMatrixDimensions(B); // Complex[] BData = B.Data; // for (int i = 0; i < this.MeData.Length; i++) // { // this.MeData[i] /= BData[i]; // } //} /// /// Element-by-element multiplication: aij = aij*bij /// /// The B MatrixComplex public virtual void ElemntsMult(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]; } } /// /// In place addition A=A+B /// /// The B MatrixComplex 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]; } } /// /// In place scalar-matrix multiplication, A=s*A /// /// The scalar s. public virtual void Multiply(double s) { for (int i = 0; i < this._Data.Length; i++) { this._Data[i].Real = s * this._Data[i].Real; this._Data[i].Imaginary = s * this._Data[i].Imaginary; } } /// /// In place scalar-matrix multiplication, A=c*A /// /// The scalar s. public virtual void MultiplyC(Complex c) { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] = c * this._Data[i]; } } /// /// In place matrix subtraction, A=A-B. /// /// The B MatrixComplex. 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; } } /// /// In place unary minus -A. /// public virtual void UnaryMinus() { for (int i = 0; i < this._Data.Length; i++) { this._Data[i].Real = -this._Data[i].Real; this._Data[i].Imaginary = -this._Data[i].Imaginary; } } #endregion #region Methods /// /// Gets the column vectors of this matrix. /// /// The columns vectors. public ComplexVector[] GetColumnVectors() { ComplexVector[] columnVects = new ComplexVector[this._ColumnCount]; Complex[] VectData; for (int j = 0; j < this._ColumnCount; j++) { columnVects[j] = new ComplexVector(VectorType.Column, this._RowCount); VectData = columnVects[j].Data; for (int i = 0; i < VectData.Length; i++) { VectData[i] = this._Data[i + j * this._RowCount]; } } return columnVects; } /// /// Gets the row vectors of this matrix. /// /// The row vectors. public ComplexVector[] GetRowVectors() { ComplexVector[] rowVects = new ComplexVector[this.RowCount]; Complex[] VectData; for (int i = 0; i < this._RowCount; i++) { rowVects[i] = new ComplexVector(VectorType.Row, this._ColumnCount); VectData = rowVects[i].Data; for (int j = 0; j < VectData.Length; j++) { VectData[j] = this._Data[i + j * this._RowCount]; } } return rowVects; } /// /// Gets a matrix that contains the real part of this matrix. /// /// A matrix that contains the real part of this matrix. public Matrix GetReal() { Matrix RealMatrix = new Matrix(this.RowCount, this.ColumnCount); double[] RealData = RealMatrix.Data; for (int i = 0; i < this._Data.Length; i++) { RealData[i] = this._Data[i].Real; } return RealMatrix; } /// /// Gets a matrix that contains the imaginary part of this matrix. /// /// A matrix that contains the imaginary part of this matrix. public Matrix GetImag() { Matrix ImagMatrix = new Matrix(this.RowCount, this.ColumnCount); double[] ImagData = ImagMatrix.Data; for (int i = 0; i < this._Data.Length; i++) { ImagData[i] = this._Data[i].Imaginary; } return ImagMatrix; } /// /// Sets the real part of the elements of this matrix equal to the elemnets of a real matrix. /// /// A matrix that contains the values of the real part. public void SetReal(Matrix RM) { this.CheckMatrixDimensions(RM); double[] RealData = RM.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i].Real = RealData[i]; } } /// /// Sets the imaginary part of the elements of this matrix equal to the elemnets of a real matrix. /// /// A matrix that contains the values of the imaginary part. public void SetImag(Matrix IM) { this.CheckMatrixDimensions(IM); double[] ImagData = IM.Data; for (int i = 0; i < this._Data.Length; i++) { this._Data[i].Imaginary = ImagData[i]; } } /// /// Returns the string of the matrix. /// /// The string of the matrix. public string MatrixToString() { using (StringWriter writer = new StringWriter()) { for (int i = 0; i < this._RowCount; i++) { for (int j = 0; j < this._ColumnCount; j++) writer.Write(this[i, j] + ", "); writer.WriteLine(); } return writer.ToString(); } } /// /// Returns the string of the matrix. /// /// A numeric format string. /// The string of the matrix. public string MatrixToString(string format) { using (StringWriter writer = new StringWriter()) { for (int i = 0; i < this._RowCount; i++) { for (int j = 0; j < this._ColumnCount; j++) writer.Write(this[i, j].ToString(format) + ", "); writer.WriteLine(); } return writer.ToString(); } } ///// ///// maximum column sum. ///// ///// maximum column sum. //public double Norm1() //{ // double n = 0.0; // double ColSum = 0.0; // int NRows = this.MeRowCount; // for (int j = 0; j < this.MeColumnCount; j++) // { // ColSum = 0.0; // for (int i = 0; i < this.MeRowCount; i++) // { // ColSum += Math.Abs(this.MeData[i + j * NRows]); // } // n = Math.Max(n, ColSum); // } // return n; //} ///// ///// ///// ///// //public double InfinityNorm() //{ // double n = 0.0; // double RowSum = 0.0; // int NRows = this.MeRowCount; // for (int i = 0; i < this.MeRowCount; i++) // { // RowSum = 0.0; // for (int j = 0; j < this.MeColumnCount; j++) // { // RowSum += Math.Abs(this.MeData[i + j * NRows]); // } // n = Math.Max(n, RowSum); // } // return n; //} ///// Frobenius norm ///// Sqrt of sum of squares of all elements. //public double FrobeniusNorm() //{ // double n=0; // for(int i=0; isqrt(a^2 + b^2) without under/overflow. //private double Hypot(double a, double b) //{ // double r; // if (Math.Abs(a) > Math.Abs(b)) // { // r = b/a; // r = Math.Abs(a) * Math.Sqrt(1 + r * r); // } // else if (b != 0) // { // r = a/b; // r = Math.Abs(b) * Math.Sqrt(1 + r * r); // } // else // { // r = 0.0; // } // return r; //} #endregion #region Matrix-Matrix Multiplication /// /// Matrix multiplication. /// /// The left side matrix of the multiplication operator. /// The right side matrix of the multiplication operator. /// A matrix that represents the result of the matrix multiplication. public static ComplexMatrix operator *(ComplexMatrix 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); Complex[] 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; } /// /// Matrix multiplication. /// /// The left side matrix of the multiplication operator. /// The right side matrix of the multiplication operator. /// A matrix that represents the result of the matrix multiplication. 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; } /// complex-Matrix multiplication. /// The left side scalar of the multiplication operator. /// The right side matrix of the multiplication operator. /// A matrix that represents the result of the multiplication. 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; } #endregion #region Matrix-Matrix Addition /// /// Matrix addition. /// /// The left side matrix of the addition operator. /// The right side matrix of the addition operator. /// A matrix that represents the result of the matrix addition. 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; } #endregion #region Matrix-Matrix Subtraction ///// Matrix Subtraction /// /// Matrix subtraction. /// /// The left side matrix of the subtraction operator. /// The right side matrix of the subtraction operator. /// A matrix that represents the result of the matrix subtraction. 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; } #endregion #region IMatrix Members /// /// Copy all elements of this matrix to a rectangular 2D array. /// /// A rectangular 2D array. public Complex[,] CopyToArray() { Complex[,] matrixData = new Complex[this._RowCount, this._ColumnCount]; for (int j = 0; j < this._ColumnCount; j++) { for (int i = 0; i < this._RowCount; i++) { matrixData[i, j] = this._Data[i + j * this._RowCount]; } } return matrixData; } /// /// Copy all elements of this matrix to a jagged array. /// /// A jagged array. public Complex[][] CopyToJaggedArray() { Complex[][] newData = new Complex[this._RowCount][]; for (int i = 0; i < this._RowCount; i++) { Complex[] row = new Complex[this._ColumnCount]; for (int j = 0; j < this._ColumnCount; j++) { row[j] = this._Data[i + j * this._RowCount]; } newData[i] = row; } return newData; } #endregion } }