static void DefineVariableInput(UndefinedVariableTreeNode vDefinition, string expressionInput, CalculationContext context) { Token[] tokens = lexer.Tokenize(expressionInput); TreeNode tree = parser.Construct(tokens); FinishedFunction function = linker.BuildFunction(tree, context, new string[0]); double value = treeCalculator.Calculate(function); context.VariableTable.AssignItem(vDefinition.Name, value); }
static void CalculateInput(string expressionInput, CalculationContext context) { Token[] tokens = lexer.Tokenize(expressionInput); TreeNode tree = parser.Construct(tokens); FinishedFunction function = linker.BuildFunction(tree, context, new string[0]); double value = treeCalculator.Calculate(function); Console.WriteLine(String.Format(" = {0}", value.ToString("G", System.Globalization.CultureInfo.InvariantCulture))); }
public PostfixFunction Convert(FinishedFunction function) { MemoryStream stream = new MemoryStream(); ConvertRecursion(function.TopNode, function.FunctionTable, stream); stream.WriteByte((byte)PostfixFunction.PostfixCommand.End); return(new PostfixFunction(stream.GetBuffer(), function)); }
public FinishedFunction FindDifferential(FinishedFunction func, int index) { if (index < 0 || index >= func.ParameterCount) { throw new ArgumentException(String.Format("There is no parameter with index {0}", index)); } TreeNode topNode = func.TopNode; TreeNode differentialTopNode = MakeDifferential(topNode, index); return(new FinishedFunction(differentialTopNode, func.VariableTable, func.FunctionTable, func.ParameterCount)); }
static void TestInput(string input, CalculationContext context) { string funcName = input.Substring(6).Trim(' '); FinishedFunction function = context.FunctionTable[funcName]; PostfixFunction postfixFunction = translator.Convert(function); Console.WriteLine("Testing functionality: "); TestCalculator(postfixCalculator, postfixFunction, context.VariableTable, function.ParameterCount, 10, 1, true); TestCalculator(treeCalculator, function, context.VariableTable, function.ParameterCount, 10, 1, true); Console.WriteLine("Testing speed: "); TestCalculator(postfixCalculator, postfixFunction, context.VariableTable, function.ParameterCount, 100000, 100, false); TestCalculator(treeCalculator, function, context.VariableTable, function.ParameterCount, 100000, 100, false); }
static void OptimizeInput(string input, CalculationContext context) { string funcName = input.Substring(10).Trim(' '); FinishedFunction unoptimized = context.FunctionTable[funcName]; FinishedFunction optimized = optimizer.OptimizeWithTable(unoptimized); Console.WriteLine("Unoptimized: "); Console.WriteLine(unoptimized.TopNode); visualizer.VisualizeAsTree(unoptimized.TopNode, context); Console.WriteLine("Optimized: "); Console.WriteLine(optimized.TopNode); visualizer.VisualizeAsTree(optimized.TopNode, context); }
static void FindDifferentialInput(string input, int parameterIndex, CalculationContext context) { string funcName = input.Substring(4).Trim(' '); FinishedFunction function = context.FunctionTable[funcName]; DefaultDifferentiator differentiator = new DefaultDifferentiator(); FinishedFunction df = differentiator.FindDifferential(function, parameterIndex); Console.WriteLine(df.TopNode); visualizer.VisualizeAsTree(df.TopNode, context); FinishedFunction odf = optimizer.OptimizeWithTable(df); Console.WriteLine("Optimized: "); Console.WriteLine(odf.TopNode); visualizer.VisualizeAsTree(odf.TopNode, context); }
// so, we "insert" copy of function tree in or original tree. // Also, we replace all parameters with trees specified in original tree TreeNode InsertFunction(UndefinedFunctionTreeNode fTree, FinishedFunction function) { if (fTree.Parameters.Length != function.ParameterCount) { throw new Exception(String.Format("Invalid parameter count in \"{0}\" call (expected {1}, actual {2})", fTree.Name, function.ParameterCount, fTree.Parameters.Length)); } TreeNode parent = fTree.Parent; TreeNode[] parameters = fTree.Parameters; TreeNode newTree = function.TopNode.Clone(parent); newTree = ReplaceParametersWithTreeNodes(newTree, parameters); return(newTree); }
static void PrintLibrarySummary(CalculationContext context) { Console.WriteLine("Functions: "); foreach (string name in context.FunctionTable.AllItemNames) { FinishedFunction function = context.FunctionTable[name]; Console.WriteLine(" {0}({1} argument{3}) = {2}", name, function.ParameterCount, function.TopNode, (function.ParameterCount != 1) ? "s" : String.Empty); } Console.WriteLine("Variables: "); foreach (string name in context.VariableTable.AllItemNames) { double variable = context.VariableTable[name]; Console.WriteLine(" {0} = #[{1}] = {2}", name, context.VariableTable.GetIndex(name), variable.ToString("G", System.Globalization.CultureInfo.InvariantCulture)); } }
static void DefineFunctionInput(UndefinedFunctionTreeNode fDefinition, string expressionInput, CalculationContext context) { string[] parameterNames = new string[fDefinition.Parameters.Length]; for (int i = 0; i < parameterNames.Length; ++i) { if (fDefinition.Parameters[i] is UndefinedVariableTreeNode vTreeNode) { parameterNames[i] = vTreeNode.Name; } else { throw new Exception("Invalid function definition"); } } Token[] tokens = lexer.Tokenize(expressionInput); TreeNode tree = parser.Construct(tokens); FinishedFunction function = linker.BuildFunction(tree, context, parameterNames); context.FunctionTable.AssignItem(fDefinition.Name, function); }
static void AssignFunction(CalculationContext context, Operation operation) { FinishedFunction funcToAssign; switch (operation) { case UnaryOperation uOperation: funcToAssign = new FinishedFunction( new UnaryOperationTreeNode(uOperation, new FunctionParameterTreeNode(0)), null, null, 1); break; case BinaryOperation bOperation: funcToAssign = new FinishedFunction( new BinaryOperationTreeNode(bOperation, new FunctionParameterTreeNode(0), new FunctionParameterTreeNode(1)), null, null, 2); break; default: return; } context.FunctionTable.AssignNewItem(operation.FunctionName, funcToAssign); }
public void SetFinishedEvent(FinishedFunction fe) { this.FinishedEvent += fe; }
private static void ProccessCompleteCallback(object a_sender, RunWorkerCompletedEventArgs a_e) { FinishedFunction?.Invoke(); }