On Sat, 13 Jul 2019 at 21:40, Gary Pajer <gary.pa...@gmail.com> wrote: > > Oscar, > > That does seem to do what I want ... I think. > What's the difference between this and Chris Smith's answer?
I think that both Chris and Gagandeep thought that you were trying to do something more complicated than you actually were. > I think I made a symbolic equation between symbols. Didn't you make a > *pythonic* assignment? Then python did the right thing when using the > multiplication operation '*' (by overloading?) So what happens if I break it down is this: In [1]: x, y = symbols('x, y') In [2]: x Out[2]: x In [3]: y Out[3]: y In [4]: a = x + y In [5]: a Out[5]: x + y In [6]: b = x * y In [7]: b Out[7]: x⋅y In [8]: a*b Out[8]: x⋅y⋅(x + y) Note that I haven't created any symbols called a and b because I don't need to. They are Python variables. The line a = x + y creates an expression object and assigns it to a (as a normal Python assignment). Likewise for the line b = x * y. SymPy doesn't know that I have called these objects a and b and it doesn't need to. Since a (as a Python variable) refers to the expression x + y and b refers to x*y when I multiply these two I get a new expression x*y*(x + y). This new expression is created by operator overloading. Specifically SymPy's Expr class has a __mul__ method that does this but that's more detail than you need. I can also create equation (Eq) objects as in: In [9]: z = Eq(x, 2*y) In [10]: z Out[10]: x = 2⋅ This creates a new equation object. You can pass that object to some SymPy functions that will know how to interpret it for example if you ask the solve function to to solve the equation for y: In [11]: solve(z, y) Out[11]: ⎡x⎤ ⎢─⎥ ⎣2⎦ However creating the equation object does not in itself cause SymPy to understand *in general* that there is a relationship between x and y. SymPy only looks at the equation object you have created if you pass that object (e.g. z) to some SymPy function. > I took things a little farther: > > q,r,s = symbols('q r s') Here you create two symbols q and r which you assign to the Python variables q and r. That's reasonable except that... > q = x+y > r = x*y now you've reassigned the Python variables q and r to be different expression objects. There was no need to create the symbols q and r earlier if this was the intention. > print(q*r) > q*r > s = q*r + 1 > s > s.subs(x,3) > > Everything worked. Great! > Is it the case that I made things overly complicated from the beginning? I think so. > I'm confused. I don't know how Sympy works, and would appreciate a better > description than the tutorial. Can you point to a particular part of the tutorial that is confusing? That would help to improve it. Generally it is a lot easier to understand SymPy if you are experienced with Python. A lot of our users are new to both Python and SymPy and I think that makes it hard to explain what's going on. Some things would be cleaner for users if SymPy was not embedded within Python and can be confusing unless you already know Python well and can see why things are the way they are. -- 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 sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxTMGzhk3FBwtUo8seu-jGCc91m5u21Hwi-ektTUidQc_w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.