public void AddVectorsDiffSizeTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1), new Number(3) }); var result = vector1.Add(vector2); }
public void AddVectorsDiffSizeTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1), new Number(3) }); Assert.Throws<ArgumentException>(() => vector1.Add(vector2)); }
/// <summary> /// Initializes a new instance of the <see cref="Matrix"/> class. /// </summary> /// <param name="args">The arguments.</param> /// <exception cref="System.ArgumentNullException"><paramref name="args"/> is null.</exception> /// <exception cref="System.ArgumentException"></exception> public Matrix(Vector[] args) : base(args, args.Length) { if (args == null) throw new ArgumentNullException(nameof(args)); if (args.Length < 1) throw new ArgumentException(); }
public void CrossProductToStringTest() { var left = new Matrices.Vector(new[] { new Number(1), new Number(-2) }); var right = new Matrices.Vector(new[] { new Number(4), new Number(0) }); var exp = new CrossProduct(left, right); Assert.Equal("crossProduct({1, -2}, {4, 0})", exp.ToString(commonFormatter)); }
/// <summary> /// Initializes a new instance of the <see cref="Matrix"/> class. /// </summary> /// <param name="matrixSize">The size of the matrix.</param> /// <param name="vectorSize">The size of the vector.</param> public Matrix(int matrixSize, int vectorSize) : base(null, matrixSize) { var vectors = new Vector[matrixSize]; for (var i = 0; i < vectors.Length; i++) vectors[i] = new Vector(vectorSize); Arguments = vectors; }
public void MulByNumberVectorTest() { var vector = new Vector(new[] { new Number(2), new Number(3) }); var number = new Number(5); var expected = new Vector(new[] { new Number(10), new Number(15) }); var result = vector.Mul(number); Assert.Equal(expected, result); }
public void AddVectorsTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1) }); var expected = new Vector(new[] { new Number(9), new Number(4) }); var result = vector1.Add(vector2); Assert.Equal(expected, result); }
public void SubTwoVectorsTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1) }); var sub = new Sub(vector1, vector2); var expected = new Vector(new[] { new Number(-5), new Number(2) }); var result = sub.Calculate(); Assert.AreEqual(expected, result); }
public void AddTwoVectorsTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1) }); var add = new Add(vector1, vector2); var expected = new Vector(new[] { new Number(9), new Number(4) }); var result = add.Calculate(); Assert.AreEqual(expected, result); }
public void MultiOpMulMul() { // ({1, 2, 3} * 2) * 4 var vector = new Vector(new[] { new Number(1), new Number(2), new Number(3) }); var mul1 = new Mul(vector, new Number(2)); var mul2 = new Mul(mul1, new Number(4)); var expected = new Vector(new[] { new Number(8), new Number(16), new Number(24) }); var result = mul2.Calculate(); Assert.AreEqual(expected, result); }
public void MultiOpAddMul() { // ({2, 3, 4} + {1, 2, 3}) * 4 var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) }); var add = new Add(vector2, vector1); var mul = new Mul(add, new Number(4)); var expected = new Vector(new[] { new Number(12), new Number(20), new Number(28) }); var result = mul.Execute(); Assert.Equal(expected, result); }
/// <summary> /// Adds the <paramref name="right"/> vector to the <paramref name="left"/> vector. /// </summary> /// <param name="left">The left vector.</param> /// <param name="right">The right vector.</param> /// <param name="parameters">An object that contains all parameters and functions for expressions.</param> /// <returns>The sum of matrices.</returns> /// <exception cref="System.ArgumentException">The size of matrices is invalid.</exception> public static Vector Add(this Vector left, Vector right, ExpressionParameters parameters) { if (left.ParametersCount != right.ParametersCount) throw new ArgumentException(Resource.MatrixArgException); var exps = new IExpression[left.ParametersCount]; Parallel.For(0, left.ParametersCount, i => exps[i] = new Number((double)left.Arguments[i].Execute(parameters) + (double)right.Arguments[i].Execute(parameters)) ); return new Vector(exps); }
public void ExecuteVectorTest() { var vector = new Vector(new[] { new Number(1), new Number(2) }); var expected = new Matrix(new[] { new Vector(new[] { new Number(1) }), new Vector(new[] { new Number(2) }) }); var exp = new Transpose(vector); Assert.Equal(expected, exp.Execute()); }
public void MultiOpMulAdd() { // ({1, 2, 3} * 4) + {2, 3, 4} var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) }); var mul = new Mul(vector1, new Number(4)); var add = new Add(mul, vector2); var expected = new Vector(new[] { new Number(6), new Number(11), new Number(16) }); var result = add.Execute(); Assert.Equal(expected, result); }
public void ExecuteTest2() { var exp = new Del(new Add(new Add(new Mul(new Number(2), new Variable("x1")), new Pow(new Variable("x2"), new Number(2))), new Pow(new Variable("x3"), new Number(3)))); exp.Differentiator = new Differentiator(); exp.Simplifier = new Simplifier(); var expected = new Vector(new IExpression[] { new Number(2), new Mul(new Number(2), new Variable("x2")), new Mul(new Number(3), new Pow(new Variable("x3"), new Number(2))) }); Assert.Equal(expected, exp.Execute()); }
/// <summary> /// Adds the <paramref name="right"/> vector to the <paramref name="left"/> vector. /// </summary> /// <param name="left">The left vector.</param> /// <param name="right">The right vector.</param> /// <param name="parameters">An object that contains all parameters and functions for expressions.</param> /// <returns>The sum of matrices.</returns> /// <exception cref="System.ArgumentException">The size of matrices is invalid.</exception> public static Vector Add(this Vector left, Vector right, ExpressionParameters parameters) { if (left.ParametersCount != right.ParametersCount) throw new ArgumentException(Resource.MatrixArgException); var exps = new IExpression[left.ParametersCount]; #if NET40_OR_GREATER Parallel.For(0, left.ParametersCount, i => exps[i] = new Number((double)left.Arguments[i].Calculate(parameters) + (double)right.Arguments[i].Calculate(parameters)) ); #else for (int i = 0; i < left.ParametersCount; i++) exps[i] = new Number((double)left.Arguments[i].Calculate(parameters) + (double)right.Arguments[i].Calculate(parameters)); #endif return new Vector(exps); }
/// <summary> /// Adds the <paramref name="right"/> matrix to the <paramref name="left"/> matrix. /// </summary> /// <param name="left">The left matrix.</param> /// <param name="right">The right matrix.</param> /// <param name="parameters">An object that contains all parameters and functions for expressions.</param> /// <returns>The sum of matrices.</returns> /// <exception cref="System.ArgumentException">The size of matrices is invalid.</exception> public static Matrix Add(this Matrix left, Matrix right, ExpressionParameters parameters) { if (left.ParametersCount != right.ParametersCount || left.SizeOfVectors != right.SizeOfVectors) throw new ArgumentException(Resource.MatrixArgException); var vectors = new Vector[left.ParametersCount]; #if NET40_OR_GREATER Parallel.For(0, left.ParametersCount, i => { var exps = new IExpression[left.SizeOfVectors]; for (int j = 0; j < left.SizeOfVectors; j++) exps[j] = new Number((double)left[i][j].Calculate(parameters) + (double)right[i][j].Calculate(parameters)); vectors[i] = new Vector(exps); }); #else for (int i = 0; i < left.ParametersCount; i++) { var exps = new IExpression[left.SizeOfVectors]; for (int j = 0; j < left.SizeOfVectors; j++) exps[j] = new Number((double)left[i][j].Calculate(parameters) + (double)right[i][j].Calculate(parameters)); vectors[i] = new Vector(exps); } #endif return new Matrix(vectors); }
/// <summary> /// Transposes the specified vector. /// </summary> /// <param name="vector">The vector.</param> /// <returns>The transposed matrix.</returns> public static Matrix Transpose(this Vector vector) { var vectors = new Vector[vector.ParametersCount]; #if NET40_OR_GREATER Parallel.For(0, vectors.Length, i => vectors[i] = new Vector(new[] { vector[i] })); #else for (int i = 0; i < vectors.Length; i++) vectors[i] = new Vector(new[] { vector[i] }); #endif return new Matrix(vectors); }
/// <summary> /// Subtracts the <paramref name="right"/> vector from the <paramref name="left"/> vector. /// </summary> /// <param name="left">The left vector.</param> /// <param name="right">The right vector.</param> /// <returns>The difference of matrices.</returns> public static Vector Sub(this Vector left, Vector right) { return Sub(left, right, null); }
/// <summary> /// Adds the <paramref name="right"/> vector to the <paramref name="left"/> vector. /// </summary> /// <param name="left">The left vector.</param> /// <param name="right">The right vector.</param> /// <returns>The sum of matrices.</returns> public static Vector Add(this Vector left, Vector right) { return Add(left, right, null); }
/// <summary> /// Multiplies the <paramref name="left" /> matrix by the <paramref name="right" /> vector. /// </summary> /// <param name="left">The left matrix.</param> /// <param name="right">The right vector.</param> /// <returns> /// The product of matrices. /// </returns> public static Matrix Mul(this Matrix left, Vector right) { return Mul(left, right, null); }
/// <summary> /// Multiplies the <paramref name="left" /> matrix by the <paramref name="right" /> vector. /// </summary> /// <param name="left">The left matrix.</param> /// <param name="right">The right vector.</param> /// <param name="parameters">An object that contains all parameters and functions for expressions.</param> /// <returns> /// The product of matrices. /// </returns> /// <exception cref="System.ArgumentException">The size of matrices is invalid.</exception> public static Matrix Mul(this Matrix left, Vector right, ExpressionParameters parameters) { var matrix = new Matrix(new[] { right }); return left.Mul(matrix, parameters); }
public void MulByNumberMatrixTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(9), new Number(5) }); var matrix = new Matrix(new[] { vector1, vector2 }); var number = new Number(5); var expected = new Matrix(new[] { new Vector(new[] { new Number(10), new Number(15) }), new Vector(new[] { new Number(45), new Number(25) }) }); var result = matrix.Mul(number); Assert.AreEqual(expected, result); }
static double Absolute(Vector vec, ExpressionParameters parameters) { return Math.Sqrt( vec.Arguments.Sum ( a => Math.Pow ( ( double ) a.Calculate ( parameters ), 2 ) ) ); }
public void MultiOpSubMul() { // ({2, 3, 4} - {1, 2, 3}) * 4 var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) }); var sub = new Sub(vector2, vector1); var mul = new Mul(sub, new Number(4)); var expected = new Vector(new[] { new Number(4), new Number(4), new Number(4) }); var result = mul.Execute(); Assert.Equal(expected, result); }
public void MatrixMulVectorTest() { var vector = new Vector(new[] { new Number(-2), new Number(1) }); var matrix = new Matrix(new[] { new Vector(new[] { new Number(3) }), new Vector(new[] { new Number(-1) }) }); var expected = new Matrix(new[] { new Vector(new[] { new Number(-6), new Number(3) }), new Vector(new[] { new Number(2), new Number(-1) }) }); var result = matrix.Mul(vector); Assert.AreEqual(expected, result); }
public void SubVectorsTest() { var vector1 = new Vector(new[] { new Number(2), new Number(3) }); var vector2 = new Vector(new[] { new Number(7), new Number(1) }); var expected = new Vector(new[] { new Number(-5), new Number(2) }); var result = vector1.Sub(vector2); Assert.Equal(expected, result); }
/// <summary> /// Adds the <paramref name="right"/> matrix to the <paramref name="left"/> matrix. /// </summary> /// <param name="left">The left matrix.</param> /// <param name="right">The right matrix.</param> /// <param name="parameters">An object that contains all parameters and functions for expressions.</param> /// <returns>The sum of matrices.</returns> /// <exception cref="System.ArgumentException">The size of matrices is invalid.</exception> public static Matrix Add(this Matrix left, Matrix right, ExpressionParameters parameters) { if (left.ParametersCount != right.ParametersCount || left.SizeOfVectors != right.SizeOfVectors) throw new ArgumentException(Resource.MatrixArgException); var vectors = new Vector[left.ParametersCount]; Parallel.For(0, left.ParametersCount, i => { var exps = new IExpression[left.SizeOfVectors]; for (int j = 0; j < left.SizeOfVectors; j++) exps[j] = new Number((double)left[i][j].Execute(parameters) + (double)right[i][j].Execute(parameters)); vectors[i] = new Vector(exps); }); return new Matrix(vectors); }
public void TransposeVectorTest() { var vector = new Vector(new[] { new Number(1), new Number(2) }); var expected = new Matrix(new[] { new Vector(new[] { new Number(1) }), new Vector(new[] { new Number(2) }) }); var result = vector.Transpose(); Assert.Equal(expected, result); }
/// <summary> /// Transposes the specified vector. /// </summary> /// <param name="vector">The vector.</param> /// <returns>The transposed matrix.</returns> public static Matrix Transpose(this Vector vector) { var vectors = new Vector[vector.ParametersCount]; Parallel.For(0, vectors.Length, i => vectors[i] = new Vector(new[] { vector[i] })); return new Matrix(vectors); }
public void VectorMulMatrixTest() { var vector = new Vector(new[] { new Number(-2), new Number(1) }); var matrix = new Matrix(new[] { new Vector(new[] { new Number(3) }), new Vector(new[] { new Number(-1) }) }); var expected = new Matrix(new[] { new Vector(new[] { new Number(-7) }) }); var result = vector.Mul(matrix); Assert.Equal(expected, result); }