   -> Dynamic equations: how to?
  Heya!I''ve recently begun work on a program to browse strange attractor sets in C (no OOP here; it''s a personal preference I don''t wish to debate). The actual theory behind them, albeit simple, is unecessary for this post. Let''s just say they''re nice little pieces of eye-candy generated from a few formulas. Every "frame" a position is calculated and the pixel at that position grows brighter. Needless to say, the formulas are reused over and over and over.Given the large amount of sets my program can handle, I''m looking for ways to compress things a little. I also want the user to be able to add formulas, or maybe have the program generate random ones.At first, I had a huge switch statement in the middle of my pixel calculation loop. This was unefficient and very hard to read through. I later created a module that had a function for every set. An other function, given the ID of a set, returns a pointer to the function that generates that particular set. My loop becomes MUCH more readable and simpler.Now what I want to do is be able to generate the forumlas on the fly. Something kinda like...fn_X = "sin(%1) - %2";X = fn_X(X, Y);Of course, this doesn''t work, but you get the idea. Basically, I mean to construct an equation dynamically. The obvious solution is an equation parser and a form of mini-scripting language. But given the fact complex equations such assin(x + p[1]) - y * sin(y + p[2]) + sin(y + p[1]) - x * sin(x + p[2]);are commonplace (in fact, this is part of a set in the program) and are evaluated many thousands of times per second, I''m worried parsing a long string of text would be too slow to be acceptable. So is there any way to do like above? Dynamically create functions? And if not, any work-arounds? Ideas?Thanks 
    I would recommend looking for "expression parsers" on google.  also, the Spirit library at www.boost.org is nice. You can define everything inline, and it is flexible.  Look up on that  ;quote:Original post by Grizwald    I would recommend looking for "expression parsers" on google.  also, the Spirit library at www.boost.org is nice. You can define everything inline, and it is flexible.  Look up on that Hmm, though I''m grateful for the help, this doesn''t solve my problem...What I''d need to do is sort of "compile" the expression on the fly so that whever I tell it to call, say, f(x) = sin(x + p[1]) - y * sin(y + p[2]) + sin(y + p[1]) - x * sin(x + p[2]) it doesn''t have to parse the whole thing again, but rather just "does it". Sort of like if I had a function pointer to the already-hard-coded function only it wouldn''t be hard-coded in this case but "compiled" (well, ok, let''s start using the proper words for this, "parsed") in advance. From what I understand so far, both the Spirit Library and expression parsers would have to reparse the equation every time, which would be way, WAY too much of a problem for me...Unless, of course, the lack of caffeine made me overlook/misunderstand something... Finals can be killer... :x;read and digest;  This may be far-fetched and difficult, but it would work.Inside your program, write the string (this is a simplification)"#include "some_header.h"var func(var a, var b, ...) {    return #;}"to a file. (Replace # with the function, in string form)From inside your program, call a compiler on the new file, with linker options set to generate a dynamically linked library.Wait for the compiler to complete.Now link this library from inside your program.;  If you''re going to do that, then why not just generate assembly on the fly, write it to memory, and jump straight into that?  ;quote:Original post by petewood  read and digest  Unfortunately, expression templates are compile time constructs and not runtime, which doesn''t help RuneLancer to provide the level of dynamism he wants (unless he includes a compiler in his project, switches to C++ - he''s using C, remember - and spawns the resulting executable) especially when allowing users to enter formulas.The solution is to implement a scripting technology (ie, write an expression parser/interpreter) or switch to a language that is itself runtime interpreted as it''s likely to have an eval statement. If you choose the latter you have an abundance of C-like languages as options (Perl, Python, PHP, Lua...).If you choose the former then you need to tokenize input formulas, after validating them of course, then invoke the appropriate atomic operations with specified parameters and combine them as the expression indicates. It''s not hard, just mildly tedious, and there''s no point writing one from scratch except as a learning exercise.  ;  Hark, Greenspun''s 10th rule shall be fulfilled.  ;  Does this do it?This piece of code compiles an equation to 32 bits x86 code at runtime... that should be fast enough.In case I messed up the link, it's the code of the day of February 6th 2002 on www.flipcode.com[edited by - Kurioes on May 9, 2003 9:49:49 AM];  Build a tree from you equation! with nodes that are operations, and leafs that are numbers/symbols!I know this aint easy, but i think its the way the compiler does it... X = 15 * Y + 32 * 1/Y  =(15 * Y)+(32 *(1/Y))  =((15)*(Y))+((32)*((1)/(Y)))then start from the bottom and calculate upwards...Y = 5....    1/5 = 0.2... 32*0.2 = 6.4... 15/5 = 3... 3 + 6.4 = 9.4...  X = 9.4...all you need are the basic operators, and something that stores values...If you also need to solve the equations: X = 3 + 1/X it will bet harder, but i think is thould be possible to create an algo that moved every X over to one side...  other symbols, should be references to floats...  also remember that if your equation have more then one solution, you have to make everything abstract: your parsher must be able to understand X=Y :-) instead of X = 13... think that is hard...   but a simple equation parser should realy be no problem.it should also be possible to find patterns, where math rules can be applied.... After all processing, and simplification, you store the tree, and run it over and over... should be realy fast...[edited by - uncutno on May 9, 2003 10:10:35 AM]