Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2007-12-02 00:03:24

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

C++ Mathematical scripting language

I've just about completed my C++ Mathematics scripting language.

What i mean by that, is that it can be used in the way of a user inputting an equation, and the program evalulates it. But rather than just evaluating it for the answer, it compiles it like a scripting language into a bytecode, which my math virtual machine runs through for evalulation -> i.e. one heck of alot faster if you are using my thing for a graphing application where the user inputs an equation to be graphed or sometihng like that where the expression has to be evaluated millions of times

It does full syntax checking and will output pretty informative errors for anything you try to compile, and also does some optimization on the expression itself

for example, if you have empty brackets it will remove them, and it will remove unnecesary brackets like ((3+4)) will go to (3+4) before starting the compiling etc.

For example, if you were to declare two variables you wanted to be able to use in the expresison 'x' and 'y' you can set them to whatever you like, and re-evaluate the expression extremely quickly through its compilation with new values of x and y

It does not use a stack based method wherein values can only be pushed and popped off the top of the stack, its all done in heap memory the reason being, even with a big equation, then amount of memory that is used is nominal so the execution time advantage far outweighs the memory usage advantage of stack based method.

so if you were to compile for example SIN(3+x!)/(4+(xy)^2)
in compilation, the memory heap will have the numerical constants pushed onto it so that always the first 3 items in heap will be
3,4,2

variables are stored in a seperate heap, so that when compiled this expresison would look like this (-> means where its stored in heap)

FAC v0 -> h3
ADD h0 h3 -> h4
MUL v0 v1 -> h5
POW h5 h2 -> h6
ADD h1 h6 -> h7
SIN h4 -> h8
DIV h8 h7

when actually compiled all the v0,h0 etc are stored as pointers for even faster access.

as an idea of how effecient it is:
If i compile the expression SIN(TAN(x^x)^x)^x^x
and run a loop of 1million setting x to the current loop value, and evaluate the expression. it takes my computer av. 1350ms.

If i have a pure C++ coded loop doing the same thing, it takes my computer av. 975ms

If i do a pure AS3 coded loop doing the same thing in Flash CS3, it takes the computer av. 1220ms (Note that AS3 in the flash virtual machine has JIT compilation like in Java so it runs much faster than it would otherwise) (in browser)

Theres still a few glitches im working through, and then some optimizations to make, but otherwise its complete.

---

At the moment the functions available are all trig functions (sin,cos,tan,sec,csc,cot + inverses, hyperbolics and inverse hyperbolics) logarithm of any base, natural logarithm, and natural exponential. the factorial uses a Gamma function approximation so that it can evalulate any real (except negative integres where its undefined)


as a C++ example you can have sometihng like

Mexp exp1;
double * xst = exp1.createVar('x',10);
exp1.parse("SIN(TAN(x^x)^x)^x^x");
for(int i = 0; i<1000000; i++) {
   *xst = (double)i;
   double val = exp1.evaluate();
}

Last edited by luca-deltodesco (2007-12-02 00:46:52)


The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

Board footer

Powered by FluxBB