/* 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); }
/* 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); }
/* Override Object.Equals function */ public override bool Equals(object other) { if (!checkValidType(other)) { return(false); } Vector otherCast = (Vector)other; if (otherCast.Size != this.Size) { return(false); } for (int i = 0; i < this.Size; i++) { if (!(this.GetValue(i) + .00001 >= otherCast.GetValue(i) && this.GetValue(i) - .00001 <= otherCast.GetValue(i))) { return(false); } } return(true); }
/* 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; }
/* Vector * Vector -> Scalar */ public double MultiplyVector(object right) { if (!checkValidType(right)) { throw new InvalidOperationException("Can't call MultiplyVector with anything but a Vector"); } Vector rightCast = (Vector)right; if (this.Size != rightCast.Size) { throw new DimensionMismatchException("The dimensions of the vectors must be equal"); } double result = 0; for (int i = 0; i < this.Size; i++) { result += this.GetValue(i) * rightCast.GetValue(i); } 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); }
/* 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; }