[GiNaC-list] Piecewise function

Vladimir V. Kisil kisilv at maths.leeds.ac.uk
Wed Apr 22 18:50:10 CEST 2015

	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:


  [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

  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 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 at 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 at 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 at ginac.de
    VH> https://www.cebix.net/mailman/listinfo/ginac-list

More information about the GiNaC-list mailing list