using System; namespace Science.Mathematics.LinearAlgebra { /// /// Vector /// public class Vector { private int s; public int Size { get{return s;} } private double[] el; public Vector(int dimension) { s = dimension; el = new double[s]; } public Vector(double[] x) { s = x.Length; el = new double[s]; for(int k = 0; k < s; k++) el[k] = x[k]; } public double this[int where] { get{return el[where];} set{el[where]=value;} } public double Norm { get { double sq = 0.0; foreach (double k in el) sq += k * k; return Math.Sqrt(sq); } } public double Length { get { return this.Norm; } } public static Vector operator *(double a, Vector c) { Vector b = new Vector(c.Size); for(int k = 0; k < c.Size; k++) b[k] = a*c[k]; return b; } public static Vector operator *(Vector c, double a) { return a*c; } public static Vector operator +(Vector a, Vector c) { Vector b = new Vector(a.Size); for(int k = 0; k < a.Size; k++) b[k] = a[k] + c[k]; return b; } public static Vector operator -(Vector a, Vector c) { Vector b = new Vector(a.Size); for(int k = 0; k < a.Size; k++) b[k] = a[k] - c[k]; return b; } public static Vector operator -(Vector c) { Vector b = new Vector(c.Size); for (int k = 0; k < c.Size; k++) b[k] = -c[k]; return b; } public static double operator *(Vector a, Vector c) { double sum = 0.0; for(int k = 0; k < a.Size; k++) sum += a[k]*c[k]; return sum; } public static Vector operator %(Vector a, Vector c) // CrossProduct in only Length = 3 Real Vector { Vector b = new Vector(3); b[0] = a[1]*c[2]-a[2]*c[1]; b[1] = a[2]*c[0]-a[0]*c[2]; b[2] = a[0]*c[1]-a[1]*c[0]; return b; } public void Chop() { for (int k = 0; k < s; k++) { if (el[k] < 0.00000001 && el[k] > -0.00000001) el[k] = 0.0; } } public override string ToString() { string res = ""; for (int i = 0; i < this.Size; i++) { res += this[i].ToString() + "\r\n"; } return res; } } }