Hello, I am currently trying the functionalities of GiNaC in order to translate some Maple code to C++ code, for a Matrix-using application. But I'm facing some difficulties for which some help could be very useful. First, I need to represent a symbolic vector and then functions of this vector. So far, I've been using 'n' distincts symbols for a vector of size 'n', which is certainly not the best way, especially for what I need to do afterwards (c.f the example below). Then, I need to compute the derivative of a function with respect to the previous vector, i.e. a gradient or a Jacobian matrix. With the "n distinct symbols" solution, I would do that by derivating n times, but it would be much more efficient if the whole gradient computation could be done in only one step: do you see any better way to do so within GiNaC? Finally, it happens that this vector is itself a function of time and I need sometimes to compute derivatives of functions of this vector with respect to time (in a more efficient way than just computing the aforementioned gradient or Jacobian and remultiplyling it by the time-derivative of the vector). I need therefore to express that a symbol (the members of the vector) is in fact a function of another symbol, with respect to which I can compute derivatives. How we do this in Maple is substitute symbols q[i] by functions q[i](t), differentiate with respect to t, then substitute back q[i](t) with q [i] and diff(q[i](t),t) with qdot[i] with the following code:
R=matrix(3,3);//+ initialization map(x->subs({seq(q[i](t)=q[i], i=1..NDDL), seq(diff(q[i](t), t)=qdot[i], i=1..NDDL)}, diff(subs({seq(q[i]=q[i](t), i=1..NDDL)}, x), t)), P)):
Doing something similar with GiNaC requires that we deal with user defined functions, but the "function" objects don't look like they are really made on this purpose: am I understanding correctly? We would define 'n' functions q[i](t) with derivatives defined as 'n' other functions qdot [i](t), something like that, but isn't there any simpler and more effective way? Many thanks for your time and help, A. Begault. -- View this message in context: http://www.nabble.com/Symbolic-computation-and-derivatives-tf3726576.html#a1... Sent from the Ginac - General mailing list archive at Nabble.com.
Dear Antoine,
First, I need to represent a symbolic vector and then functions of this vector. So far, I've been using 'n' distincts symbols for a vector of size 'n', which is certainly not the best way, especially for what I need to do afterwards (c.f the example below).
If your functions are or can be written in a co-ordinate independent way. I.e., as a tensor expression, you can use indexed objects for the components of the vector. If not, then creating n different symbols seems to be the only way.
Then, I need to compute the derivative of a function with respect to the previous vector, i.e. a gradient or a Jacobian matrix. With the "n distinct symbols" solution, I would do that by derivating n times, but it would be much more efficient if the whole gradient computation could be done in only one step: do you see any better way to do so within GiNaC?
I do not really see an easier way, I'm afraid. In the case that your function can be written as a tensor expression, it would be nice if GiNaC was able to differentiate with respect to a symbol carrying indices. Unfortunately it can only differentiate with respect to symbols at the moment.
Finally, it happens that this vector is itself a function of time and I need sometimes to compute derivatives of functions of this vector with respect to time (in a more efficient way than just computing the aforementioned gradient or Jacobian and remultiplyling it by the time-derivative of the vector). I need therefore to express that a symbol (the members of the vector) is in fact a function of another symbol, with respect to which I can compute derivatives.
I think you would need to use the same way as you used with Maple.
Doing something similar with GiNaC requires that we deal with user defined functions, but the "function" objects don't look like they are really made on this purpose: am I understanding correctly? We would define 'n' functions q[i](t) with derivatives defined as 'n' other functions qdot [i](t), something like that, but isn't there any simpler and more effective way?
The functions q[i] would not need to know their derivative (you are talking about functions as in the class GiNaC::function, I think). In that case they would simply return something that looks like D[0](q[i])(t). You could also consider the idea of making i another parameter of your function. That way you would have q(i, t), which could easily be more convenient to create. Good luck, Chris
participants (2)
-
Antoine Begault
-
Chris.Dams@mi.infn.it