/// <summary> /// Constructeur par défaut /// </summary> public CalculateurIhm() { _analyseur = new AnalyseurMath(); _listeFonction = new ListeFonction(); _variables = new Variables(); _evaluateur = new EvaluationVisitor(_variables, _listeFonction, TypeAngle.Degres); }
public object Evaluate() { EvaluationVisitor visitor = new EvaluationVisitor(); visitor.TaxFunction += TaxFunction; visitor.TaxWithPointFunction += TaxWithPointFunction; visitor.AnnualBonusTaxFunction += AnnualBonusTaxFunction; visitor.ForeignTaxFunction += ForeignTaxFunction; visitor.IsSalaryEndDateMonthEquelFunction += IsSalaryEndDateMonthEquelFunction; visitor.DoubleSalaryFunction += DoubleSalaryFunction; visitor.AnnualBonusForeignTaxFunction += AnnualBonusForeignTaxFunction; visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = parameters; //LogicalExpression.Create(Parse(expression)).Accept(visitor); //modify by wsl CommonTree ct = Parse(expression); //将表达式转化成LogicalExpression对象 LogicalExpression le = LogicalExpression.Create(ct); //计算表达式 le.Accept(visitor); return(visitor.Result); }
public void MathExpression_Arithmetic_Division_Throws_If_Denominator_Is_Zero() { var expression = MathExpressionBuilder.CreateArithmetic(ArithmeticOperator.Division, 42, 0); var visitor = new EvaluationVisitor(); var message = Assert.Throws<MathExpressionException>(() => visitor.Evaluate(expression, new MathExpressionContext())).Message; Assert.AreEqual("Cannot divide by zero.", message); }
public void MathExpression_Arithmetic_Expression_Throws_When_Evaluated_If_Operator_Is_Unknown() { var op = ((ArithmeticOperator)int.MaxValue); var expression = MathExpressionBuilder.CreateArithmetic(op, 42, 0); var visitor = new EvaluationVisitor(); var exception = Assert.Throws<MathExpressionException>(() => visitor.Evaluate(expression, new MathExpressionContext())); Assert.AreEqual("Encountered unknown arithmetic operator.", exception.Details); }
public CalculateurModel() { _analyseur = new AnalyseurMath(); _listeFonction = new ListeFonction(); _variables = new Variables(); _variables.Ajouter("PI", Math.PI); _variables.Ajouter("E", Math.E); _evaluateur = new EvaluationVisitor(_variables, _listeFonction, TypeAngle.Degres); }
public void EvaluationVisit(string input, int value) { var lexer = new Lexer(input); var parser = new Parser(lexer); INode tree = parser.Parse(); var visitor = new EvaluationVisitor(); int result = visitor.Visit(tree); Assert.That(result, Is.EqualTo(value)); }
public object Evaluate() { EvaluationVisitor visitor = new EvaluationVisitor(); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = parameters; LogicalExpression.Create(Parse(expression)).Accept(visitor); return(visitor.Result); }
public void LogicalExpressionShouldHandlePrecedence() { BinaryExpresssion expression = LogicalExpression.Create(Parse("3 * (5 + 2)")) as BinaryExpresssion; EvaluationVisitor visitor = new EvaluationVisitor(); visitor.Visit(expression); Assert.AreEqual(21, visitor.Result); Assert.AreEqual(40, new Expression("5 * 2 ^ 3").Evaluate()); }
public void LogicalExpressionShouldCreateInteger() { Value expression = LogicalExpression.Create(Parse("2")) as Value; Assert.IsNotNull(expression); Assert.AreEqual("2", expression.Text); EvaluationVisitor visitor = new EvaluationVisitor(); visitor.Visit(expression); Assert.AreEqual(2, visitor.Result); }
public void LogicalExpressionShouldCreateNot() { UnaryExpression expression = LogicalExpression.Create(Parse("not true")) as UnaryExpression; Assert.IsNotNull(expression); Assert.AreEqual(typeof(Value), expression.Expression.GetType()); EvaluationVisitor visitor = new EvaluationVisitor(); visitor.Visit(expression); Assert.AreEqual(false, visitor.Result); }
public void LogicalExpressionShouldCreateTimes() { BinaryExpresssion expression = LogicalExpression.Create(Parse("3 * 2")) as BinaryExpresssion; Assert.IsNotNull(expression); Assert.AreEqual(BinaryExpressionType.Times, expression.Type); EvaluationVisitor visitor = new EvaluationVisitor(); visitor.Visit(expression); Assert.AreEqual(6, visitor.Result); }
/// <inheritdoc/> public override void VisitObjectCreateExpression(ObjectCreateExpression syntax) { var type = _context.GetType(syntax.Type.ToString()); if (type != null) { var arguments = syntax.Arguments .Select(arg => { var visitor = new EvaluationVisitor(_context, _visitedMethods); arg.AcceptVisitor(visitor); return(visitor.Result); }) .ToArray(); Result = Activator.CreateInstance(type, arguments); } }
/// <inheritdoc/> public override void VisitInvocationExpression(InvocationExpression syntax) { var methodToEval = GetMethodToEval(syntax, _context, _declaringTypeFilter); if (methodToEval == null) { throw new NotSupportedException($"Encountered syntax that is unable to be evaluated: {syntax}"); } var arguments = syntax.Arguments .Select(arg => { var visitor = new EvaluationVisitor(_context, _visitedMethods); arg.AcceptVisitor(visitor); return(visitor.Result); }) .ToArray(); Result = _context.Invoke(methodToEval, arguments); }
/// <summary> /// Attempts to visit an <see cref="InvocationExpression"/> node /// </summary> /// <param name="syntax">The node to evaluate</param> /// <returns>True if it was visited, otherwise false</returns> public bool TryVisit(InvocationExpression syntax) { var methodToEval = GetMethodToEval(syntax, _context, _declaringTypeFilter); if (methodToEval != null) { var arguments = syntax.Arguments .Select(arg => { // allow all methods var visitor = new EvaluationVisitor(_context, _visitedMethods, t => true); arg.AcceptVisitor(visitor); return(visitor.Result); }) .ToArray(); Result = _context.Invoke(methodToEval, arguments); return(true); } return(false); }
public void MathExpression_Can_Evaluate_Arithmetic_Multiplication_Expression() { var expression = MathExpressionBuilder.CreateArithmetic(ArithmeticOperator.Multiplication, 21, 2); var visitor = new EvaluationVisitor(); Assert.AreEqual(42, visitor.Evaluate(expression, new MathExpressionContext())); }
public object Evaluate() { if (HasErrors()) { throw new EvaluationException(Error, ErrorException); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = Parameters; // Add a "null" parameter which returns null if configured to do so // Configured as an option to ensure no breaking changes for historical use if ((Options & EvaluateOptions.AllowNullParameter) == EvaluateOptions.AllowNullParameter && !visitor.Parameters.ContainsKey("null")) { visitor.Parameters["null"] = null; } // if array evaluation, execute the same expression multiple times if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters) { int size = -1; ParametersBackup = new Dictionary <string, object>(); foreach (string key in Parameters.Keys) { ParametersBackup.Add(key, Parameters[key]); } ParameterEnumerators = new Dictionary <string, IEnumerator>(); foreach (object parameter in Parameters.Values) { if (parameter is IEnumerable) { int localsize = 0; foreach (object o in (IEnumerable)parameter) { localsize++; } if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } foreach (string key in Parameters.Keys) { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } var results = new List <object>(); for (int i = 0; i < size; i++) { foreach (string key in ParameterEnumerators.Keys) { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } ParsedExpression.Accept(visitor); results.Add(visitor.Result); } return(results); } ParsedExpression.Accept(visitor); return(visitor.Result); }
private string DebugToString(EvaluationVisitor debugExpectedResults) { var sb = new StringBuilder(); bool allTrue = true; sb.AppendLine("SolverNode " + DebugOriginChain(4)); foreach (ScalarConstraint c in Constraints) { string result; try { bool isTrue = c.Accept(debugExpectedResults, Ig.nore); result = isTrue ? " Y " : " N [" + c.Expr.Accept(debugExpectedResults, Ig.nore).ToString(CultureInfo.InvariantCulture) + "]"; allTrue &= isTrue; } catch (NotSupportedException) { result = " ? "; } sb.Append(result); sb.AppendLine(c.ToString().WithParDepth(3)); } foreach (var c in ClosedVariables.Values) { sb.Append(" "); sb.AppendLine(c.ToString()); } sb.AppendLine(debugExpectedResults.DebugVariableValuesAsString()); return (allTrue ? "+" : "-") + sb; }
private static bool DebugIsTrueOrUnknown(AbstractConstraint c, EvaluationVisitor debugExpectedResults) { try { return c.Accept(debugExpectedResults, Ig.nore); } catch (NotSupportedException) { return true; } }
public static IEnumerable<SolverNode> SolverStep(IEnumerable<SolverNode> open, IDictionary<IVariable, VariableWithValue> previousValues, out SolverNode solutionOrNull, EvaluationVisitor debugExpectedResults = null) { double minRank = open.Min(cs => cs.Rank); SolverNode selected; if (debugExpectedResults == null) { selected = open.First(cs => cs.Rank <= minRank); } else { // The debugExpectedResults are intended to steer the solver only on // nodes that match the expected result! selected = open.FirstOrDefault(cs => cs.Constraints.All(c => DebugIsTrueOrUnknown(c, debugExpectedResults)) ); if (selected == null) { Debug.WriteLine("---- All open nodes do not match expected results:"); foreach (var node in open) { Debug.WriteLine(node.DebugToString(debugExpectedResults)); } throw new InvalidOperationException("No node matches expected results"); } } IEnumerable<SolverNode> expanded = selected.Expand( /*previousValues*/).ToArray(); if (!expanded.Any()) { // Dead node - register in dead nodes. DebugDeadNodes.Add(selected); // ???? if (debugExpectedResults != null) { Debug.WriteLine(".... Constraint state " + selected.DebugToString(debugExpectedResults)); } } //IEnumerable<SolverNode> newOpen = open.Except(selected).Concat(expandedSets); IEnumerable<SolverNode> newOpen = expanded.Concat(open.Except(selected)).ToArray(); // Not really correct: We should also check whether all anchor variables have // a single value. For the moment, in our tests, we live with this rough check. solutionOrNull = expanded.FirstOrDefault(cs => cs.IsSolved()); return newOpen; }
public void TestSquare2() { AbstractExpr p = new UnaryExpression(C(-0.5).C + V("a") + V("b") + V("c"), new Square()); // (a-0.5+b+c)² // = a²-0.5a+ab+ac + -0.5a+0.25-0.5b-0.5c + ab-0.5b+b²+bc + ac-0.5c+bc+c² // = a²-0.5a-0.5a+0.25 - 0.5b-0.5b+b² -0.5c-0.5c+c² + ab+ac+ab+bc+ac+bc // = a²-a+0.25 + b²-b + c²-c + ab+ac+ab+bc+ac+bc IPolynomial pa = Polynomial.CreatePolynomial(V("a"), 1, -1, 0.25); IPolynomial pb = Polynomial.CreatePolynomial(V("b"), 1, -1, 0); IPolynomial pc = Polynomial.CreatePolynomial(V("c"), 1, -1, 0); AbstractExpr e = V("a").C * V("b") + V("a").C * V("c") + V("b").C * V("a") + V("b").C * V("c") + V("c").C * V("a") + V("c").C * V("b"); AbstractExpr expected = pa.C + (pb.C + (pc.C + e)); IAbstractExpr fold = Fold(p); // We check by comparing values at a few places, as we do not know the exact // order of the pairs in e. for (double a = -3.5; a < 4; a++) { for (double b = -3.5; b < 4; b++) { for (double c = -3.5; c < 4; c++) { var visitor = new EvaluationVisitor(new Dictionary<IVariable, double> { { V("a"), a }, { V("b"), b }, { V("c"), c } }); double f = fold.Accept(visitor); double exp = expected.Accept(visitor); Assert.AreEqual(exp, f, 1e-5); } } } }
public void MathExpression_Can_Evaluate_Constant_Expression() { var expression = new ConstantExpression("pi", Math.PI); var visitor = new EvaluationVisitor(); Assert.AreEqual(Math.PI, visitor.Evaluate(expression, new MathExpressionContext())); }
public async System.Threading.Tasks.Task <object> EvaluateAsync() { if (HasErrors()) { throw new EvaluationException(Error); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunctionAsync += EvaluateFunctionAsync; visitor.EvaluateParameterAsync += EvaluateParameterAsync; visitor.Parameters = Parameters; // if array evaluation, execute the same expression multiple times if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters) { int size = -1; ParametersBackup = new Dictionary <string, object>(); foreach (string key in Parameters.Keys) { ParametersBackup.Add(key, Parameters[key]); } ParameterEnumerators = new Dictionary <string, IEnumerator>(); foreach (object parameter in Parameters.Values) { if (parameter is IEnumerable) { int localsize = 0; foreach (object o in (IEnumerable)parameter) { localsize++; } if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } foreach (string key in Parameters.Keys) { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } var results = new List <object>(); for (int i = 0; i < size; i++) { foreach (string key in ParameterEnumerators.Keys) { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } await ParsedExpression.AcceptAsync(visitor); results.Add(visitor.Result); } return(results); } await ParsedExpression.AcceptAsync(visitor); return(visitor.Result); }
public object Evaluate() { if (HasErrors()) { throw new EvaluationException(Error); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = Parameters; // if array evaluation, execute the same expression multiple times if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters) { int size = -1; ParametersBackup = new Dictionary <string, object>(); { // foreach(var key in Parameters.Keys) var __enumerator3 = (Parameters.Keys).GetEnumerator(); while (__enumerator3.MoveNext()) { var key = (string)__enumerator3.Current; { ParametersBackup.Add(key, Parameters[key]); } } } ParameterEnumerators = new Dictionary <string, IEnumerator>(); { // foreach(var parameter in Parameters.Values) var __enumerator4 = (Parameters.Values).GetEnumerator(); while (__enumerator4.MoveNext()) { var parameter = (object)__enumerator4.Current; { if (parameter is IEnumerable) { int localsize = 0; { // foreach(var o in (IEnumerable)parameter) var __enumerator7 = ((IEnumerable)parameter).GetEnumerator(); while (__enumerator7.MoveNext()) { var o = (object)__enumerator7.Current; { localsize++; } } } if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } } } { // foreach(var key in Parameters.Keys) var __enumerator5 = (Parameters.Keys).GetEnumerator(); while (__enumerator5.MoveNext()) { var key = (string)__enumerator5.Current; { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } } } var results = new List <object>(); for (int i = 0; i < size; i++) { { // foreach(var key in ParameterEnumerators.Keys) var __enumerator6 = (ParameterEnumerators.Keys).GetEnumerator(); while (__enumerator6.MoveNext()) { var key = (string)__enumerator6.Current; { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } } } ParsedExpression.Accept(visitor); results.Add(visitor.Result); } return(results); } ParsedExpression.Accept(visitor); return(visitor.Result); }
public object Evaluate() { if (HasErrors()) { throw new EvaluationException(Error); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, Options.NoCache()); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = Parameters; // if array evaluation, execute the same expression multiple times if (Options.IterateParameters()) { int size = -1; ParametersBackup = new Dictionary <string, object>(); foreach (string key in Parameters.Keys) { ParametersBackup.Add(key, Parameters[key]); } ParameterEnumerators = new Dictionary <string, IEnumerator>(); foreach (object parameter in Parameters.Values) { var enumerable = parameter as IEnumerable; if (enumerable != null) { int localsize = enumerable.Cast <object>().Count(); if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } foreach (string key in Parameters.Keys) { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } var results = new List <object>(); for (int i = 0; i < size; i++) { foreach (string key in ParameterEnumerators.Keys) { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } ParsedExpression.Accept(visitor); results.Add(visitor.Result); } return(results); } ParsedExpression.Accept(visitor); return(visitor.Result); }
public object Evaluate() { if (HasErrors()) { throw new EvaluationException(Error); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = Parameters; // if array evaluation, execute the same expression multiple times if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters) { int size = -1; ParametersBackup = new Dictionary<string, object>(); foreach (string key in Parameters.Keys) { ParametersBackup.Add(key, Parameters[key]); } ParameterEnumerators = new Dictionary<string, IEnumerator>(); foreach (object parameter in Parameters.Values) { if (parameter is IEnumerable) { int localsize = 0; foreach (object o in (IEnumerable)parameter) { localsize++; } if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } foreach (string key in Parameters.Keys) { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } var results = new List<object>(); for (int i = 0; i < size; i++) { foreach (string key in ParameterEnumerators.Keys) { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } ParsedExpression.Accept(visitor); results.Add(visitor.Result); } return results; } ParsedExpression.Accept(visitor); return visitor.Result; }
public void TestRewritePolynomialWithSinX() { IPolynomial p = P("x", 1, 2, 3, 4); AbstractExpr sinx = new UnaryExpression(V("x"), new Sin()).C; AbstractExpr expected = sinx * (sinx * (sinx + C(2)) + C(3)) + C(4); for (double x = -4; x < 5; x++) { IAbstractExpr result = Fold(p.Accept(new RewritingVisitor(V("x"), sinx), Ig.nore)); var eval = new EvaluationVisitor(V("x"), x); Assert.AreEqual(expected.Accept(eval), result.Accept(eval), 1e-5); } }
public void MathExpression_Can_Evaluate_Variable_Expression() { var expression = new VariableExpression("x"); var visitor = new EvaluationVisitor(); var context = new MathExpressionContext(); context.Define("x", 42D); Assert.AreEqual(42, visitor.Evaluate(expression, context)); }
private double f(double x) { EvaluationVisitor evaluationVisitor = new EvaluationVisitor(new Dictionary<Variable, double> { { _variable, x } }); return _expr.Accept(evaluationVisitor, Ig.nore); }
public void MathExpression_Can_Evaluate_Numeric_Expression() { var expression = new NumericExpression(42); var visitor = new EvaluationVisitor(); Assert.AreEqual(42, visitor.Evaluate(expression, new MathExpressionContext())); }
public static IDictionary<IVariable, VariableWithValue> Solve( IEnumerable<AbstractConstraint> solverConstraints, int loopLimit, IDictionary<IVariable, VariableWithValue> previousValues, int frameNo, EvaluationVisitor debugExpectedResults = null) { // Create initial open set IEnumerable<SolverNode> open = new[] { new SolverNode(solverConstraints, new Dictionary<IVariable, AbstractClosedVariable>(), null) }; DebugDeadNodes.Clear(); // Solver loop SolverNode solutionOrNull; do { if (!open.Any()) { DebugWriteDeadNodes(); throw new Exception("No solution found for frame " + frameNo + " - no more open nodes. Look into DebugDeadNodes to see nodes that did not mathc a rule."); } open = SolverStep(open, previousValues, out solutionOrNull, debugExpectedResults); //if (debugExpectedResults != null) { // foreach (var node in open.Except(DebugWrittenNodes)) { // string debugOutifDead = node.DebugToString(debugExpectedResults); // Debug.WriteLine(debugOutifDead); // } // DebugWrittenNodes.UnionWith(open); //} if (--loopLimit < 0) { DebugWriteDeadNodes(); throw new Exception("Cannot find solution for frame " + frameNo + " - loop limit exhausted"); } } while (solutionOrNull == null); solutionOrNull.ResolveBacksubstitutions(); Debug.WriteLine("++++ solution node ++++"); Debug.WriteLine(solutionOrNull); return solutionOrNull.ClosedVariables .Where(kvp => kvp.Value is VariableWithValue) .ToDictionary(kvp => kvp.Key, kvp => (VariableWithValue)kvp.Value); }
public object Evaluate(LogicalExpression InputLogicExpression = null) { if (InputLogicExpression == null) //evaluate with existing logical expression { if (HasErrors()) { throw new EvaluationException(Error); } if (ParsedExpression == null) { ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache); } } else //New evaluate { this.ParsedExpression = InputLogicExpression; } // if (ParsedExpression == null) { return(null); } var visitor = new EvaluationVisitor(Options); visitor.EvaluateFunction += EvaluateFunction; visitor.EvaluateParameter += EvaluateParameter; visitor.Parameters = Parameters; // if array evaluation, execute the same expression multiple times if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters) { int size = -1; ParametersBackup = new Dictionary <string, object>(); foreach (string key in Parameters.Keys) { ParametersBackup.Add(key, Parameters[key]); } ParameterEnumerators = new Dictionary <string, IEnumerator>(); foreach (object parameter in Parameters.Values) { if (parameter is IEnumerable) { int localsize = 0; foreach (object o in (IEnumerable)parameter) { localsize++; } if (size == -1) { size = localsize; } else if (localsize != size) { throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items"); } } } foreach (string key in Parameters.Keys) { var parameter = Parameters[key] as IEnumerable; if (parameter != null) { ParameterEnumerators.Add(key, parameter.GetEnumerator()); } } var results = new List <object>(); for (int i = 0; i < size; i++) { foreach (string key in ParameterEnumerators.Keys) { IEnumerator enumerator = ParameterEnumerators[key]; enumerator.MoveNext(); Parameters[key] = enumerator.Current; } ParsedExpression.Accept(visitor); results.Add(visitor.Result); } return(results); } ParsedExpression.Accept(visitor); //Record data ParsedExpression.objResult = visitor.Result; //Resturn data return(visitor.Result); }
public void TestSquare3() { AbstractExpr p = new UnaryExpression(C(1).C + V("a") + V("b"), new Square()); // (a+b+1)² // = a²+ab+a + ab+b²+b + a+b+1 // = a²+2a+1 + b²+2b + ab+ab IPolynomial pa = Polynomial.CreatePolynomial(V("a"), 1, 2, 1); IPolynomial pb = Polynomial.CreatePolynomial(V("b"), 1, 2, 0); AbstractExpr e = V("a").C * V("b") + V("a").C * V("b"); var expected = pa.C + (pb.C + e); IAbstractExpr fold = Fold(p); // We check by comparing values at a few places, as we do not know the exact // order of the pairs in e. for (double a = -3.5; a < 4; a++) { for (double b = -3.5; b < 4; b++) { var visitor = new EvaluationVisitor(new Dictionary<IVariable, double> { { V("a"), a }, { V("b"), b } }); double f = fold.Accept(visitor); double exp = expected.Accept(visitor); Assert.AreEqual(exp, f, 1e-5); } } }