public void TestVectorMath() { Vector v1 = new Vector(5); Vector v2 = new Vector(5); v1.SetValue(0, 1.0); v1.SetValue(1, 2.0); v1.SetValue(2, 3.0); v1.SetValue(3, 4.0); v1.SetValue(4, 5.0); v2.SetValue(0, 5.0); v2.SetValue(1, 4.0); v2.SetValue(2, 3.0); v2.SetValue(3, 2.0); v2.SetValue(4, 1.0); Vector sum = new Vector(5); sum.SetValue(0, 6.0); sum.SetValue(1, 6.0); sum.SetValue(2, 6.0); sum.SetValue(3, 6.0); sum.SetValue(4, 6.0); Assert.AreEqual(sum, v1 + v2); Vector diff = new Vector(5); diff.SetValue(0, -4.0); diff.SetValue(1, -2.0); diff.SetValue(2, 0.0); diff.SetValue(3, 2.0); diff.SetValue(4, 4.0); Assert.AreEqual(diff, v1 - v2); double dotprod = 35.0; Assert.AreEqual(dotprod, v1 * v2); Matrix m = new Matrix(2, 2); m.SetValue(0, 0, 1); m.SetValue(0, 1, 2); m.SetValue(1, 0, 3); m.SetValue(1, 1, 4); Vector v = new Vector(2); v.SetValue(0, 1); v.SetValue(1, 2); Vector result1 = new Vector(2); result1.SetValue(0, 5); result1.SetValue(1, 11); Vector result2 = new Vector(2); result2.SetValue(0, 7); result2.SetValue(1, 10); Assert.AreEqual(result1, m * v); Assert.AreEqual(result2, v * m); }
/* Create a new vector from an array of doubles */ public static Vector NewVector(double[] input) { if (input.Length == 0) throw new FormatException("Vector is an invalid format"); Vector v = new Vector(input.Length); v.vector = input; return v; }
/* Vector * Scalar -> Vector */ public Vector MultiplyScalar(object right) { double rightCast = 0.0; if (right.GetType() == typeof(int) || right.GetType() == typeof(float)) { rightCast = Convert.ToDouble(right); } else if (right.GetType() == typeof(double)) { rightCast = (double)right; } else { throw new InvalidOperationException("Scalar must be a double or convertable to a double"); } Vector result = new Vector(this.Size); for (int i = 0; i < result.Size; i++) { result.SetValue(i, this.GetValue(i) * rightCast); } return result; }
/* Add two vectors */ public Vector AddVector(object right) { if (!checkValidType(right)) { throw new InvalidOperationException("Can only add vectors with other vectors"); } Vector rightCast = (Vector)right; if (this.Size != rightCast.Size) { throw new DimensionMismatchException("Vectors must be same size"); } Vector result = new Vector(this.Size); for (int i = 0; i < this.Size; i++) { result.SetValue(i, this.GetValue(i) + rightCast.GetValue(i)); } return result; }
public void TestVectorIndexer() { Vector v = new Vector(3); v.SetValue(0, 1.0); v.SetValue(1, 2.0); v.SetValue(2, 3.0); Assert.AreEqual(v[0], 1.0); Assert.AreEqual(v[1], 2.0); Assert.AreEqual(v[2], 3.0); v[0] = 1.0; v[1] = 2.0; v[2] = 3.0; Assert.AreEqual(v[0], 1.0); Assert.AreEqual(v[1], 2.0); Assert.AreEqual(v[2], 3.0); }
/* Vector * Matrix -> Vector */ public Vector MultiplyMatrix(object right) { if (right.GetType() != typeof(Matrix)) { throw new InvalidOperationException("Can't call MultiplyMatrix with anything but a Matrix"); } Matrix rightCast = (Matrix)right; if (this.Size != rightCast.M) { throw new DimensionMismatchException("The dimension of the vector must" + " equal the M dimension of the matrix"); } Vector result = new Vector(rightCast.N); for (int c = 0; c < rightCast.N; c++) { for (int i = 0; i < this.Size; i++) { result.SetValue(c, result.GetValue(c) + this.GetValue(i) * rightCast.GetValue(i, c)); } } return result; }
public void TestVectorFactoryNewVector() { Vector v = Vector.NewVector(new double[]{1.0,2.0,3.0}); Vector expected = new Vector(3); expected[0] = 1.0; expected[1] = 2.0; expected[2] = 3.0; Assert.AreEqual(expected, v); v = Vector.NewVector("{1.0,2.0,3.0}"); Assert.AreEqual(expected, v); try { v = Vector.NewVector("{{"); Assert.Fail(); } catch (FormatException) { } try { v = Vector.NewVector("{}"); Assert.Fail(); } catch (FormatException) { } try { v = Vector.NewVector("{1.0,,2.0}"); Assert.Fail(); } catch (FormatException) { } try { v = Vector.NewVector("{13.0.0}"); Assert.Fail(); } catch (FormatException) { } try { v = Vector.NewVector("{"); Assert.Fail(); } catch (FormatException) { } }
public void TestVectorSize() { Vector v = new Vector(5); v.SetValue(0, 1.0); v.SetValue(1, 1.0); v.SetValue(2, 1.0); v.SetValue(3, 1.0); v.SetValue(4, 1.0); Assert.AreEqual(v.Size, 5); }
public Vector getRange(int[] idx) { Vector X = new Vector(idx.Length); for (int j = 0; j < idx.Length; j++) { X[j] = this[idx[j]]; } return X; }
/// <summary> /// Gets The Row Of A Matrix As A Vector /// </summary> /// <param name="row">Index Of The Row</param> /// <returns></returns> public MathLib.Vector GetRow(int row) { MathLib.Vector output = new Vector(this.Width); int i, I = this.Width; for (i = 0; i < I; i++) { output[i] = this[row, i]; } return output; }
/// <summary> /// * Operator /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static MathLib.Vector operator *(Vector lhs, Matrix rhs) { if (lhs.Dimension != rhs.Height) { throw new ArgumentException("lhs Width != rhs Height"); } else { MathLib.Vector output = new Vector(rhs.Width); int i, I = output.Dimension; int j, J = lhs.Dimension; double[] vec = (double[])lhs; for (i = 0; i < I; i++) { double sum = 0; for (j = 0; j < J; j++) sum += vec[j] * rhs._MatrixData[j, i]; output[i] = sum; } return output; } }
/// <summary> /// Calculates The CrossProduct Of A Pair Of Vectors /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static Vector CrossProduct(Vector lhs, Vector rhs) { if ((lhs.Dimension != rhs.Dimension) || (lhs.Dimension != 3)) { throw new System.ArgumentException("lhs & rhs Dimensions are different or not equal to 3"); } else { Vector output = new MathLib.Vector(3); output[0] = lhs[2] * rhs[3] - lhs[3] * rhs[2]; output[1] = lhs[3] * rhs[1] - lhs[1] * rhs[3]; output[2] = lhs[1] * rhs[2] - lhs[2] * rhs[1]; return output; } }
/// <summary> /// Calcuates The DotProduct Of A Pair Of Vectors /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static double DotProduct(Vector lhs, Vector rhs) { if (lhs.Dimension != rhs.Dimension) { throw new System.ArgumentException("lhs & rhs Dimensions are different"); } else { int i; double output = 0; for (i = 0; i < lhs.Dimension; i++) { output += lhs[i] * rhs[i]; } return output; } }
public static Vector min(Vector V, double s) { return min(s, V); }
public static Vector min(double s, Vector V) { Vector X = new Vector(V.Dimension); for (int j = 0; j < V.Dimension; j++) { X[j] = Math.Min(s, V[j]); } return X; }
public Vector getRange(int a, int b) { Vector X = new Vector(b - a); int k = 0; for (int j = a; j <= b; j++) { X[k] = this[j]; k++; } return X; }
/* Matrix * Vector -> Vector */ public Vector MultiplyVector(object right) { if (right.GetType() != typeof(Vector)) { throw new InvalidOperationException("Can't call MultiplyVector with anything but a Vector"); } Vector rightCast = (Vector)right; if (this.N != rightCast.Size) { throw new DimensionMismatchException("The N dimension of the matrix must equal the size of the vector"); } Vector result = new Vector(this.m); for (int r = 0; r < this.m; r++) { for (int i = 0; i < this.n; i++) { result.SetValue(r, result.GetValue(r) + rightCast.GetValue(i) * this.GetValue(r, i)); } } return result; }
/// <summary> /// Gets A Column Of The Matrix as a Vector /// </summary> /// <param name="column"></param> /// <returns></returns> public MathLib.Vector GetColumn(int column) { MathLib.Vector output = new Vector(this.Height); double[] vec_data = (double[])output; int i = 0, I = this.Height; for (i = 0; i < I; i++) { vec_data[i] = this._MatrixData[i, column]; } return output; }
/// <summary> /// Thomas algorithm for solving a tridiagonal system of equations /// /// Equation ith: /// Li[i]*xv[i-1] + Di[i]*xv[i] + Ui[i]*xv[i+1] = Bi[i] /// </summary> /// <param name="Li">lower diagonal n = 0,.. n-1 Li[0] = 0</param> /// <param name="Di">diagonal n = 0,.. n-1</param> /// <param name="Ui">lower diagonal n = 0,.. n-1 Ui[n-1] = 0</param> /// <param name="Bi"></param> /// <returns>xv</returns> public static Vector Thomas(Vector Li, Vector Di, Vector Ui, Vector Bi) { if ((Di.Dimension != Li.Dimension) || (Ui.Dimension != Li.Dimension) || (Bi.Dimension != Li.Dimension)) throw new ArgumentException("Vector's not of consistent dimension."); if (Di[0] == 0.0) throw new ArithmeticException("Error 1 in Thomas"); double bet; Vector xv = new Vector(Li.Dimension); Vector Gi = new Vector(Li.Dimension); // Forward substitution Gi[0] = Di[0]; xv[0] = Bi[0]; for (int i = 1; i < Li.Dimension; i++) { bet = Li[i] / Gi[i - 1]; Gi[i] = Di[i] - bet * Ui[i - 1]; xv[i] = Bi[i] - bet * xv[i - 1]; if (Gi[i] == 0.0) throw new ArithmeticException("Error 2 in Thomas"); } // Backward substitution xv[Li.Dimension - 1] = xv[Li.Dimension - 1] / Gi[Li.Dimension - 1]; for (int i = Li.Dimension - 2; i >= 0; i--) xv[i] = (xv[i] - Ui[i] * xv[i + 1]) / Gi[i]; return xv; }
/// <summary> /// + Operator /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static MathLib.Vector operator +(double lhs, Vector rhs) { int i; Vector output = new Vector(rhs.Dimension); for (i = 0; i < rhs.Dimension; i++) { output[i] = rhs[i] + lhs; } return output; }