Skip to content

Commit

Permalink
#150 add, sub
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 20, 2016
1 parent 255d8be commit 40b698c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
9 changes: 9 additions & 0 deletions xFunc.Maths/Expressions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions xFunc.Maths/Expressions/Sub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions xFunc.Tests/Expressions/Maths/AddTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down
56 changes: 42 additions & 14 deletions xFunc.Tests/Expressions/Maths/SubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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();

Expand Down

0 comments on commit 40b698c

Please sign in to comment.