From 40b698c3822000a33a089cc7dccd612423ef127e Mon Sep 17 00:00:00 2001 From: sys_27 Date: Thu, 20 Oct 2016 17:22:38 +0300 Subject: [PATCH] #150 add, sub --- xFunc.Maths/Expressions/Add.cs | 9 ++++ xFunc.Maths/Expressions/Sub.cs | 9 ++++ xFunc.Tests/Expressions/Maths/AddTest.cs | 28 ++++++++++++ xFunc.Tests/Expressions/Maths/SubTest.cs | 56 ++++++++++++++++++------ 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/xFunc.Maths/Expressions/Add.cs b/xFunc.Maths/Expressions/Add.cs index 4e74af2c0..394df4ceb 100644 --- a/xFunc.Maths/Expressions/Add.cs +++ b/xFunc.Maths/Expressions/Add.cs @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. using System; +using System.Numerics; using xFunc.Maths.Expressions.Matrices; namespace xFunc.Maths.Expressions @@ -70,6 +71,14 @@ public override object Execute(ExpressionParameters parameters) var leftResult = m_left.Execute(parameters); var rightResult = m_right.Execute(parameters); + if (ResultType == ExpressionResultType.ComplexNumber) + { + var leftComplex = leftResult is Complex ? (Complex)leftResult : (double)leftResult; + var rightComplex = rightResult is Complex ? (Complex)rightResult : (double)rightResult; + + return Complex.Add(leftComplex, rightComplex); + } + if (ResultType == ExpressionResultType.Matrix) return ((Matrix)leftResult).Add((Matrix)rightResult, parameters); diff --git a/xFunc.Maths/Expressions/Sub.cs b/xFunc.Maths/Expressions/Sub.cs index c26d2ed65..3b17a598e 100644 --- a/xFunc.Maths/Expressions/Sub.cs +++ b/xFunc.Maths/Expressions/Sub.cs @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. using System; +using System.Numerics; using xFunc.Maths.Expressions.Matrices; namespace xFunc.Maths.Expressions @@ -71,6 +72,14 @@ public override object Execute(ExpressionParameters parameters) var leftResult = m_left.Execute(parameters); var rightResult = m_right.Execute(parameters); + if (ResultType == ExpressionResultType.ComplexNumber) + { + var leftComplex = leftResult is Complex ? (Complex)leftResult : (double)leftResult; + var rightComplex = rightResult is Complex ? (Complex)rightResult : (double)rightResult; + + return Complex.Subtract(leftComplex, rightComplex); + } + if (ResultType == ExpressionResultType.Matrix) return ((Matrix)leftResult).Sub((Matrix)rightResult, parameters); diff --git a/xFunc.Tests/Expressions/Maths/AddTest.cs b/xFunc.Tests/Expressions/Maths/AddTest.cs index a825c0b24..32f50c0ed 100644 --- a/xFunc.Tests/Expressions/Maths/AddTest.cs +++ b/xFunc.Tests/Expressions/Maths/AddTest.cs @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. using System; +using System.Numerics; using xFunc.Maths.Expressions; using xFunc.Maths.Expressions.Matrices; using Xunit; @@ -39,6 +40,33 @@ public void ExecuteTest2() Assert.Equal(-1.0, exp.Execute()); } + [Fact] + public void ExecuteTest3() + { + var exp = new Add(new ComplexNumber(7, 3), new ComplexNumber(2, 4)); + var expected = new Complex(9, 7); + + Assert.Equal(expected, exp.Execute()); + } + + [Fact] + public void ExecuteTest4() + { + var exp = new Add(new Number(7), new ComplexNumber(2, 4)); + var expected = new Complex(9, 4); + + Assert.Equal(expected, exp.Execute()); + } + + [Fact] + public void ExecuteTest5() + { + var exp = new Add(new ComplexNumber(7, 3), new Number(2)); + var expected = new Complex(9, 3); + + Assert.Equal(expected, exp.Execute()); + } + [Fact] public void AddTwoVectorsTest() { diff --git a/xFunc.Tests/Expressions/Maths/SubTest.cs b/xFunc.Tests/Expressions/Maths/SubTest.cs index dc64f4afc..9d4bc5cf6 100644 --- a/xFunc.Tests/Expressions/Maths/SubTest.cs +++ b/xFunc.Tests/Expressions/Maths/SubTest.cs @@ -13,24 +13,52 @@ // See the License for the specific language governing permissions and // limitations under the License. using System; +using System.Numerics; using xFunc.Maths.Expressions; using xFunc.Maths.Expressions.Matrices; using Xunit; namespace xFunc.Tests.Expressions.Maths { - + public class SubTest { [Fact] - public void ExecuteTest() + public void ExecuteTest1() { IExpression exp = new Sub(new Number(1), new Number(2)); Assert.Equal(-1.0, exp.Execute()); } + [Fact] + public void ExecuteTest2() + { + var exp = new Sub(new ComplexNumber(7, 3), new ComplexNumber(2, 4)); + var expected = new Complex(5, -1); + + Assert.Equal(expected, exp.Execute()); + } + + [Fact] + public void ExecuteTest3() + { + var exp = new Sub(new Number(7), new ComplexNumber(2, 4)); + var expected = new Complex(5, -4); + + Assert.Equal(expected, exp.Execute()); + } + + [Fact] + public void ExecuteTest4() + { + var exp = new Sub(new ComplexNumber(7, 3), new Number(2)); + var expected = new Complex(5, 3); + + Assert.Equal(expected, exp.Execute()); + } + [Fact] public void SubTwoVectorsTest() { @@ -47,22 +75,22 @@ public void SubTwoVectorsTest() [Fact] public void SubTwoMatricesTest() { - var matrix1 = new Matrix(new[] - { - new Vector(new[] { new Number(6), new Number(3) }), - new Vector(new[] { new Number(2), new Number(1) }) + var matrix1 = new Matrix(new[] + { + new Vector(new[] { new Number(6), new Number(3) }), + new Vector(new[] { new Number(2), new Number(1) }) }); - var matrix2 = new Matrix(new[] - { - new Vector(new[] { new Number(9), new Number(2) }), - new Vector(new[] { new Number(4), new Number(3) }) + var matrix2 = new Matrix(new[] + { + new Vector(new[] { new Number(9), new Number(2) }), + new Vector(new[] { new Number(4), new Number(3) }) }); var sub = new Sub(matrix1, matrix2); - var expected = new Matrix(new[] - { - new Vector(new[] { new Number(-3), new Number(1) }), - new Vector(new[] { new Number(-2), new Number(-2) }) + var expected = new Matrix(new[] + { + new Vector(new[] { new Number(-3), new Number(1) }), + new Vector(new[] { new Number(-2), new Number(-2) }) }); var result = sub.Execute();