I would like to ask if there is a way in c# how to convert an input string and evaluate as a math equation (including complex numbers, referencing library, etc.) without any external library?
I’m looking for equivalent commands as compile() and eval() in Python.
The expression language contains some syntactic sugar which needs to be converted into proper code first. Try running your string through GH_ExpressionSyntaxWriter.RewriteAll() prior to calling Evaluate().
running the string through GH_ExpressionSyntaxWriter.RewriteAll() works well.
My intention is to evaluate the same expression multiple times but I´m struggling to use the GH_ExpressionSyntaxWriter.RewriteAll() method outside the loop. I have already implemented the CatchedSymbols() method but it´s quite slow. Is there a way to reduce the calculation time even more?
I’ll have a look, but note that the expression evaluator in Grasshopper 1.0 is pretty slow. It doesn’t compile the script into executable code, every statement is interpreted every time.
I’ve rewritten the code the way it was supposed to be used (with correct caching), but it really doesn’t matter all that much in terms of performance.
The trick with caching is that if you know you’ll run the same expression several times, you can set up the parser once ahead of time with cached symbols, and then call the Evaluate() overload which doesn’t take any arguments. It will use the cached symbols instead, shaving a few microseconds/milliseconds off each iteration.
private void RunScript(string expression, ref object result)
{
var expr = GH_ExpressionSyntaxWriter.RewriteAll(expression);
var parser = new GH_ExpressionParser();
parser.CacheSymbols(expr);
var c = new List<object>();
for (int i = 0; i < 10000; i++)
{
var z = new Complex(i, i);
parser.AddVariable("z", z);
c.Add(parser.Evaluate());
}
result = c;
}
How many inputs/outputs and how fast/slow are your computations?
In specific cases the C# component can be slower than .gha components when working with a lot of data, and you can win some performance by writing it as a gha plugin.
I think basically this is what you need. You’ll need to surround your string with some boilerplate code to turn it into valid c#.
This may actually be a lot harder than I thought since you’re working with complex values here. So your original expression “sin(z*z) + {2, 2}” would have to be converted into something like:
using System.Numerics;
namespace ScriptNamespace
{
public static class ScriptWrapper
{
public static object Execute(Complex z)
{
return Complex.Sin(z * z) + new Complex(2, 2);
}
}
}
And when I put it like that it hardly seems worth the trouble. I spend a long time getting this sort of stuff to work for gh2 expressions. I got there in the end, but boy was it hard.
I have a program to evaluate function. It first make a graph then make a heap/stack of operation and data. it is similar to Reverse Polish notation
I have an abstract class for stack, it is derived for function (+, -, *, /, % ) for function (Sin…), constants (numbers) and for variables
public abstract class FunctionOperationOnStack
{
/// <summary>
///
/// </summary>
/// <param name="arg_tab_stacks">Pile de valeurs</param>
/// <param name="arg_tab_variablesValues">Pile de valeurs des variables</param>
/// <param name="arg_positionOnStack">Index sur la pile</param>
/// <param name="tolerance"></param>
/// <returns></returns>
public abstract int StackOperation(ref double[] arg_tab_stacks, double[] arg_tab_variablesValues, ref int arg_positionOnStack, double tolerance);
}
I really appreciate your help @DavidRutten@TomTom@laurent_delrieu but I’m quite new in C#. I don’t think I’m able to create a whole new math parser library which works with complex numbers, etc. yet.
My initial intention was while rewriting the Chimpanzee plug-in in C# to add an equation input for the Mandelbrot set component for the users.
Currently the Mandelbrot set component without an equation input calculates 1M points for 100 iterations in less than 5s (old laptop).
It looks like it would be much efficient to reference some external math parser library or don’t implement the thought.