Dear Dr. Kisil Thank you for your answer I'll carefully read. Pr. C. Prud'homme has hacked our GiNaC fork to let people use this kind of expression. https://github.com/feelpp/feelpp/commit/547c08b66e5b6019ab258477ab19a329cdf4... Now, the one that parse the string "(x<0.5)*1000+(x>0.5)*10:x:y" will find in the source file: f[0] = 10.0*(a[0]>5.0000000000000000e-01)+1000.0*(a[0]<5.0000000000000000e-01); Thank again for your quick answer ! All the best, VH 2015-04-22 18:50 GMT+02:00 Vladimir V. Kisil <kisilv@maths.leeds.ac.uk>:
Dear Vincent,
You are right that GiNaC expression do not admit boolean terms directly. However with your example you can proceed as follow (I do understand that your actual target is much more complicated):
1. Use the step function from GiNaC:
f=2*x*step(x)+3*x*step(-x).
[but for x=0 you need take care as step(0)=.5]
Of course, if you have a hundred points where the function changes the expression, this becomes much more complicated
2. Define your own function of three variables S(a,b,x) such that S(a,b,x)=1 for a<x<b and zero otherwise. GiNaC tutorial contains a demonstration how make a user-defined function. Then, any piece-wise function F will be a sum of terms f_i(x)*S(a_i,_b_i,x) where f_i is the expression for F on [a_i,b_i] (assuming disjoint partition.
3. Finally, you may derive your own class from GiNaC::basic to hold your piece-wise function (again, GiNaC tutorial gives an example). As I see it shall have two lists: one with points of the partition, another with the respective expression. Then you add your custom methods to operate this class.
The approaches are given in the order of increasing complexity, but the efficiently shall grow in the same direction.
Best wishes, Vladimir -- Vladimir V. Kisil http://www.maths.leeds.ac.uk/~kisilv/ Book: Geometry of Mobius Transformations http://goo.gl/EaG2Vu Software: Geometry of cycles http://moebinv.sourceforge.net/
On Wed, 22 Apr 2015 16:47:13 +0200, Vincent Huber < vincent.huber@cemosis.fr> said:
VH> Hello Dr. Kisil,
VH> The question from C. Trophime is not about evaluating the VH> function but to define it. Feel++ <http://www.feelpp.org> is a VH> Finite Element library that main goal is to solve the VH> variational problem a(u,v)=f(v) that arises from a lot of VH> physical problems.
VH> We use GiNaC to dynamically define the right hand side of our VH> problem, or our boundaries conditions and whatever can be VH> expressed with it.
VH> The question - I guess - can be explained like that : I would VH> like to define
VH> -
VH> f(x) = 2*x if x>0 -
VH> f(x) = 3*x if x<=0.
VH> Thus, basically, I would like to write
VH> ex MyEx = (x<=0)?3*x:2*x
VH> To my best knowledge, boolean expression are not handled in VH> GiNaC, isn’t it ?
VH> all the best,
VH> Vincent H. -- Docteur Ingénieur de recherche CeMoSiS VH> <http://www.cemosis.fr> - vincent.huber@cemosis.fr Tel: +33 (0)3 VH> 68 8*5 02 06* IRMA - 7, rue René Descartes 67 000 Strasbourg
VH> ---------------------------------------------------- VH> Alternatives:
VH> ---------------------------------------------------- VH> _______________________________________________ GiNaC-list VH> mailing list GiNaC-list@ginac.de VH> https://www.cebix.net/mailman/listinfo/ginac-list
-- Docteur Ingénieur de recherche CeMoSiS <http://www.cemosis.fr> - vincent.huber@cemosis.fr Tel: +33 (0)3 68 8*5 02 06* IRMA - 7, rue René Descartes 67 000 Strasbourg