Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changable runtime equation

Status
Not open for further replies.

ajikoe

Programmer
Apr 16, 2004
71
0
0
ID
hello.

I would like to call a function which has an equation. But during the execution of main program I would like to modify the equation inside the function.

How can I do that if the function I built must be inside a text file, for example the function is define in a unit file?

Thanks
pujo
 
Maybe you could create a deferred class Equation :

Code:
class OneParameterEquation
{
public:
  virtual int solve (int x) const = 0;
};

class Equation1 : public OneParameterEquation
{
public:
  int solve (int x) const
  {
     //TODO : implement it
     return 0;
  }
}

class Equation2 : public OneParameterEquation
{
public:
  int solve (int x) const
  {
     //TODO : implement it
     return 0;
  }
}

Thanks to polymorphism, you can then write things like :
Code:
int main ()
{
  OneParameterEquation* equation = new Equation1;
  int result = equation->solve (5);

  //switch to another equation :
  OneParameterEquation* equation = new Equation2;
  int result = equation->solve (20);

  return 0;
}

Does it do what you want?

--
Globos
 
Thanks for your help,

I want to make an equation which change not only the variable but more complex such as:
from
y = sin(x)

to

y = cos(x)-x*x

and more changing during time execution

can you help me

Thanks
Pujo
 
In my point of view you has to make an "interpeter" function which understands those expressions you wish to use and then does the calculation if needed.

According to my knowledge it's not possible to compile at run-time without a serious setup and then execute the result. It should be possible but there's a whole lot of possible troubles and the equation should nevertheless be "understood" by the program writing the code and then wer'e back on square 1 again, it's really the "translating" code that's the thing.

Do you wish to calculate on the equations or do you only need a "converter"? (text to text so to speak)

Totte
Keep making it perfect and it will end up broken.
 

Hello,

The scenario is like this:
I have a function inside a unit.
for example : y = x+x;

I use that function to get y by inputing x value.

Then during a runtime:
I change that function inside my units and it become like this:
y = x-x;

I will use that new function to get y by inputing x value.

And my program will keep trying to change the functions model until some condition met.

so I build the converter (it is done) and also I need to calculate the dynamic equations.

Pujo

 
Well, if you already has the decoding done there should be no problem in adding the calculating to that.

By the way y = x - x will always be y = 0 so i take that you mean y = x - z.

Is the problem to read the values from text to whatever format needed?

Does you have trouble in converting "Y = 123.45 + 567.89" or where lies the problem?

What is the syntax in your equations?

Totte
Keep making it perfect and it will end up broken.
 
yes your right :
y = x-x y will always be 0.
and my program keep trying change it until stoping sequence initiated.

the problem is that I use units and that function for example :
y = x-2x;
, but when I change the equation inside my units
for example:
y = x-5x;
, and try to call my function I found that it still executes y = x-2x; not the new one.

This is the problem.

Pujo
 
Ahhhhh....i do believe that i understand what the problem is.

As i read it you make a program which does something. In the task you use a equation and you need to change that equation.

You then changes that equation in the source code but the program which is running does not use the new equation, am i right?

If this is the case you MUST stop the execution, compile the altered sourcecode and run it.

Even if that equation resides in a .DLL-file that .DLL file should be re-loaded every time to make it possible to change the equation in run-time and i doubt that it's possible without serious CPU load.

So either stop the program and re-compile it and start it or make a text-equation-solver function where you can enter the equation and the solver/decoder will do the calculation in run-time, based upon the text entered. This will of course be a bottleneck in the program but could be usable in teh development process.

Totte
Keep making it perfect and it will end up broken.
 
You right about my problem....
If I choose to stop the program and recompile it I think it is not a good Idea since I will run my program and change the equation thousand times.

Do you know something about equation decoder?

Sincerely Yours,
Pujo
 
Well, to decode the equation could be quite simple...or very complex, all depending on what you need of functions.

If it's simple, i.e. uses '+', '-', '*' and '/' and only a few variables and steps but several steps and complex functions could be a nightmare.

If we assumes that the literal 'X', 'Y' (and 'Z'?) is certain variables in the program and you only uses '+' and '-' and no more than 1 step (i.e. "X=X+Y", "Z=X+Y", X=Y+Y" and so forth) it should be fairly simple to make a text-translator doing the job.

Totte
Keep making it perfect and it will end up broken.
 
There are many ways for doing a full function evaluator:
it can be as complex as a compiler.
Yoo can see it in 2 parts
1.-
First you need to check the lexical components that are allowed for your equation (using Automatums is recomended).
Then you need to analiza the sintax.
Finally you should check the types, I mean size of vectors, matrix, etc.

1.5-
Replace the vars with their correspondings values

2.-
The other part consists in the evaluator, which u need to convert the function in a polac format. (postfix, prefix).

Once you have done all this steps in your program, function or class, you set the property for the evaluator to the new function to evaluate, and the do the same with the new function as Totte said.


--- LastCyborg ---
 
Sounds to me as though you need a program such as matlab, maple or the like.

Writing even a simple converter is tricky. But check the net, there might be stuff that is downloadable for free (or for a price).

You might look at the GSL if it might suit your needs (I think it will) Gnu Scientific Library.

The complete range of subject areas covered by the library includes, here is a list of stuff it can do

Complex Numbers Roots of Polynomials Special Functions
Vectors and Matrices Permutations Sorting
BLAS Support Linear Algebra Eigensystems
Fast Fourier Transforms Quadrature Random Numbers
Quasi-Random Sequences Random Distributions Statistics
Histograms N-Tuples Monte Carlo Integration
Simulated Annealing Differential Equations Interpolation
Numerical Differentiation Chebyshev Approximation Series Acceleration
Discrete Hankel Transforms Root-Finding Minimization
Least-Squares Fitting Physical Constants IEEE Floating-Point

Sorry about the format, but it was cut and pasted. :)

And good luck, it is not an easy task in any way you look at it.


o__
,_.>/ _
(_)_\(_)_______
..speed is good
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top