Hello, On Sun, Jan 28, 2007 at 09:31:18AM -0300, Charlls Quarra wrote:
I'm just trying out ginac for the first time. I have an expression with sumands of this type (in latex format): [snipped the expression]
so i want to collect terms that have the same coefficients of the form D[0..i times] (F)(q) * D[ 0.. j times] (F)(q) .... but the polynomial collect won't work.
Could you please post the complete example which illustrates this?
Initially i thought about substituting each D[ ] (F)(q) with a different symbol, then collect on the multivariate polynomials of these symbols and then finally substitute back the D[] expressions, however, I don't know how the collect call is supposed to receive the symbols that it must collect,
Reading the manual _does_ help: "Another useful representation of multivariate polynomials is as a univariate polynomial in one of the variables with the coefficients being polynomials in the remaining variables. The method `collect()' accomplishes this task: ex ex::collect(const ex & s, bool distributed = false); The first argument to `collect()' can also be a list of objects in which case the result is either a recursively collected polynomial, or a polynomial in a distributed form with terms like c*x1^e1*...*xn^en, as specified by the `distributed' flag. Note that the original polynomial needs to be in expanded form (for the variables concerned) in order for `collect()' to be able to find the coefficients properly."
the code looks like this:
ex jacobian = diff( q + dq_new*dt , q )*diff( p + dp_new*dt , p ) - diff( q + dq_new*dt , p )*diff( p + dp_new*dt , q ); cout << " the jacobian of the integrator is " << expand(jacobian) << endl; exmap diff_map , inv_diff_map; symbol foo[10]; diff_map[ F(q) ] = foo[0]; inv_diff_map[ foo[0] ] = F(q); diff_map[ F(q).diff(q) ] = foo[1]; inv_diff_map[ foo[1] ] = F(q).diff(q); diff_map[ F(q).diff(q,2) ] = foo[2]; inv_diff_map[ foo[2] ] = F(q).diff(q,2); diff_map[ F(q).diff(q,3) ] = foo[3]; inv_diff_map[ foo[3] ] = F(q).diff(q,3); diff_map[ F(q).diff(q,4) ] = foo[4]; inv_diff_map[ foo[4] ] = F(q).diff(q,4); ex rord = collect( jacobian.subs( diff_map ) , { foo[0] , foo[1] , foo[2] , foo[3] , foo[4] } ); // i know this is an error in C++, but this is the idea rord = rord.subs( inv_diff_map ); cout << "reordered we obtain " << rord << endl;
any thoughts on this?
I tnink this should do the job: ex rord = jacobian.expand().collect(lst(F(q), F(q).diff(q), F(q).diff(q, 2), F(q).diff(q, 3), F(q).diff(q, 4)), true /* treat the expression as Z[x,y,...] (as opposed to Z[x][y]...) */ ); Best regards, Alexei -- All science is either physics or stamp collecting.