On Wed, 7 Apr 2021 at 10:28, 'Bruce Allen' via sympy <[email protected]> wrote: > > I have a very basic sympy question, which has me stumped, and am hoping > that someone here can set me straight. I have an expression for which > subs() seems to have no effect: > > >>> a=Symbol('a', real=True, positive=True) > > >>> q=u[7] > > >>> q > -a*(45*a**18 - 120*a**16 + 240*a**12 - 504*a**8 + 1440*a**4 - 2700*a**2 > - 4645)/4050 > > >>> q.subs(a, Rational(1,2)) > -a*(45*a**18 - 120*a**16 + 240*a**12 - 504*a**8 + 1440*a**4 - 2700*a**2 > - 4645)/4050 > > >>> srepr(q) > "Mul(Integer(-1), Rational(1, 4050), Symbol('a', real=True, > positive=True), Add(Mul(Integer(45), Pow(Symbol('a', real=True, > positive=True), Integer(18))), Mul(Integer(-1), Integer(120), > Pow(Symbol('a', real=True, positive=True), Integer(16))), > Mul(Integer(240), Pow(Symbol('a', real=True, positive=True), > Integer(12))), Mul(Integer(-1), Integer(504), Pow(Symbol('a', real=True, > positive=True), Integer(8))), Mul(Integer(1440), Pow(Symbol('a', > real=True, positive=True), Integer(4))), Mul(Integer(-1), Integer(2700), > Pow(Symbol('a', real=True, positive=True), Integer(2))), Integer(-4645)))" > > But if I just cut and paste the same expression into the variable, then > subs() works as expected: > > >>> q=-a*(45*a**18 - 120*a**16 + 240*a**12 - 504*a**8 + 1440*a**4 - > 2700*a**2 - 4645)/4050 > > >>> q.subs(a, Rational(1,2)) > 1371514291/2123366400 > > The internal representation is identical: > > >>> srepr(q) > "Mul(Integer(-1), Rational(1, 4050), Symbol('a', real=True, > positive=True), Add(Mul(Integer(45), Pow(Symbol('a', real=True, > positive=True), Integer(18))), Mul(Integer(-1), Integer(120), > Pow(Symbol('a', real=True, positive=True), Integer(16))), > Mul(Integer(240), Pow(Symbol('a', real=True, positive=True), > Integer(12))), Mul(Integer(-1), Integer(504), Pow(Symbol('a', real=True, > positive=True), Integer(8))), Mul(Integer(1440), Pow(Symbol('a', > real=True, positive=True), Integer(4))), Mul(Integer(-1), Integer(2700), > Pow(Symbol('a', real=True, positive=True), Integer(2))), Integer(-4645)))" > > Why does subs() work in one case, and not in the other?
I'm not sure. I can't reproduce this without u though. I tried making u=(the srepr output) but then the substitution worked fine. Oscar -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxSaRRKWmh42b4rvr6E%2BWzEo-Rp0uuvCEhxxidB5%2BoQw4w%40mail.gmail.com.
