Improving .subs() method issue

63 vistas
Ir al primer mensaje no leído

HAWK

no leída,
29 mar 2024, 5:04:03 a.m.29 mar
para sympy
Hello SymPy community,
I want to ask if you guys allow me I would like to raise a issue or we can say improvement. I found .subs() amusing but I think we can upgrade that. For reference when we have a equation eq=x**2+5 and then we use eq.subs(x,1) it means x is replaced by 1(x-->1), that gives a simple answer 6. That's totally fine but if I want to substitute a expression with something, it won't work let's take the previous example, eq=x**2+5 and then we use eq.subs(x+1,2) this gives old equation back, but it was supposed to replace x+1 to 2 that means same as before (x-->1) and answer supposed to be 6 as in real mathematics.
Screenshot 2024-03-29 141816.png

Sangyub Lee

no leída,
30 mar 2024, 11:00:52 a.m.30 mar
para sympy
I may give the step-by-step technical explanation why eq.subs works this way:

- The subexpressions of x**2 + 5 are x**2, and 5, which is eq.args
- The subexpressions of x**2 are x and 2, which is (x**2).args
- There are no subexpressions of 5, because 5 is an atom.
- There are no subexpressions of 2, because 2 is an atom.
- There are no subexpressions of x, because x is an atom.

And the subs, (reasonably) works when the expression that is substituted, is equal to the expression, or its subexpressions.
x is recursively the subexpression of x**2 + 5, so eq.subs(x, 1) works,
however, x+1 is not the subexpression of  x**2 + 5, so eq.subs(x+1,2) works.

Although this is not 100% everything about how subs works, however, I think that it explains fairly most of the logic about the subs,
and I hope it can be useful for your programming experience with subs.
The reason that subs(x+1, 2) does not work, is debatable that it should be a bug, or it should be intended part of the design,

However, I'm weighted towards the opinion that it shouldn't be part of the design,
because it is much more complicated and opens up much more rooms for extension or incompleteness, if we adopt that as part of the design.
For example, if subs(x+1, 2) works, then we should also think of how subs(x**2 + 1, 2), or how subs(x**3 + 1, 2) works,
and I'm afraid that such discussions like this clearly gets into discussing very complicated mathematics that you or me are not familiar with.

HAWK

no leída,
30 mar 2024, 4:25:30 p.m.30 mar
para sy...@googlegroups.com
Thank you sangyub for such detailed explanation, I really appreciate it. I think you are right if we implement such algo for .subs(), it will just make things complex. But was thinking that if we could try such things may be it would make way better and I think there is a way to make it work. I will write you back if I can make things work like I explained.

--
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 sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/8ab1f6bc-55c1-462a-837e-400341c8d0e7n%40googlegroups.com.
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos