My Opera is closing 3rd of March

The Place For Sharing

No boundaries

Mathematical Expression Class

, , ,

1. Problem
   How to calculate a mathematical expression.
   Some expression seem look same
   Ex : 1 + sin(2)*cos(sin(2))
        1 + sin(4)*cos(sin(1))
    They are same form and one of forms is 1 +  sin(x)*cos(sin(y))
   , are there any way to decreases calculate operation?

2. Solution
Let me show you my solution and this is it.
UML diagram.


Flow Chart.


Demo.


3. Usage
Class cExpression
Header : Expression.h
Library : ExpressionStaticLib.lib

Some class, method and data of classes in UML diagrams above is hidden. Because it is just necessary for building the library.

Advise
// calculate two expression without unknown parameter
cExpression* e1 = cExpression::CreateObject("2 + sin(3*1) - cos(3 + 1)","");
cExpression* e2 = cExpression::CreateObject("2 + sin(4*3) - cos(4 +3)","");
e1->Calculate(NULL);
e1->Calculate(NULL);

// calculate one expression with two parameter subset
cExpression* e = cExpression::CreateObject("2 + sin(x*y) - cos(x + y)","");
double aParam1[]={3,1};
double aParam2[]={4,3};
e->Calculate(aParam1);
e->Calculate(aParam2)
;

Let see, two way above they are same.
but The total time in 1st way is T1 = 2*( preprocessing time + calculating time) and
2nd way is T2 = preprocessing time + 2* calculating time.
the different is T1 - T2 = preprocessing time.
Preprocessing time is very much larger calculating time.

So, if you want to calculate a lot of expression and they are same form. (ex: draw graph of a function)
The best way is create one expression object which a representative expression string for all them and
calculate with each corresponding unknown parameter subset.

3. Links
Demo
Library

Dynamic function calling

Write a comment

New comments have been disabled for this post.