Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed May 1, 2017
1 parent d72711d commit d83b817
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 91 deletions.
36 changes: 20 additions & 16 deletions xFunc.Maths/Analyzers/TypeAnalyzer.cs
Expand Up @@ -31,7 +31,7 @@ namespace xFunc.Maths.Analyzers
// todo: optimize
// todo: check ||
// todo: define/undefine!!!

public class TypeAnalyzer : IAnalyzer<ResultType>
{

Expand Down Expand Up @@ -163,9 +163,14 @@ public virtual ResultType Analyze(Del exp)
/// <returns>The result of analysis.</returns>
public virtual ResultType Analyze(Derivative exp)
{
if ((exp.ParametersCount >= 2 && exp.Arguments[1] is Variable) ||
(exp.ParametersCount >= 3 && exp.Arguments[2] is Number))
return exp.ParametersCount == 3 ? ResultType.Number : ResultType.Expression;
if (exp.ParametersCount == 1)
return ResultType.Expression;

if (exp.ParametersCount == 2 && exp.Arguments[1] is Variable)
return ResultType.Expression;

if (exp.ParametersCount == 3 && exp.Arguments[1] is Variable && exp.Arguments[2] is Number)
return ResultType.Number;

throw new ParameterTypeMismatchException();
}
Expand Down Expand Up @@ -423,17 +428,16 @@ public virtual ResultType Analyze(Number exp)
public virtual ResultType Analyze(Pow exp)
{
var leftResult = exp.Left.Analyze(this);
var rightResult = exp.Left.Analyze(this);
var rightResult = exp.Right.Analyze(this);
if (leftResult == ResultType.Undefined || rightResult == ResultType.Undefined)
return ResultType.Undefined;

if (leftResult == ResultType.Number)
{
if (rightResult == ResultType.Number)
return ResultType.Number;
if (rightResult == ResultType.ComplexNumber)
return ResultType.ComplexNumber;
}
if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;

if (leftResult == ResultType.ComplexNumber &&
(rightResult == ResultType.Number || rightResult == ResultType.ComplexNumber))
return ResultType.ComplexNumber;

throw new ParameterTypeMismatchException();
}
Expand Down Expand Up @@ -1409,7 +1413,7 @@ public virtual ResultType Analyze(GreaterOrEqual exp)
return ResultType.Undefined;

if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;
return ResultType.Boolean;

throw new ParameterTypeMismatchException();
}
Expand All @@ -1427,7 +1431,7 @@ public virtual ResultType Analyze(GreaterThan exp)
return ResultType.Undefined;

if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;
return ResultType.Boolean;

throw new ParameterTypeMismatchException();
}
Expand Down Expand Up @@ -1473,7 +1477,7 @@ public virtual ResultType Analyze(LessOrEqual exp)
return ResultType.Undefined;

if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;
return ResultType.Boolean;

throw new ParameterTypeMismatchException();
}
Expand All @@ -1491,7 +1495,7 @@ public virtual ResultType Analyze(LessThan exp)
return ResultType.Undefined;

if (leftResult == ResultType.Number && rightResult == ResultType.Number)
return ResultType.Number;
return ResultType.Boolean;

throw new ParameterTypeMismatchException();
}
Expand Down
28 changes: 1 addition & 27 deletions xFunc.Maths/Expressions/Derivative.cs
Expand Up @@ -35,15 +35,12 @@ public class Derivative : DifferentParametersExpression
/// <param name="args">The arguments.</param>
/// <param name="countOfParams">The count of parameters.</param>
/// <exception cref="System.ArgumentNullException"><paramref name="args"/> is null.</exception>
public Derivative(IExpression[] args, int countOfParams)
: base(args, countOfParams)
public Derivative(IExpression[] args, int countOfParams) : base(args, countOfParams)
{
if (args == null)
throw new ArgumentNullException(nameof(args));
if (args.Length != countOfParams)
throw new ArgumentException();
if (countOfParams == 2 && !(args[1] is Variable))
throw new ArgumentException(Resource.InvalidExpression);
}

/// <summary>
Expand Down Expand Up @@ -172,29 +169,6 @@ public IExpression Expression
/// </value>
public Number DerivativePoint => ParametersCount >= 3 ? (Number)m_arguments[2] : null;

/// <summary>
/// Gets or sets the arguments.
/// </summary>
/// <value>
/// The arguments.
/// </value>
public override IExpression[] Arguments
{
get
{
return base.Arguments;
}
set
{
if (value != null &&
((value.Length >= 2 && !(value[1] is Variable)) ||
(value.Length >= 3 && !(value[2] is Number))))
throw new ArgumentException(Resource.InvalidExpression);

base.Arguments = value;
}
}

/// <summary>
/// Gets the minimum count of parameters.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Programming/Equal.cs
Expand Up @@ -54,7 +54,7 @@ public override object Execute(ExpressionParameters parameters)
if (leftValueObject is bool leftBool && rightValueObject is bool rightBool)
return leftBool == rightBool;

throw new NotSupportedException();
throw new ResultIsNotSupportedException(this, leftValueObject, rightValueObject);
}

/// <summary>
Expand Down
46 changes: 32 additions & 14 deletions xFunc.Tests/Analyzers/TypeAnalyzerTest.cs
Expand Up @@ -136,39 +136,49 @@ public void TestAddTwoMatrixTest()
[Fact]
public void TestAddNumberVectorTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Number(1), new Vector(new[] { new Number(1) })));
var exp = new Add(new Number(1), new Vector(new[] { new Number(1) }));

TestException(exp);
}

[Fact]
public void TestAddVectorNumberTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Vector(new[] { new Number(1) }), new Number(1)));
var exp = new Add(new Vector(new[] { new Number(1) }), new Number(1));

