| | #region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | #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}]")]
|
| | [DebuggerTypeProxy(typeof(MatrixComplexDebuggerDisplay))]
|
| | public class ComplexMatrix : IMatrix<Complex>
|
| | {
|
| | #region Fields
|
| |
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | protected Complex[] _Data;
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | protected int _RowCount;
|
| |
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | protected int _ColumnCount;
|
| |
|
| | #endregion
|
| |
|
| |
|
| | #region Public Constructors
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| |
|
| | for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++)
|
| | {
|
| | this._Data[i] = Data[i];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| |
|
| | for (int i = 0; i < Math.Min(this._Data.Length, Data.Length); i++)
|
| | {
|
| | this._Data[i] = Data[i];
|
| | }
|
| | }
|
| |
|
| | #endregion
|
| |
|
| |
|
| | #region Public Properties
|
| |
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | internal Complex[] Data
|
| | {
|
| | get { return this._Data; }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | public int RowCount
|
| | {
|
| | get { return _RowCount; }
|
| | set { _RowCount = value; }
|
| | }
|
| |
|
| |
|
| |
|
| | [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
| | public int ColumnCount
|
| | {
|
| | get { return _ColumnCount; }
|
| | set { _ColumnCount = value; }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| | public bool IsSquare
|
| | {
|
| | get
|
| | {
|
| | bool isSquare = false;
|
| | if (this._ColumnCount == this.RowCount) isSquare = true;
|
| | return isSquare;
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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
|
| |
|
| |
|
| | private void CheckMatrixDimensions(ComplexMatrix B)
|
| | {
|
| | if (this._RowCount != B.RowCount || B.ColumnCount != this._ColumnCount)
|
| | {
|
| | throw new System.ArgumentException("Matrix dimensions must agree.");
|
| | }
|
| | }
|
| |
|
| |
|
| | 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
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| | public virtual void MultiplyC(Complex c)
|
| | {
|
| | for (int i = 0; i < this._Data.Length; i++)
|
| | {
|
| | this._Data[i] = c * this._Data[i];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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 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
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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();
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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();
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | #endregion
|
| |
|
| | #region Matrix-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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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<Complex> Members
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | 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;
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| | 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
|
| | }
|
| | } |