using System; namespace Science.Mathematics.LinearAlgebra { public class VectorComplex { private int s; public int Size { get{return s;} } private Complex[] cel; public VectorComplex(int dimension) { s = dimension; cel = new Complex[s]; for (int k = 0; k < s; k++) cel[k] = new Complex(0.0, 0.0); } public VectorComplex(Complex[] x) { s = x.Length; cel = new Complex[s]; for(int k = 0; k < s; k++) cel[k] = new Complex(x[k].Real,x[k].Imaginary); } public Complex this[int where] { get{return cel[where];} set{cel[where]=value;} } public VectorComplex Conjugate { get { VectorComplex v = new VectorComplex(this.s); for(int k = 0; k < s; k++) { v[k].Real = cel[k].Real; v[k].Imaginary = - cel[k].Imaginary; } return v; } } public double Norm { get { double sq = 0.0; foreach (Complex k in cel) sq += Complex.Norm(k) * Complex.Norm(k); return Math.Sqrt(sq); } } public double Length { get { return this.Norm; } } public static VectorComplex operator *(double a, VectorComplex c) { VectorComplex b = new VectorComplex(c.Size); for (int k = 0; k < c.Size; k++) { b[k].Real = a * c[k].Real; b[k].Imaginary = a * c[k].Imaginary; } return b; } public static VectorComplex operator *(VectorComplex c, double a) { return a*c; } public static VectorComplex operator *(Complex a, VectorComplex c) { VectorComplex b = new VectorComplex(c.Size); for (int k = 0; k < c.Size; k++) { Complex t = a * c[k]; b[k].Real = t.Real; b[k].Imaginary = t.Imaginary; } return b; } public static VectorComplex operator *(VectorComplex c, Complex a) { return a*c; } public static VectorComplex operator +(VectorComplex a, VectorComplex c) { VectorComplex b = new VectorComplex(a.Size); for (int k = 0; k < a.Size; k++) { b[k].Real = a[k].Real + c[k].Real; b[k].Imaginary = a[k].Imaginary + c[k].Imaginary; } return b; } public static VectorComplex operator -(VectorComplex a, VectorComplex c) { VectorComplex b = new VectorComplex(a.Size); for (int k = 0; k < a.Size; k++) { b[k].Real = a[k].Real - c[k].Real; b[k].Imaginary = a[k].Imaginary - c[k].Imaginary; } return b; } public static VectorComplex operator -(VectorComplex a) { VectorComplex b = new VectorComplex(a.Size); for (int k = 0; k < a.Size; k++) { b[k].Real = - a[k].Real; b[k].Imaginary = - a[k].Imaginary; } return b; } public static Complex operator *(VectorComplex a, VectorComplex c) { Complex sum = new Complex(0.0, 0.0); for (int k = 0; k < a.Size; k++) { Complex t = a[k] * c[k]; sum.Real += t.Real; sum.Imaginary += t.Imaginary; } return sum; } public static Complex operator ^(VectorComplex a, VectorComplex c) { VectorComplex ac = a.Conjugate; return ac * c; } public void Chop() { for (int k = 0; k < this.s; k++) { if (cel[k].Real < 0.00000001 && cel[k].Real > -0.00000001) cel[k].Real = 0.0; if (cel[k].Imaginary < 0.00000001 && cel[k].Imaginary > -0.00000001) cel[k].Imaginary = 0.0; } } public override string ToString() { string res = ""; for (int i = 0; i < this.Size; i++) { res += this[i].ToString() + "\r\n"; } return res; } } }