subs

26 views
Skip to first unread message

Juan Luis Varona

unread,
Nov 23, 2021, 2:42:12 PM11/23/21
to sage-support
In the expression (5^x)^2-7*5^x+4, I want to substitute x^5 by t.

With sagemath 7.2 (or another old versions), I can do
((5^x)^2-7*5^x+4).subs(5^x==t)
and I get t^2 - 5*t + 4

But sagemath 9.4 does not change the first 5^x and he gives
5^(2*x) - 7*t + 4

Why?

(In both cases, var("t") has been previously used)

Yours,

Juan Luis Varona

slelievre

unread,
Nov 23, 2021, 3:06:15 PM11/23/21
to sage-support
2021-11-23 19:42:12 UTC, Juan Luis Varona:
In recent versions of Sage, defining:
```
sage: t, x = SR.var('t, x')
sage: a = (5^x)^2-7*5^x+4
```
automatically groups exponents and gives:
```
sage: a
5^(2*x) - 7*5^x + 4
```
in which `5^x` is only seen once as such (old versions
of Sage possibly did not group exponents, thus keeping
two visible occurrences of `5^x` in the resulting `a`).

This means that only one `5^x` gets replaced
by `t` when we do the following substitution:
```
sage: aa = a.subs(5^x == t)
sage: aa

5^(2*x) - 7*t + 4
```

To work around this, we can instead think of
rewriting `x` as `log(t, 5)` as in the following
substitution, which gives the expected result:
```
sage: ab = a.subs(x == log(t, 5))
sage: ab
t^2 - 7*t + 4
```

Now we have a polynomial expression in t and
we can use corresponding tools.    --Samuel

Juan Luis Varona

unread,
Nov 23, 2021, 4:52:43 PM11/23/21
to sage-support
Thanks!

Emmanuel Charpentier

unread,
Nov 24, 2021, 11:20:10 AM11/24/21
to sage-support

FWIW :

sage: var("x, t")
(x, t)
sage: maxima_calculus.ratsubst(t,5^x, ex).sage()
t^2 - 7*t + 4
sage: maxima_calculus.lratsubst([5^x==t], ex).sage()
t^2 - 7*t + 4

See also this ask.sagemath.org question.

I think that wrapping Maximas [l]ratsubst might be interesting…

Emmanuel Charpentier

unread,
Nov 24, 2021, 11:26:38 AM11/24/21
to sage-support

Also :

sage: import sympy
sage: ex._sympy_().subs({(5^x)._sympy_():t._sympy_()})._sage_()
t^2 - 7*t + 4

HTH,

Reply all
Reply to author
Forward
0 new messages