Reputation: 539
i want to build a 3d plotting application. i have successfully built the drawer and it can draw any function. the drawer receives as an input the z and Sets it for each point.
i have the class 3Dpoint :
class 3Dpoint
{
public double x,y,z
public 3Dpoint(double a, double b)
{
this.x = a;
this.y = b;
this.z =0;
}
}
as the application goes on, it defines more and more points. for each point, it sets the x and y and then calculate the z :
for(int a ... )
for(int b ....)
{
3Dpoint p = new 3Dpoint(a,b);
p.z = Math.sin(p.x) * Math.Cos(p.y); // for instance
}
now instead of the z calculation to be permanent, i want to let the user set it, the input will be something like :
z= cos(x) ^ sqrt(sin(y)) + 6
now i have built a parser but it is very slow and it takes him plenty of time to done, and when i run it for each point it takes him forever to done.
the code : http://codeviewer.org/view/code:3bcc
got any idea how to improve my parser ? know any free online parser codes i can use?
tnx. :)
Upvotes: 1
Views: 220
Reputation: 29244
Check out Symbolism which claims to be
Automatic simplification of algebraic expressions in C#
with tests like:
var x = new Symbol("x");
var y = new Symbol("y");
var z = new Symbol("z");
Func<int, Integer> Int = (n) => new Integer(n);
AssertIsTrue(x + x == 2 * x);
AssertIsTrue(x + x == 2 * x);
AssertIsTrue(x + x + x == 3 * x);
AssertIsTrue(5 + x + 2 == 7 + x);
AssertIsTrue(3 + x + 5 + x == 8 + 2 * x);
AssertIsTrue(4 * x + 3 * x == 7 * x);
AssertIsTrue(x + y + z + x + y + z == 2 * x + 2 * y + 2 * z);
it should improve the speed by simplification.
PS. I have not used it myself.
Upvotes: 1
Reputation: 21072
You can check the parser you like the most if there is available for it ready to use syntax for parsing mathematical expressions. Since this is most common example explaining how parsing works, you should not have any problems with that. Take a look at Irony, Coco/R, GOLD, ANTLR (with already mentioned NCalc), LLLPG, or Sprache.
Not as mature as more popular parsers, even my NLT suite includes simple math calculator -- the input is text, the output is the number (outcome of math evaluation).
Upvotes: 0