TestException(exp);
}

[Fact]
public void TestAddNumberMatrixTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Number(1), new Matrix(new[] { new Vector(new[] { new Number(2) }) })));
var exp = new Add(new Number(1), new Matrix(new[] { new Vector(new[] { new Number(2) }) }));

TestException(exp);
}

[Fact]
public void TestAddMatrixNumberTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Matrix(new[] { new Vector(new[] { new Number(2) }) }), new Number(1)));
var exp = new Add(new Matrix(new[] { new Vector(new[] { new Number(2) }) }), new Number(1));

TestException(exp);
}

[Fact]
public void TestAddVectorMatrixTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Vector(new[] { new Number(1) }),
new Matrix(new[] { new Vector(new[] { new Number(2) }) })));
var exp = new Add(new Vector(new[] { new Number(1) }), new Matrix(new[] { new Vector(new[] { new Number(2) }) }));

TestException(exp);
}

[Fact]
public void TestAddMatrixVectorTest()
{
Assert.Throws<ParameterTypeMismatchException>(() => new Add(new Matrix(new[] { new Vector(new[] { new Number(2) }) }),
new Vector(new[] { new Number(1) })));
var exp = new Add(new Matrix(new[] { new Vector(new[] { new Number(2) }) }), new Vector(new[] { new Number(1) }));

TestException(exp);
}

[Fact]
Expand Down Expand Up @@ -622,7 +632,7 @@ public void TestLogNumber()
[Fact]
public void TestLogComplexNumber()
{
var exp = new Log(new ComplexNumber(8, 4), new ComplexNumber(2, 4));
var exp = new Log(new ComplexNumber(8, 3), new Number(2));

Test(exp, ResultType.ComplexNumber);
}
Expand Down Expand Up @@ -750,11 +760,19 @@ public void TestPowNumber()
[Fact]
public void TestPowComplexNumber()
{
var exp = new Pow(new Number(4), new ComplexNumber(2, 4));
var exp = new Pow(new ComplexNumber(2, 4), new Number(4));

Test(exp, ResultType.ComplexNumber);
}

[Fact]
public void TestPowNumberComplexNumber()
{
var exp = new Pow(new Number(4), new ComplexNumber(2, 4));

TestException(exp);
}

[Fact]
public void TestPowException()
{
Expand Down Expand Up @@ -1074,7 +1092,7 @@ public void TestMatrixVector()
{
var exp = new Matrix(new Vector[] { new Vector(2), new Vector(2) });

Test(exp, ResultType.Vector);
Test(exp, ResultType.Matrix);
}

[Fact]
Expand Down Expand Up @@ -2752,7 +2770,7 @@ public void TestEqualBoolean()
[Fact]
public void TestEqualException()
{
var exp = new Equal(new Variable("x"), new Bool(false));
var exp = new Equal(new ComplexNumber(2, 3), new Bool(false));

TestException(exp);
}
Expand All @@ -2770,7 +2788,7 @@ public void TestForNumber()
{
var exp = new For(null, null, new Bool(false), null);

Test(exp, ResultType.Boolean);
Test(exp, ResultType.Undefined);
}

[Fact]
Expand Down Expand Up @@ -3002,7 +3020,7 @@ public void TestWhileNumber()
{
var exp = new While(new Variable("x"), new Bool(false));

Test(exp, ResultType.Number);
Test(exp, ResultType.Undefined);
}

[Fact]
Expand Down
7 changes: 4 additions & 3 deletions xFunc.Tests/Expressions/DelegateExpressionTest.cs
Expand Up @@ -52,9 +52,10 @@ public void ExecuteTest3()
{
var uf1 = new UserFunction("func", new[] { new Variable("x") }, 1);
var func = new DelegateExpression(p => (double)p.Variables["x"] == 10 ? 0 : 1);
var funcs = new FunctionCollection();
funcs.Add(uf1, func);

var funcs = new FunctionCollection
{
{ uf1, func }
};
var uf2 = new UserFunction("func", new[] { new Number(12) }, 1);
var result = uf2.Execute(new ExpressionParameters(funcs));

Expand Down
13 changes: 0 additions & 13 deletions xFunc.Tests/Expressions/Programming/EqualTest.cs
Expand Up @@ -47,19 +47,6 @@ public void NumberVarEqualTest()

Assert.Equal(true, result);
}

[Fact]
public void NumberAndBoolVarEqualTest()
{
var parameters = new ParameterCollection()
{
new Parameter("x", 10),
new Parameter("y", false)
};
var equal = new Equal(new Variable("x"), new Variable("y"));

Assert.Throws<NotSupportedException>(() => (bool)equal.Execute(parameters));
}

[Fact]
public void BoolTrueEqualTest()
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Tests/Expressions/UnaryTest.cs
Expand Up @@ -19,7 +19,7 @@

namespace xFunc.Tests.Expressionss
{

public class UnaryTest
{

Expand Down
16 changes: 0 additions & 16 deletions xFunc.Tests/MathParserTest.cs
Expand Up @@ -175,22 +175,6 @@ public void ParseDerivWithOneParam()
Assert.Equal(expected, exp);
}

[Fact]
public void ParseDerivSecondParamIsNotVar()
{
var tokens = new List<IToken>
{
new FunctionToken(Functions.Derivative, 2),
new SymbolToken(Symbols.OpenBracket),
new VariableToken("x"),
new SymbolToken(Symbols.Comma),
new NumberToken(3),
new SymbolToken(Symbols.CloseBracket)
};

Assert.Throws<ArgumentException>(() => parser.Parse(tokens));
}

[Fact]
public void ParseDefine()
{
Expand Down

0 comments on commit d83b817

Please sign in to comment.