#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 { /// /// Represents a Complex Vector. /// [DebuggerDisplay(": {Type} , Length : {Length}", Name = "vector")] [DebuggerTypeProxy(typeof(VectorComplexDebuggerDisplay))] public class ComplexVector { #region Fields /// /// Los datos del vector /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected Complex[] _Data; /// /// El tipo de vector. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected VectorType _Type = VectorType.Column; #endregion #region Constructor /// /// Initializes a new instance of the VectorComplex class of the given size. /// /// The vector length public ComplexVector(int length) : this(VectorType.Column, length) { } /// /// Initializes a new instance of the Vector class of the given size and type. /// /// The vector type /// length">The vector length public ComplexVector(VectorType type, int length) { if (length < 1) throw new System.ArgumentException("length < 1"); this._Type = type; this._Data = new Complex[length]; } /// /// Initializes a new instance of the Vector class that contains elements /// copied from the specified array. /// /// The array whose elements are copied to the vector. public ComplexVector(Complex[] data) : this(VectorType.Column, data) { } /// /// Initializes a new instance of the Vector class that contains elements /// copied from the specified array. /// /// The vector type /// The array whose elements are copied to the vector. public ComplexVector(VectorType type, Complex[] data) { if (data.Length < 1) throw new System.ArgumentException("data.Length < 1"); this._Type = type; this._Data = new Complex[data.Length]; data.CopyTo(this._Data, 0); //for (int i = 0; i < data.Length; i++) //{ // this.MeData[i] = data[i]; //} } #endregion #region Public Properties /// /// Los datos del vector /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] internal Complex[] Data { get { return this._Data; } } /// /// Returns the number of rows. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] public int Length { get { return this._Data.Length; } } public VectorType Type { get { return this._Type; } set { this._Type = value; } } /// /// Gets or sets the element at the specified index. /// /// The zero-based index of the element to get or set. /// The element at the specified index. public virtual Complex this[int index] { get { return this._Data[index]; } set { this._Data[index] = value; } } #endregion #region Operators /// /// Vector addition. /// /// The left side vector of the addition operator. /// The right side vector of the addition operator. /// A vector that represents the result of the addition. public static ComplexVector operator +(ComplexVector A, ComplexVector B) { return A.Add(B); } /// /// Unary minus. /// /// The vector. /// Vector r[i] = -this[i] public static ComplexVector operator -(ComplexVector v) { return v.UnaryMinus(); } /// /// Vector subtraction. /// /// The left side vector of the subtraction operator. /// The right side vector of the subtraction operator. /// A vector that represents the result of the vector subtraction. public static ComplexVector operator -(ComplexVector A, ComplexVector B) { return A.Subtract(B); } /// /// Scalar-Vector multiplication. /// /// The left side complex number of the multiplication operator. /// The right side vector of the multiplication operator. /// A vector that represents the result of the multiplication. public static ComplexVector operator *(Complex c, ComplexVector A) { return A.Multiply(c); } /// /// Vector-Scalar multiplication. /// /// The left side vector of the multiplication operator. /// The right side complex number of the multiplication operator. /// A vector that represents the result of the multiplication. public static ComplexVector operator *(ComplexVector A, Complex c) { return A.Multiply(c); } ///// Vector multiplication. //public static Complex operator *(VectorComplex A, VectorComplex B) //{ // if (A.Type != VectorType.Row || B.Type != VectorType.Column || B.Length != A.Length) // { // throw new System.ArgumentException("Vector dimensions or type are not valid."); // } // Complex C = new Complex(0.0, 0.0); // Complex[] AData = A.Data; // Complex[] BData = B.Data; // for (int i = 0; i < AData.Length; i++) // { // C += AData[i] * BData[i]; // } // return C; //} ///// ///// The dot product ///// ///// ///// The dot product of A. //public static double DotProduct(Vector A ) //{ // double C = 0.0; // double[] AData = A.Data; // for (int i = 0; i < AData.Length; i++) // { // C += AData[i] * AData[i]; // } // return C; //} /// /// Transposed vector. /// /// public ComplexVector Transpose() { ComplexVector AT = new ComplexVector(this._Data); if (this._Type == VectorType.Column) AT.Type = VectorType.Row; else AT.Type = VectorType.Column; return AT; } ///// Matrix- Vector multiplication. //public static MatrixComplex operator *(MatrixComplex A, VectorComplex B) //{ // int BRows; // int BColumns; // if (B.Type == VectorType.Column) // { // BColumns = 1; // BRows = B.Length; // } // else // { // BColumns = B.Length; // BRows = 1; // } // if (BRows != A.Columns) // { // throw new System.ArgumentException("Matrix dimensions are not valid."); // } // MatrixComplex C = new MatrixComplex(A.Rows, BColumns); // Complex[] AData = A.Data; // Complex[] BData = B.Data; // Complex[] CData = C.Data; // int ARows = A.Rows; // int AColumns = A.Columns; // Complex Sum; // for (int j = 0; j < BColumns; j++) // { // for (int i = 0; i < ARows; i++) // { // Sum = new Complex(0.0, 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- Vector multiplication. //public static MatrixComplex operator *(VectorComplex A, MatrixComplex B) //{ // int ARows; // int AColumns; // if (A.Type == VectorType.Column) // { // AColumns = 1; // ARows = A.Length; // } // else // { // AColumns = A.Length; // ARows = 1; // } // if (B.Rows != AColumns) // { // throw new System.ArgumentException("Matrix dimensions are not valid."); // } // MatrixComplex C = new MatrixComplex(ARows, B.Columns); // Complex[] AData = A.Data; // Complex[] BData = B.Data; // Complex[] CData = C.Data; // int BRows = B.Rows; // int BColumns = B.Columns; // Complex Sum; // for (int j = 0; j < BColumns; j++) // { // for (int i = 0; i < ARows; i++) // { // Sum = new Complex(0.0, 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; //} #endregion /// /// Implicit Vector to Matrix conversion. /// /// The Vector /// The Matrix. public static implicit operator ComplexMatrix(ComplexVector V) { ComplexMatrix NewMatrix; if (V.Type == VectorType.Column) { NewMatrix = new ComplexMatrix(V.Length, 1, V.Data); } else { NewMatrix = new ComplexMatrix(1, V.Length, V.Data); } return NewMatrix; } #region Public Methods #region Add /// /// Add a complex number to all elements of this vector. /// /// The complex number. /// /// VectorComplex r[i] = this[i] + c /// public ComplexVector Add(Complex c) { ComplexVector v = new ComplexVector(this._Type, this._Data.Length); Complex[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] = this._Data[i] + c; } return v; } /// /// In place add a scalar to all elements of this vector. /// /// The complex number. public void AddInplace(Complex c) { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] += c; } } /// /// Add a VectorComplex. /// /// The vector B. /// /// VectorComplex r[i] = this[i] + B[i] /// public ComplexVector Add(ComplexVector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } ComplexVector r = new ComplexVector(this._Type, this.Length); Complex[] rData = r.Data; for (int i = 0; i < rData.Length; i++) { rData[i] = this._Data[i] + B[i]; } return r; } /// /// In place add a VectorComplex. /// /// The vector B. public void AddInplace(ComplexVector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } for (int i = 0; i < this._Data.Length; i++) { this._Data[i] += B[i]; } } #endregion #region Subtract /// /// Subtract a scalar to all elements of this vector. /// /// The complex number. /// /// VectorComplex r[i] = this[i] - c /// public ComplexVector Subtract(Complex c) { ComplexVector v = new ComplexVector(this._Type, this.Length); Complex[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] = this._Data[i] - c; } return v; } /// /// In place subtract a scalar to all elements of this vector. /// /// The complex number. public void SubtractInplace(Complex c) { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] -= c; } } /// /// Subtract a VectorComplex. /// /// The vector B. /// /// VectorComplex r[i] = this[i] - B[i] /// public ComplexVector Subtract(ComplexVector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } ComplexVector r = new ComplexVector(this._Type, this.Length); Complex[] rData = r.Data; for (int i = 0; i < rData.Length; i++) { rData[i] = this._Data[i] - B[i]; } return r; } /// /// In place add a VectorComplex. /// /// The vector B. public void SubtractInplace(ComplexVector B) { if (B.Type != this.Type || B.Length != this.Length) { throw new System.ArgumentException("Vector dimensions or type are not valid."); } for (int i = 0; i < this._Data.Length; i++) { this._Data[i] -= B[i]; } } #endregion #region Multiply /// /// Multiply a scalar to all elements of this vector. /// /// The complex number. /// /// VectorComplex r[i] = this[i] * c /// public ComplexVector Multiply(Complex c) { ComplexVector v = new ComplexVector(this._Type, this.Length); Complex[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] = this._Data[i] * c; } return v; } /// /// In place multiply this vector with a scalar. /// /// The scalar public void MultiplyInplace(Complex scalar) { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] *= scalar; } } #endregion #region UnaryMinus /// /// Unary minus this vector. /// /// /// Vector r[i] = -this[i] /// public ComplexVector UnaryMinus() { ComplexVector v = new ComplexVector(this._Type, this.Length); Complex[] vData = v.Data; for (int i = 0; i < vData.Length; i++) { vData[i] -= this._Data[i]; } return v; } /// /// In place unary minus of this vector. /// public void UnaryMinusInplace() { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] = -this._Data[i]; } } #endregion #region Conjugate /// /// Conjugate this vector. /// /// /// Vector r[i] = Real(this[i]) - Imaginary(this[i]) /// public ComplexVector Conjugate() { ComplexVector conjVect= new ComplexVector(this._Data.Length); Complex[] v = conjVect.Data; for (int i = 0; i < v.Length; i++) { v[i] = this._Data[i].Conjugate; } return conjVect; } /// /// In place conjugation of this vector. /// public void ConjugateInplace() { for (int i = 0; i < this._Data.Length; i++) { this._Data[i] = this._Data[i].Conjugate; } } #endregion /// /// Calculate the 1-norm of the vector. /// /// /// r = sum(abs(this[i])) /// public double Norm1() { double sum = 0; for (int i = 0; i < this._Data.Length; i++) { sum += this._Data[i].Modulus; } return sum; } ///// ///// Calculate the norm of the vector ///// ///// The norm //public double Norm() //{ // double norm = Vector.DotProduct(this); // norm = Math.Sqrt(norm); // return norm; //} /// /// Returns the equivalent string representation of the vector. /// /// The string representation of the vector. public string VectorToString() { using (StringWriter writer = new StringWriter()) { if (this._Type == VectorType.Column) { for (int i = 0; i < this._Data.Length; i++) { writer.Write(this._Data[i]); if (i < this._Data.Length - 1) writer.WriteLine(); } } else if (this._Type == VectorType.Row) { for (int i = 0; i < this._Data.Length; i++) { if (i < this._Data.Length - 1) writer.Write(this._Data[i] + ", "); else writer.Write(this._Data[i]); } } return writer.ToString(); } } #endregion } }