# [GiNaC-list] Substitution troubles

Richard B. Kreckel kreckel at ginac.de
Tue Feb 24 23:25:56 CET 2009

```Hi!

Jeremy Jay wrote:
> It's not "intuitive" because I can see a plain single "1" when i print
> the poly, and I can call subs(1==q) on the poly with no errors, but
> when I print the poly again it is unchanged.
>
> If I were to print your first example as written, I would not see a "4"
> in the output and I would not expect anything to be substituted. If I
> wanted to make sure I got the 4, I would have to call expand or whatever
> appropriately, before the substitution.
>
> In the second example I would expect it to come out to (x^2+A*x+A). I
> see 4s and I'm substituting As in their place. If I don't want the 4x
> changed I'll have to do some extra work myself, but I don't expect GiNaC
> to differentiate between the 4s I see.
>
>
> I think any of these cases are very "intuitive" for any programmer with
> any experience with any pattern matching. A substitution is just that, a
> substitution, and it is not "well-defined" mathematically and I don't
> really see any reason for it to be as rigorous as you imply. If someone
> wants to do silly things like replace 4s with As, let him!

But GiNaC is not sed! If you want to carry over a programmer's naive
notion of substitution, then you could always print the expression and
do the replacement in a string buffer. I recommend using boost::regex.

> Alexei made his point, yes, but I don't see how it is "correct." He said
> that the semantics of a simple substitution rely upon the internal
> representation of the polynomial -- although there is no documentation
> on this point anywhere on the website. This make it non-intuitive.

Alexei was referring to GiNaC's actual internal representation. It could
be a different representation. I'm seeing 1s all over every mathematical
expression. 1+x*y could as well be written as
1^(1^1)+1^1*x^(1^1)*y^(1^1) and, upon substitution, result in
q^(q^q)+q^q*x^(q^q)*y^(q^q). Or as (-1)^2+x*y and be invariant under
subs(1==q). Is this really more insane than GiNaC's actual representation?

> If you have a reference for a rigorous mathematical definition of
> substitution on numbers feel free to send it to me to correct me.

That's an interesting twist. IIRC you were the one suggesting to do this
substitution on numbers. Now, we all seem to agree that it's not
well-defined. Are you proposing, then, that .subs(x==y) isn't
well-defined either? But every mathematician is familiar with this
notion of substituting free variables and considers it sane. Is it not