[GiNaC-list] Substitution troubles

Jeremy Jay dinkumator at gmail.com
Tue Feb 24 14:50:46 CET 2009


On Tue 24 Feb 2009 - 09:53AM, Alexei Sheplyakov wrote:
> Although there's nothing wrong about your code, such substitution won't work.
> Rewrite the `poly' expression as
> 
> symbol A("A");
> ex poly((A-delta(c0,c1))*(A-delta(c0,c2)) );
> 
> and than substitute A with whatever you need (1, q, etc).
> 
> 
> In general, subs() and friends (match(), has()) operate on internal
> representation of expressions (as opposed to their mathematical properties).
> GiNaC stores sums (and products) as
> 
> (overall_coeff . ((coeff_0 . rest_0) (coeff_1 .  rest_1) ... ))
> 
> where overall_coeff, coeff_0, coeff_1, ... are numbers, and rest_0, rest_1
> are arbitrary expressions. subs() operates only on `rest' parts (there
> are some tricks for products, but that's a different story):
> 
> ginac/expairseq.cpp
> 1710                 // Substitute only in the "rest" part of the pairs
> 1711                 epvector::const_iterator cit = seq.begin(), last = seq.end();
> 1712                 while (cit != last) {
> 1713 
> 1714                         const ex &subsed_ex = cit->rest.subs(m, options);
> 1715                         if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
> 
> Making substitution in "coeff" part(s) would give a (very) surprising results,
> i.e.
> 
> (1 + x*y).subs(1 == q) => q + x^q*y^q
> (1/2*x^2/z^2).subs(2 == q) => 1/2*x^q/z^2
> 
> Best regards,
> 	Alexei
> 

Hmm I guess it makes sense in that regard, but it is certainly
non-intuitive. Printing the poly shows exactly what I want to replace
and not the interal representation. Might I suggest someone update the
tutorial with a statement like "Numeric literals are strongly
discouraged in the left side of a substitution, and will not produce the
desired effect."

Using symbols and doing these extra replacements makes what was a pretty
simple algorithm much messier...

Thanks

Jeremy


More information about the GiNaC-list mailing list