user defined functions and constants
Hello guys, I'm working on a CAS for school mathematics in C++ and want to use GiNaC as a backend. In a CAS, the user has the option to define constants and functions, for example def a = 5 def f(x) = 2 * x + 3 def g(x, y) = x + 2y and then use these definitions like this f(3) + a * 4 - g(4, 5) Implement this for constants is pretty straight forward in GiNaC with the subs() method and an exmap which contains all defined constants. For the example above, it would be enough to insert a symbol with the value a together with the expression 5 into the map, then call subs() with it. Enable the same support for functions however seems very difficult, as functions can only defined at compile time with a macro. And using subs() seems a bit silly with functions: GiNaC::ex ex(f(2)); ex.subs(f(2) == f(2)); //what? This problem seems pretty common to me and I really wondered why GiNaC has no options to archive this. Did I miss something?
Hi,
On Sat, 21 Sep 2013 20:07:12 +0200, Gunnar Birke <gunnar.birke@online.de> said:
GB> the map, then call subs() with it. Enable the same support for GB> functions however seems very difficult, as functions can only GB> defined at compile time with a macro. And using subs() seems a GB> bit silly with functions: Unfortunately, I did not understand the difficulty which you were talking about. May be you was mislead by name of GiNaC::function class? For your example, you do not need o define f(x) and g(x,y) as GiNaC::function. They may be happily defined as an GiNaC::ex containing GiNaC::realsymbol x and y. I think, this will be sufficient for most of your purposes. Best wishes, Vladimir -- Vladimir V. Kisil email: kisilv@maths.leeds.ac.uk www: http://www.maths.leeds.ac.uk/~kisilv/ Book: Geometry of Mobius Transformations http://www.worldscientific.com/worldscibooks/10.1142/p835
participants (2)
-
Gunnar Birke
-
Vladimir V. Kisil