diff --git a/xFunc.Maths/Analyzers/TypeAnalyzer.cs b/xFunc.Maths/Analyzers/TypeAnalyzer.cs index 0df6edca7..155025418 100644 --- a/xFunc.Maths/Analyzers/TypeAnalyzer.cs +++ b/xFunc.Maths/Analyzers/TypeAnalyzer.cs @@ -31,7 +31,7 @@ namespace xFunc.Maths.Analyzers // todo: optimize // todo: check || // todo: define/undefine!!! - + public class TypeAnalyzer : IAnalyzer { @@ -163,9 +163,14 @@ public virtual ResultType Analyze(Del exp) /// The result of analysis. 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(); } @@ -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(); } @@ -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(); } @@ -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(); } @@ -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(); } @@ -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(); } diff --git a/xFunc.Maths/Expressions/Derivative.cs b/xFunc.Maths/Expressions/Derivative.cs index 3ed5d7d45..0ba22bfb6 100644 --- a/xFunc.Maths/Expressions/Derivative.cs +++ b/xFunc.Maths/Expressions/Derivative.cs @@ -35,15 +35,12 @@ public class Derivative : DifferentParametersExpression /// The arguments. /// The count of parameters. /// is null. - 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); } /// @@ -172,29 +169,6 @@ public IExpression Expression /// public Number DerivativePoint => ParametersCount >= 3 ? (Number)m_arguments[2] : null; - /// - /// Gets or sets the arguments. - /// - /// - /// The arguments. - /// - 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; - } - } - /// /// Gets the minimum count of parameters. /// diff --git a/xFunc.Maths/Expressions/Programming/Equal.cs b/xFunc.Maths/Expressions/Programming/Equal.cs index e0d0226e0..dc1f46e36 100644 --- a/xFunc.Maths/Expressions/Programming/Equal.cs +++ b/xFunc.Maths/Expressions/Programming/Equal.cs @@ -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); } /// diff --git a/xFunc.Tests/Analyzers/TypeAnalyzerTest.cs b/xFunc.Tests/Analyzers/TypeAnalyzerTest.cs index ed4022543..2cb51fadd 100644 --- a/xFunc.Tests/Analyzers/TypeAnalyzerTest.cs +++ b/xFunc.Tests/Analyzers/TypeAnalyzerTest.cs @@ -136,39 +136,49 @@ public void TestAddTwoMatrixTest() [Fact] public void TestAddNumberVectorTest() { - Assert.Throws(() => 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(() => 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(() => 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(() => 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(() => 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(() => 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] @@ -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); } @@ -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() { @@ -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] @@ -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); } @@ -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] @@ -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] diff --git a/xFunc.Tests/Expressions/DelegateExpressionTest.cs b/xFunc.Tests/Expressions/DelegateExpressionTest.cs index 91c8a3473..b75895afa 100644 --- a/xFunc.Tests/Expressions/DelegateExpressionTest.cs +++ b/xFunc.Tests/Expressions/DelegateExpressionTest.cs @@ -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)); diff --git a/xFunc.Tests/Expressions/Programming/EqualTest.cs b/xFunc.Tests/Expressions/Programming/EqualTest.cs index 90c394183..383e2a73b 100644 --- a/xFunc.Tests/Expressions/Programming/EqualTest.cs +++ b/xFunc.Tests/Expressions/Programming/EqualTest.cs @@ -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(() => (bool)equal.Execute(parameters)); - } [Fact] public void BoolTrueEqualTest() diff --git a/xFunc.Tests/Expressions/UnaryTest.cs b/xFunc.Tests/Expressions/UnaryTest.cs index 441d1ba34..25ac796d3 100644 --- a/xFunc.Tests/Expressions/UnaryTest.cs +++ b/xFunc.Tests/Expressions/UnaryTest.cs @@ -19,7 +19,7 @@ namespace xFunc.Tests.Expressionss { - + public class UnaryTest { diff --git a/xFunc.Tests/MathParserTest.cs b/xFunc.Tests/MathParserTest.cs index 178e48428..920bcf956 100644 --- a/xFunc.Tests/MathParserTest.cs +++ b/xFunc.Tests/MathParserTest.cs @@ -175,22 +175,6 @@ public void ParseDerivWithOneParam() Assert.Equal(expected, exp); } - [Fact] - public void ParseDerivSecondParamIsNotVar() - { - var tokens = new List - { - 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(() => parser.Parse(tokens)); - } - [Fact] public void ParseDefine() {