using System; namespace Science.Mathematics.VectorCalculus { public class Vector { protected double[] el; protected int n; public int Dimension { get{return n;} } public Vector() { } public Vector(Point p) { el = new double[p.Coordinate.Length]; for (int k = 0; k < p.Coordinate.Length; k++) el[k] = p.Coordinate[k]; n = p.Coordinate.Length; } public Vector(Point p, Point q) { el = new double[p.Coordinate.Length]; for (int k = 0; k < p.Coordinate.Length; k++) el[k] = q.Coordinate[k] - p.Coordinate[k]; n = p.Coordinate.Length; } public Vector(double[] x) { el = new double[x.Length]; for (int k = 0; k < x.Length; k++) el[k] = x[k]; n = x.Length; } public Vector(int dimension) { el = new double[dimension]; n = dimension; } 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.Dimension); for(int k = 0; k < c.Dimension; 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.Dimension); for (int k = 0; k < c.Dimension; k++) b[k] = a[k]+c[k]; return b; } public static Vector operator -(Vector a, Vector c) { Vector b = new Vector(a.Dimension); for (int k = 0; k < c.Dimension; k++) b[k] = a[k]-c[k]; return b; } public static Vector operator -(Vector c) { Vector b = new Vector(c.Dimension); for (int k = 0; k < c.Dimension; 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.Dimension; k++) sum += a[k]*c[k]; return sum; } public static Vector operator %(Vector a, Vector b) // CrossProduct { Vector c = new Vector(3); c[0] = a[1] * b[2] - a[2] * b[1]; c[1] = a[2] * b[0] - a[0] * b[2]; c[2] = a[0] * b[1] - a[1] * b[0]; return c; } public Vector OrthogonalProjection(Vector on) { return this*on / on.Norm / on.Norm * on; } public override string ToString() { string res = "("; for (int i = 0; i < this.Dimension - 1; i++) { res += this[i].ToString() + ","; } res += this[this.Dimension - 1].ToString() + ")" + "\r\n"; return res; } } }