using System; using System.Collections; namespace Science.Mathematics.Calculus { /// /// A vector is formally defined as an element of a vector space. /// In the commonly encountered vector space R^{n} /// (i.e., Euclidean n-space), a vector is given /// by n coordinates and can be specified /// as (A_{1},A_{2},...,A_{n}). /// public class Vector { private int s; public int Size { get{return s;} set { s = value; if (ctype) { cel = new Complex[s]; for(int k = 0; k < s; k++) cel[k] = new Complex(0.0,0.0); } else el = new double[s]; } } private double[] el; private Complex[] cel; public double[] Element { get{return el;} } public Complex[] ElementComplex { get{return cel;} } private bool ctype=false; public bool ComplexQ { get{return ctype;} set{ctype=value;} } public Vector() { } public Vector(int size) { this.Size = size; } public Vector(bool complexQ, int size) { ctype = complexQ; this.Size = size; } public Vector(double[] x) { s = x.Length; el = new double[s]; for(int k = 0; k < s; k++) el[k] = x[k]; } public Vector(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); ctype=true; } public double this[int where] { get{return el[where];} set{el[where]=value;} } public Complex this[bool complexQ, int where] { get{return cel[where];} set{cel[where]=value;} } private Science.Mathematics.Function.DoubleToDoubleA g; private Science.Mathematics.Function.DoubleAToDoubleA ga; private Science.Mathematics.Function.ComplexToComplexA gc; private Science.Mathematics.Function.ComplexAToComplexA gac; public Science.Mathematics.Function.DoubleToDoubleA FunctionOfSingleRealVariable { get { return g; } set { g = value; } } public Science.Mathematics.Function.DoubleAToDoubleA FunctionOfMultiRealVariable { get{return ga;} set { ga = value; } } public Science.Mathematics.Function.ComplexToComplexA FunctionOfSingleComplexVariable { get { return gc; } set { gc = value; } } public Science.Mathematics.Function.ComplexAToComplexA FunctionOfMultiComplexVariable { get { return gac; } set { gac = value; } } public void Set(double t) { for(int k = 0; k < g(t).Length; k++) el[k] = g(t)[k]; } public void Set(double[] t) { for(int k = 0; k < ga(t).Length; k++) el[k] = ga(t)[k]; } public void Set(Complex t) { for (int k = 0; k < gc(t).Length; k++) cel[k] = gc(t)[k]; } public void Set(Complex[] t) { for (int k = 0; k < gac(t).Length; k++) cel[k] = gac(t)[k]; } public void Set(int where, double number) { el[where] = number; } public void Set(int where, Complex number) { cel[where].Real = number.Real; cel[where].Imaginary = number.Imaginary; } public Vector Conjugate { get { Vector v = new Vector(true,this.Size); for(int k = 0; k < s; k++) { Complex c = new Complex(this.ElementComplex[k].Real,-this.ElementComplex[k].Imaginary); v.Set(k,c); } return v; } } public void Chop() { if(ctype==true) { for(int k = 0; k < 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; } } else { for(int k = 0; k < s; k++) { if(el[k] < 0.00000001 && el[k] > -0.00000001) el[k] = 0.0; } } } public double Norm { get { if(ctype==true) { double sq = 0.0; foreach(Complex k in cel) sq += Complex.Norm(k)*Complex.Norm(k); return Math.Sqrt(sq); } else { double sq = 0.0; foreach(double k in el) sq += k*k; return Math.Sqrt(sq); } } } public static Vector operator +(Vector a, Vector b) { bool type = a.ComplexQ | b.ComplexQ; Vector c = new Vector(type,a.Size); if(type) { for(int i=0; i< a.Size; i++) c[true,i] = a[true,i] + b[true,i]; } else { for(int i=0; i< a.Size; i++) c[i] = a[i] + b[i]; } return c; } public static Vector operator -(Vector a, Vector b) { bool type = a.ComplexQ | b.ComplexQ; Vector c = new Vector(type, a.Size); if(type) { for(int i=0; i< a.Size; i++) c[true,i] = a[true,i] - b[true,i]; } else { for(int i=0; i< a.Size; i++) c[i] = a[i] - b[i]; } return c; } public static Vector operator *(double a, Vector b) { Vector c = new Vector(b.ComplexQ,b.Size); if(b.ComplexQ) { for(int i=0; i< b.Size; i++) c[true,i] = a * b[true,i]; } else { for(int i=0; i< b.Size; i++) c[i] = a * b[i]; } return c; } public static Vector operator *(Vector b, double a) { return a*b; } public static Vector operator *(Complex a, Vector b) { Vector c = new Vector(true, b.Size); if (b.ComplexQ) { for (int i = 0; i < b.Size; i++) c[true, i] = a * b[true, i]; } else { for (int i = 0; i < b.Size; i++) c[true, i] = a * b[i]; } return c; } public static Vector operator *(Vector b, Complex a) { return a * b; } } }