public override BaseExpression VisitExponentiation(Exponentiation exponentiation) { // (g(x)^f(x)) = g(x)^(f(x) - 1) * (g(x) * f'(x) * log(g(x)) + f(x) * g'(x)) var g = exponentiation.Root; var f = exponentiation.Power; return(new Multiplication( new Exponentiation( g, new Subtraction(f, Real.One) ), new Addition( new Multiplication( new Multiplication( g, this.VisitExpressionNode(f) ), new Logarithm( Real.E, g ) ), new Multiplication( f, this.VisitExpressionNode(g) ) ) )); }
public void TestExponentiation() { IFunctionRegistry registry = new MockFunctionRegistry(); AstBuilder builder = new AstBuilder(registry); Operation operation = builder.Build(new List <Token>() { new Token() { Value = 2, TokenType = TokenType.Integer }, new Token() { Value = '^', TokenType = TokenType.Operation }, new Token() { Value = 3, TokenType = TokenType.Integer } }); Exponentiation exponentiation = (Exponentiation)operation; Assert.AreEqual(new IntegerConstant(2), exponentiation.Base); Assert.AreEqual(new IntegerConstant(3), exponentiation.Exponent); }
public void CalculateTest(double firstValue, double secondValue, double expected) { var calculator = new Exponentiation(); var actualResult = calculator.ExecuteOperation(firstValue, secondValue); Assert.AreEqual(expected, actualResult); }
public void ExponentiateOneNumberToAnother() { var result = new Exponentiation(new Number(1), new Number(2)); Assert.That(result.TechnicalDescription, Is.EqualTo("pow(1, 2)")); Assert.That(result.Description, Is.EqualTo("1 ^ 2")); Assert.That(result.InnerDescription, Is.EqualTo("(1 ^ 2)")); }
public void ExponentiateANumberToAnAdditionResult() { var subResult = new Addition(new Number(2), new Number(3)); var result = new Exponentiation(new Number(1), subResult); Assert.That(result.TechnicalDescription, Is.EqualTo("pow(1, add(2, 3))")); Assert.That(result.Description, Is.EqualTo("1 ^ (2 + 3)")); Assert.That(result.InnerDescription, Is.EqualTo("(1 ^ (2 + 3))")); }
public void ExponentiateAnAdditionResultToANumber() { var subResult = new Addition(new Number(1), new Number(2)); var result = new Exponentiation(subResult, new Number(3)); Assert.That(result.TechnicalDescription, Is.EqualTo("pow(add(1, 2), 3)")); Assert.That(result.Description, Is.EqualTo("(1 + 2) ^ 3")); Assert.That(result.InnerDescription, Is.EqualTo("((1 + 2) ^ 3)")); }
public Operation Optimize(Operation operation, IFunctionRegistry functionRegistry, IConstantRegistry constantRegistry) { if (!operation.DependsOnVariables && operation.IsIdempotent && operation.GetType() != typeof(IntegerConstant) && operation.GetType() != typeof(FloatingPointConstant)) { double result = executor.Execute(operation, functionRegistry, constantRegistry); return(new FloatingPointConstant(result)); } else { if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; addition.Argument1 = Optimize(addition.Argument1, functionRegistry, constantRegistry); addition.Argument2 = Optimize(addition.Argument2, functionRegistry, constantRegistry); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction substraction = (Subtraction)operation; substraction.Argument1 = Optimize(substraction.Argument1, functionRegistry, constantRegistry); substraction.Argument2 = Optimize(substraction.Argument2, functionRegistry, constantRegistry); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; multiplication.Argument1 = Optimize(multiplication.Argument1, functionRegistry, constantRegistry); multiplication.Argument2 = Optimize(multiplication.Argument2, functionRegistry, constantRegistry); if ((multiplication.Argument1.GetType() == typeof(FloatingPointConstant) && ((FloatingPointConstant)multiplication.Argument1).Value == 0.0) || (multiplication.Argument2.GetType() == typeof(FloatingPointConstant) && ((FloatingPointConstant)multiplication.Argument2).Value == 0.0)) { return(new FloatingPointConstant(0.0)); } } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; division.Dividend = Optimize(division.Dividend, functionRegistry, constantRegistry); division.Divisor = Optimize(division.Divisor, functionRegistry, constantRegistry); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation division = (Exponentiation)operation; division.Base = Optimize(division.Base, functionRegistry, constantRegistry); division.Exponent = Optimize(division.Exponent, functionRegistry, constantRegistry); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; IList <Operation> arguments = function.Arguments.Select(a => Optimize(a, functionRegistry, constantRegistry)).ToList(); function.Arguments = arguments; } return(operation); } }
public void Calcpr( double firstElement, double secondElement, double expected) { var calculator = new Exponentiation(); var actualResult = calculator.Calculate(firstElement, secondElement); Assert.AreEqual(expected, actualResult, 0.01); }
public void CalculateTest( double first, double second, double expected) { var calculator = new Exponentiation(); var actualResult = calculator.Calculate(first, second); Assert.AreEqual(expected, actualResult); }
public void ZeroToThePowerOfMinusTen() { Number number = new Number(0); Number power = new Number(-10); double expected = double.PositiveInfinity; Exponentiation exponentiation = new Exponentiation(number, power); double actual = exponentiation.Operation(); Assert.AreEqual(expected, actual); }
public void TenSquared() { Number number = new Number(10); Number power = new Number(2); int expected = 100; Exponentiation exponentiation = new Exponentiation(number, power); double actual = exponentiation.Operation(); Assert.AreEqual(expected, actual); }
public void MinusTenToThePowerOfMinusTwo() { Number number = new Number(-10); Number power = new Number(-2); double expected = 0.01; Exponentiation exponentiation = new Exponentiation(number, power); double actual = exponentiation.Operation(); Assert.AreEqual(expected, actual); }
public void BinExpTest() { const long value = 21; const long power = 13; var result = Exponentiation.BinExp(value, power); const long expectedResult = 154472377739119461; Assert.IsTrue(result == expectedResult, "Binary exponentiation has an incorrect result"); }
public void ZeroToThePowerOfTen() { Number number = new Number(0); Number power = new Number(10); int expected = 0; Exponentiation exponentiation = new Exponentiation(number, power); double actual = exponentiation.Operation(); Assert.AreEqual(expected, actual); }
private TreeViewItem CreateTreeViewItem(Operation operation) { TreeViewItem item = new TreeViewItem(); item.Header = GetLabelText(operation); if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; item.Items.Add(CreateTreeViewItem(multiplication.Argument1)); item.Items.Add(CreateTreeViewItem(multiplication.Argument2)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; item.Items.Add(CreateTreeViewItem(addition.Argument1)); item.Items.Add(CreateTreeViewItem(addition.Argument2)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; item.Items.Add(CreateTreeViewItem(addition.Argument1)); item.Items.Add(CreateTreeViewItem(addition.Argument2)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; item.Items.Add(CreateTreeViewItem(division.Dividend)); item.Items.Add(CreateTreeViewItem(division.Divisor)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; item.Items.Add(CreateTreeViewItem(exponentiation.Base)); item.Items.Add(CreateTreeViewItem(exponentiation.Exponent)); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; foreach (Operation argument in function.Arguments) { item.Items.Add(CreateTreeViewItem(argument)); } } return(item); }
public void ModExpTest() { const long value = 4; const long power = 13; const long mod = 497; var result = Exponentiation.ModExp(value, power, mod); const long expectedResult = 445; Assert.IsTrue(result == expectedResult, "Modular exponentiation has an incorrect result"); }
/// <summary> /// <para> /// To determine the sequence of actions: execution priority is high /// </para> /// <para> /// High priority operations: negation, parentheses, number /// </para> /// </summary> private UniversalOperation ParsingAnExpression_HighPriority() { UniversalOperation result; if (MatchSearch('-')) { result = new Negation(ParsingAnExpression_LowPriority()); } else if (MatchSearch('√')) { result = new RootExtraction(ParsingAnExpression_HighPriority()); } else if (MatchSearch('S') && MatchSearch('q') && MatchSearch('r')) //Workaround { result = new Exponentiation(ParsingAnExpression_HighPriority(), new Number(2)); } else if (MatchSearch('(')) { result = ParsingAnExpression_LowPriority(); if (!MatchSearch(')')) { System.Console.WriteLine("Missing ')'"); } } else { //Parsing numbers double val = 0.0; int startPosition = pos; //Find out the size of the number to parse while (pos < currentExpression.Length && (char.IsDigit(currentExpression[pos]) || currentExpression[pos] == ',')) { pos++; } //Attempt to parse a number try { val = double.Parse(currentExpression.Substring(startPosition, pos - startPosition)); } catch (System.Exception e) { System.Console.WriteLine("The number is not parsed..."); System.Console.WriteLine(e); } result = new Number(val); } return(result); }
public static Tensor Power(Tensor argument, Tensor power) { //TODO improve Complex^Complex if (argument is Complex a && power is Complex p) { var result = Exponentiation.ExponentiateIfPossible(a, p); if (result != null) { return(result); } } if (TensorUtils.IsOne(power)) { return(argument); } if (TensorUtils.IsZero(power) || TensorUtils.IsOne(argument)) { return(Complex.One); } if (TensorUtils.IsZero(argument)) { return(Complex.Zero); } if (argument is Product) { if (TensorUtils.IsInteger(power) //case (2*x)**(y) //todo replace with isPositiveNumerical(argument.get(0)) || (argument.Size == 2 && TensorUtils.IsRealPositiveNumber(argument[0]))) { Tensor[] scalars = ((Product)argument).GetAllScalars(); if (scalars.Length > 1) { ITensorBuilder pb = argument.GetBuilder();//creating product builder foreach (Tensor t in scalars) { pb.Put(t.Pow(power));//TODO refactor for performance } return(pb.Build()); } } } if (argument is Power) { return(argument[0].Pow(argument[1].Multiply(power))); } return(new Power(argument, power)); }
public Operation Optimize(Operation operation, IFunctionRegistry functionRegistry) { if (!operation.DependsOnVariables && operation.GetType() != typeof(UnitNumberConstant) && operation.GetType() != typeof(FloatingPointConstant)) { var result = executor.Execute(operation, functionRegistry); if (result.DataType == DataType.Number) { return(new FloatingPointConstant((double)result.Value)); } else { return(new UnitNumberConstant((UnitNumber)result.Value)); } } else { if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; addition.Argument1 = Optimize(addition.Argument1, functionRegistry); addition.Argument2 = Optimize(addition.Argument2, functionRegistry); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction substraction = (Subtraction)operation; substraction.Argument1 = Optimize(substraction.Argument1, functionRegistry); substraction.Argument2 = Optimize(substraction.Argument2, functionRegistry); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; multiplication.Argument1 = Optimize(multiplication.Argument1, functionRegistry); multiplication.Argument2 = Optimize(multiplication.Argument2, functionRegistry); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; division.Dividend = Optimize(division.Dividend, functionRegistry); division.Divisor = Optimize(division.Divisor, functionRegistry); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation division = (Exponentiation)operation; division.Base = Optimize(division.Base, functionRegistry); division.Exponent = Optimize(division.Exponent, functionRegistry); } return(operation); } }
private void GetVariables(Operation operation, List <Variable> variables) { if (operation.DependsOnVariables) { if (operation.GetType() == typeof(Variable)) { variables.Add((Variable)operation); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; GetVariables(addition.Argument1, variables); GetVariables(addition.Argument2, variables); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; GetVariables(multiplication.Argument1, variables); GetVariables(multiplication.Argument2, variables); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction substraction = (Subtraction)operation; GetVariables(substraction.Argument1, variables); GetVariables(substraction.Argument2, variables); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; GetVariables(division.Dividend, variables); GetVariables(division.Divisor, variables); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; GetVariables(exponentiation.Base, variables); GetVariables(exponentiation.Exponent, variables); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; foreach (Operation argument in function.Arguments) { GetVariables(argument, variables); } } } }
static void Main() { Exponentiation exponentiation = new Exponentiation(); Console.WriteLine("\n\t\t<<<<<<<<< Exponentiation calculator >>>>>>>>>\n\n"); Console.Write("Please enter the base value: "); int exponentiationtheBaseValue = int.Parse(Console.ReadLine()); exponentiation.SetBaseValue(exponentiationtheBaseValue); Console.Write("\nPlease enter the exponent value: "); int exponentiationtheExponentValue = int.Parse(Console.ReadLine()); exponentiation.SetExponentValue(exponentiationtheExponentValue); Console.WriteLine($"\n\nThe result for your calculation is: {exponentiation.IntegerPower()}"); }
public Operation Optimize(Operation operation) { if (!operation.DependsOnVariables && operation.GetType() != typeof(IntegerConstant) && operation.GetType() != typeof(FloatingPointConstant)) { double result = executor.Execute(operation); return(new FloatingPointConstant(result)); } else { if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; addition.Argument1 = Optimize(addition.Argument1); addition.Argument2 = Optimize(addition.Argument2); } else if (operation.GetType() == typeof(Substraction)) { Substraction substraction = (Substraction)operation; substraction.Argument1 = Optimize(substraction.Argument1); substraction.Argument2 = Optimize(substraction.Argument2); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; multiplication.Argument1 = Optimize(multiplication.Argument1); multiplication.Argument2 = Optimize(multiplication.Argument2); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; division.Dividend = Optimize(division.Dividend); division.Divisor = Optimize(division.Divisor); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation division = (Exponentiation)operation; division.Base = Optimize(division.Base); division.Exponent = Optimize(division.Exponent); } return(operation); } }
private void GenerateMethodBody(ILGenerator generator, Operation operation, IFunctionRegistry functionRegistry) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; generator.Emit(OpCodes.Ldc_I4, constant.Value); generator.Emit(OpCodes.Conv_R8); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; generator.Emit(OpCodes.Ldc_R8, constant.Value); } else if (operation.GetType() == typeof(Variable)) { Type dictionaryType = typeof(IDictionary <string, double>); Variable variable = (Variable)operation; Label throwExceptionLabel = generator.DefineLabel(); Label returnLabel = generator.DefineLabel(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Callvirt, typeof(FormulaContext).GetProperty("Variables").GetGetMethod()); generator.Emit(OpCodes.Ldstr, variable.Name); generator.Emit(OpCodes.Ldloca_S, (byte)0); generator.Emit(OpCodes.Callvirt, dictionaryType.GetMethod("TryGetValue", new Type[] { typeof(string), typeof(double).MakeByRefType() })); generator.Emit(OpCodes.Ldc_I4_0); generator.Emit(OpCodes.Ceq); generator.Emit(OpCodes.Brtrue_S, throwExceptionLabel); generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Br_S, returnLabel); generator.MarkLabel(throwExceptionLabel); generator.Emit(OpCodes.Ldstr, string.Format("The variable \"{0}\" used is not defined.", variable.Name)); generator.Emit(OpCodes.Newobj, typeof(VariableNotDefinedException).GetConstructor(new Type[] { typeof(string) })); generator.Emit(OpCodes.Throw); generator.MarkLabel(returnLabel); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; GenerateMethodBody(generator, multiplication.Argument1, functionRegistry); GenerateMethodBody(generator, multiplication.Argument2, functionRegistry); generator.Emit(OpCodes.Mul); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; GenerateMethodBody(generator, addition.Argument1, functionRegistry); GenerateMethodBody(generator, addition.Argument2, functionRegistry); generator.Emit(OpCodes.Add); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; GenerateMethodBody(generator, addition.Argument1, functionRegistry); GenerateMethodBody(generator, addition.Argument2, functionRegistry); generator.Emit(OpCodes.Sub); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; GenerateMethodBody(generator, division.Dividend, functionRegistry); GenerateMethodBody(generator, division.Divisor, functionRegistry); generator.Emit(OpCodes.Div); } else if (operation.GetType() == typeof(Modulo)) { Modulo modulo = (Modulo)operation; GenerateMethodBody(generator, modulo.Dividend, functionRegistry); GenerateMethodBody(generator, modulo.Divisor, functionRegistry); generator.Emit(OpCodes.Rem); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentation = (Exponentiation)operation; GenerateMethodBody(generator, exponentation.Base, functionRegistry); GenerateMethodBody(generator, exponentation.Exponent, functionRegistry); generator.Emit(OpCodes.Call, typeof(Math).GetMethod("Pow")); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; GenerateMethodBody(generator, unaryMinus.Argument, functionRegistry); generator.Emit(OpCodes.Neg); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, lessThan.Argument1, functionRegistry); GenerateMethodBody(generator, lessThan.Argument2, functionRegistry); generator.Emit(OpCodes.Blt_S, ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, lessOrEqualThan.Argument1, functionRegistry); GenerateMethodBody(generator, lessOrEqualThan.Argument2, functionRegistry); generator.Emit(OpCodes.Ble_S, ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, greaterThan.Argument1, functionRegistry); GenerateMethodBody(generator, greaterThan.Argument2, functionRegistry); generator.Emit(OpCodes.Bgt_S, ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, greaterOrEqualThan.Argument1, functionRegistry); GenerateMethodBody(generator, greaterOrEqualThan.Argument2, functionRegistry); generator.Emit(OpCodes.Bge_S, ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, equal.Argument1, functionRegistry); GenerateMethodBody(generator, equal.Argument2, functionRegistry); generator.Emit(OpCodes.Beq_S, ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; Label ifLabel = generator.DefineLabel(); Label endLabel = generator.DefineLabel(); GenerateMethodBody(generator, notEqual.Argument1, functionRegistry); GenerateMethodBody(generator, notEqual.Argument2, functionRegistry); generator.Emit(OpCodes.Beq, ifLabel); generator.Emit(OpCodes.Ldc_R8, 1.0); generator.Emit(OpCodes.Br_S, endLabel); generator.MarkLabel(ifLabel); generator.Emit(OpCodes.Ldc_R8, 0.0); generator.MarkLabel(endLabel); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); Type funcType = GetFuncType(functionInfo.NumberOfParameters); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Callvirt, typeof(FormulaContext).GetProperty("FunctionRegistry").GetGetMethod()); generator.Emit(OpCodes.Ldstr, function.FunctionName); generator.Emit(OpCodes.Callvirt, typeof(IFunctionRegistry).GetMethod("GetFunctionInfo", new Type[] { typeof(string) })); generator.Emit(OpCodes.Callvirt, typeof(FunctionInfo).GetProperty("Function").GetGetMethod()); generator.Emit(OpCodes.Castclass, funcType); for (int i = 0; i < functionInfo.NumberOfParameters; i++) { GenerateMethodBody(generator, function.Arguments[i], functionRegistry); } generator.Emit(OpCodes.Call, funcType.GetMethod("Invoke")); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
private Expression GenerateMethodBody(Operation operation, ParameterExpression contextParameter, IFunctionRegistry functionRegistry) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; double value = constant.Value; return(Expression.Constant(value, typeof(double))); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; return(Expression.Constant(constant.Value, typeof(double))); } else if (operation.GetType() == typeof(Variable)) { Variable variable = (Variable)operation; Func <string, FormulaContext, double> getVariableValueOrThrow = PrecompiledMethods.GetVariableValueOrThrow; return(Expression.Call(null, getVariableValueOrThrow.GetMethodInfo(), Expression.Constant(variable.Name), contextParameter)); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; Expression argument1 = GenerateMethodBody(multiplication.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(multiplication.Argument2, contextParameter, functionRegistry); return(Expression.Multiply(argument1, argument2)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry); return(Expression.Add(argument1, argument2)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; Expression argument1 = GenerateMethodBody(addition.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(addition.Argument2, contextParameter, functionRegistry); return(Expression.Subtract(argument1, argument2)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; Expression dividend = GenerateMethodBody(division.Dividend, contextParameter, functionRegistry); Expression divisor = GenerateMethodBody(division.Divisor, contextParameter, functionRegistry); return(Expression.Divide(dividend, divisor)); } else if (operation.GetType() == typeof(Modulo)) { Modulo modulo = (Modulo)operation; Expression dividend = GenerateMethodBody(modulo.Dividend, contextParameter, functionRegistry); Expression divisor = GenerateMethodBody(modulo.Divisor, contextParameter, functionRegistry); return(Expression.Modulo(dividend, divisor)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentation = (Exponentiation)operation; Expression @base = GenerateMethodBody(exponentation.Base, contextParameter, functionRegistry); Expression exponent = GenerateMethodBody(exponentation.Exponent, contextParameter, functionRegistry); return(Expression.Call(null, typeof(Math).GetRuntimeMethod("Pow", new Type[] { typeof(double), typeof(double) }), @base, exponent)); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; Expression argument = GenerateMethodBody(unaryMinus.Argument, contextParameter, functionRegistry); return(Expression.Negate(argument)); } else if (operation.GetType() == typeof(And)) { And and = (And)operation; Expression argument1 = Expression.NotEqual(GenerateMethodBody(and.Argument1, contextParameter, functionRegistry), Expression.Constant(0.0)); Expression argument2 = Expression.NotEqual(GenerateMethodBody(and.Argument2, contextParameter, functionRegistry), Expression.Constant(0.0)); return(Expression.Condition(Expression.And(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Or)) { Or and = (Or)operation; Expression argument1 = Expression.NotEqual(GenerateMethodBody(and.Argument1, contextParameter, functionRegistry), Expression.Constant(0.0)); Expression argument2 = Expression.NotEqual(GenerateMethodBody(and.Argument2, contextParameter, functionRegistry), Expression.Constant(0.0)); return(Expression.Condition(Expression.Or(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; Expression argument1 = GenerateMethodBody(lessThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(lessThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.LessThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; Expression argument1 = GenerateMethodBody(lessOrEqualThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(lessOrEqualThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.LessThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; Expression argument1 = GenerateMethodBody(greaterThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(greaterThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.GreaterThan(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; Expression argument1 = GenerateMethodBody(greaterOrEqualThan.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(greaterOrEqualThan.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.GreaterThanOrEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; Expression argument1 = GenerateMethodBody(equal.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(equal.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.Equal(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; Expression argument1 = GenerateMethodBody(notEqual.Argument1, contextParameter, functionRegistry); Expression argument2 = GenerateMethodBody(notEqual.Argument2, contextParameter, functionRegistry); return(Expression.Condition(Expression.NotEqual(argument1, argument2), Expression.Constant(1.0), Expression.Constant(0.0))); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); Type funcType; Type[] parameterTypes; Expression[] arguments; if (functionInfo.IsDynamicFunc) { funcType = typeof(DynamicFunc <double, double>); parameterTypes = new Type[] { typeof(double[]) }; Expression[] arrayArguments = new Expression[function.Arguments.Count]; for (int i = 0; i < function.Arguments.Count; i++) { arrayArguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry); } arguments = new Expression[1]; arguments[0] = NewArrayExpression.NewArrayInit(typeof(double), arrayArguments); } else { funcType = GetFuncType(functionInfo.NumberOfParameters); parameterTypes = (from i in Enumerable.Range(0, functionInfo.NumberOfParameters) select typeof(double)).ToArray(); arguments = new Expression[functionInfo.NumberOfParameters]; for (int i = 0; i < functionInfo.NumberOfParameters; i++) { arguments[i] = GenerateMethodBody(function.Arguments[i], contextParameter, functionRegistry); } } Expression getFunctionRegistry = Expression.Property(contextParameter, "FunctionRegistry"); ParameterExpression functionInfoVariable = Expression.Variable(typeof(FunctionInfo)); Expression funcInstance; if (!functionInfo.IsOverWritable) { funcInstance = Expression.Convert( Expression.Property( Expression.Call( getFunctionRegistry, typeof(IFunctionRegistry).GetRuntimeMethod("GetFunctionInfo", new Type[] { typeof(string) }), Expression.Constant(function.FunctionName)), "Function"), funcType); } else { funcInstance = Expression.Constant(functionInfo.Function, funcType); } return(Expression.Call( funcInstance, funcType.GetRuntimeMethod("Invoke", parameterTypes), arguments)); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
public double Execute(Operation operation, IFunctionRegistry functionRegistry, IConstantRegistry constantRegistry, IDictionary <string, double> variables) { if (operation == null) { throw new ArgumentNullException("operation"); } if (operation.GetType() == typeof(IntegerConstant)) { IntegerConstant constant = (IntegerConstant)operation; return(constant.Value); } else if (operation.GetType() == typeof(FloatingPointConstant)) { FloatingPointConstant constant = (FloatingPointConstant)operation; return(constant.Value); } else if (operation.GetType() == typeof(Variable)) { Variable variable = (Variable)operation; double value; bool variableFound = variables.TryGetValue(variable.Name, out value); if (variableFound) { return(value); } else { throw new VariableNotDefinedException(string.Format("The variable \"{0}\" used is not defined.", variable.Name)); } } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; return(Execute(multiplication.Argument1, functionRegistry, constantRegistry, variables) * Execute(multiplication.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; return(Execute(addition.Argument1, functionRegistry, constantRegistry, variables) + Execute(addition.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; return(Execute(addition.Argument1, functionRegistry, constantRegistry, variables) - Execute(addition.Argument2, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; return(Execute(division.Dividend, functionRegistry, constantRegistry, variables) / Execute(division.Divisor, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Modulo)) { Modulo division = (Modulo)operation; return(Execute(division.Dividend, functionRegistry, constantRegistry, variables) % Execute(division.Divisor, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; return(Math.Pow(Execute(exponentiation.Base, functionRegistry, constantRegistry, variables), Execute(exponentiation.Exponent, functionRegistry, constantRegistry, variables))); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; return(-Execute(unaryMinus.Argument, functionRegistry, constantRegistry, variables)); } else if (operation.GetType() == typeof(And)) { And and = (And)operation; var operation1 = Execute(and.Argument1, functionRegistry, constantRegistry, variables) != 0; var operation2 = Execute(and.Argument2, functionRegistry, constantRegistry, variables) != 0; return((operation1 && operation2) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Or)) { Or or = (Or)operation; var operation1 = Execute(or.Argument1, functionRegistry, constantRegistry, variables) != 0; var operation2 = Execute(or.Argument2, functionRegistry, constantRegistry, variables) != 0; return((operation1 || operation2) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; return((Execute(lessThan.Argument1, functionRegistry, constantRegistry, variables) < Execute(lessThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; return((Execute(lessOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) <= Execute(lessOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; return((Execute(greaterThan.Argument1, functionRegistry, constantRegistry, variables) > Execute(greaterThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; return((Execute(greaterOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) >= Execute(greaterOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; return((Execute(equal.Argument1, functionRegistry, constantRegistry, variables) == Execute(equal.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; return((Execute(notEqual.Argument1, functionRegistry, constantRegistry, variables) != Execute(notEqual.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0); } else if (operation.GetType() == typeof(Function)) { Function function = (Function)operation; FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); double[] arguments = new double[functionInfo.IsDynamicFunc ? function.Arguments.Count : functionInfo.NumberOfParameters]; for (int i = 0; i < arguments.Length; i++) { arguments[i] = Execute(function.Arguments[i], functionRegistry, constantRegistry, variables); } return(Invoke(functionInfo.Function, arguments)); } else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
private Expression ParseRaiseExpression(TokenSet followers) //^ ensures followers[this.currentToken] || this.currentToken == Token.EndOfFile; { TokenSet followerOrRaise = followers|Token.Raise; Expression result = this.ParseUnaryExpression(followerOrRaise); if (this.currentToken == Token.Raise) { SourceLocationBuilder slb = new SourceLocationBuilder(result.SourceLocation); this.GetNextToken(); Expression operand2 = this.ParseRaiseExpression(followerOrRaise); slb.UpdateToSpan(operand2.SourceLocation); result = new Exponentiation(result, operand2, slb); } //^ assume followers[this.currentToken] || this.currentToken == Token.EndOfFile; return result; }
protected virtual Exponentiation VisitExponentiation(Exponentiation node) { return(VisitSyntaxNode(node) as Exponentiation); }
public VariableCalcurator Execute(Operation operation, IFunctionRegistry functionRegistry, IDictionary <string, VariableCalcurator> variables) { VariableCalcurator.defaultVariables = variables; if (operation == null) { throw new ArgumentNullException("operation"); } /* * if (operation.GetType() == typeof(IntegerConstant)) * { * IntegerConstant constant = (IntegerConstant)operation; * return constant.Value; * } * else if (operation.GetType() == typeof(FloatingPointConstant)) * { * FloatingPointConstant constant = (FloatingPointConstant)operation; * return constant.Value; * } * else*/ if (operation.GetType() == typeof(VariableCalcurator)) { VariableCalcurator variable = (VariableCalcurator)operation; /* * if (variable.DataType == DataType.Literal) * return new VariableCalcurator(DataType.Literal, variable.Literal()); * else if(variable.DataType == DataType.Identifier) * { * bool variableFound = variables.ContainsKey(variable.Literal()); * * if (variableFound) * return variables[variable.Literal()]; * * else * return variable; * throw new VariableNotDefinedException(string.Format("The variable \"{0}\" used is not defined.", variable.Literal())); * } */ return(variable); } else if (operation.GetType() == typeof(Multiplication)) { Multiplication multiplication = (Multiplication)operation; return(Execute(multiplication.Argument1, functionRegistry, variables).GetInstance() * Execute(multiplication.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Addition)) { Addition addition = (Addition)operation; return(Execute(addition.Argument1, functionRegistry, variables).GetInstance() + Execute(addition.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Subtraction)) { Subtraction addition = (Subtraction)operation; return(Execute(addition.Argument1, functionRegistry, variables).GetInstance() - Execute(addition.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Division)) { Division division = (Division)operation; return(Execute(division.Dividend, functionRegistry, variables).GetInstance() / Execute(division.Divisor, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Modulo)) { Modulo division = (Modulo)operation; return(Execute(division.Dividend, functionRegistry, variables).GetInstance() % Execute(division.Divisor, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Exponentiation)) { Exponentiation exponentiation = (Exponentiation)operation; return(new VariableCalcurator((float)Math.Pow(Execute(exponentiation.Base, functionRegistry, variables).GetInstance().Float(), Execute(exponentiation.Exponent, functionRegistry, variables).GetInstance().Float()))); } else if (operation.GetType() == typeof(UnaryMinus)) { UnaryMinus unaryMinus = (UnaryMinus)operation; return(-Execute(unaryMinus.Argument, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(LessThan)) { LessThan lessThan = (LessThan)operation; return(Execute(lessThan.Argument1, functionRegistry, variables).GetInstance() < Execute(lessThan.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(LessOrEqualThan)) { LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation; return(Execute(lessOrEqualThan.Argument1, functionRegistry, variables).GetInstance() <= Execute(lessOrEqualThan.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(GreaterThan)) { GreaterThan greaterThan = (GreaterThan)operation; return(Execute(greaterThan.Argument1, functionRegistry, variables).GetInstance() > Execute(greaterThan.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(GreaterOrEqualThan)) { GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation; return(Execute(greaterOrEqualThan.Argument1, functionRegistry, variables).GetInstance() >= Execute(greaterOrEqualThan.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Equal)) { Equal equal = (Equal)operation; return(Execute(equal.Argument1, functionRegistry, variables).GetInstance() == Execute(equal.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(NotEqual)) { NotEqual notEqual = (NotEqual)operation; return(Execute(notEqual.Argument1, functionRegistry, variables).GetInstance() != Execute(notEqual.Argument2, functionRegistry, variables).GetInstance()); } else if (operation.GetType() == typeof(Index)) { Index index = (Index)operation; return(Index(Execute(index.Argument1, functionRegistry, variables), Execute(index.Argument2, functionRegistry, variables), variables)); } else if (operation.GetType() == typeof(Substitution)) { Substitution substitution = (Substitution)operation; return(Substitute(Execute(substitution.Argument1, functionRegistry, variables), Execute(substitution.Argument2, functionRegistry, variables), variables)); } /* * else if (operation.GetType() == typeof(Function)) * { * Function function = (Function)operation; * * FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName); * * double[] arguments = new double[functionInfo.IsDynamicFunc ? function.Arguments.Count : functionInfo.NumberOfParameters]; * for (int i = 0; i < arguments.Length; i++) * arguments[i] = Execute(function.Arguments[i], functionRegistry, variables); * * return Invoke(functionInfo.Function, arguments); * } */ else { throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation"); } }
public long Encrypt(long message, Key publicKey) { return Exponentiation.ModPow(message, publicKey.Value, publicKey.Modulus); }
protected override Exponentiation VisitExponentiation(Exponentiation node) { visitedExponentiation = true; Assert.IsTrue(node.Value == "e0"); return(base.VisitExponentiation(node)); }
public long Decrypt(long encryptMessage) { return Exponentiation.ModPow(encryptMessage, PrivateKey.Value, PrivateKey.Modulus); }