Newbie: display product

44 views
Skip to first unread message

Gary Pajer

unread,
Jul 8, 2019, 10:11:37 PM7/8/19
to sympy
I'm sure I'm not the first person having trouble getting started.  If there is a good tutorial, more detailed than the sympy docs tutorial, please point me to it!

I want to display the product of two expressions.  Suppose I have

a, b, x, y = symbols('a b x y')
ex1 = Eq(a, x + y)
ex2 = Eq(b, x * y)

How do I display a * b in terms of x and y?

Thainks.

Gagandeep Singh (B17CS021)

unread,
Jul 8, 2019, 10:14:03 PM7/8/19
to sympy
So you want to multiply the two equations?

Gary Pajer

unread,
Jul 9, 2019, 6:43:45 AM7/9/19
to sympy
Yes.   Simple question, for sure.  I've tried a lot of things, none has worked yet.  For example

Enter code here...a, b, x, y = symbols('a b x y')
ex1 = Eq(a, x + y)
ex2 = Eq(b, x * y)
a * b

returns $ab$, and efforts to have that rendered as $(x+y)xy$ haven't worked.

Gagandeep Singh (B17CS021)

unread,
Jul 9, 2019, 6:49:48 AM7/9/19
to sympy
Is https://github.com/sympy/sympy/pull/17097 related to your doubt? If yes, then I would say wait for this to be merged. Moreover, have you tried,

Eq(Mul(*[eq.lhs for eq in (<your_equations>)]), Mul(*[eq.rhs for eq in (<your_equations)])

The above is a general method and can be modified for commutative operatprs, like Mul, Add, etc.

Chris Smith

unread,
Jul 9, 2019, 12:02:07 PM7/9/19
to sympy
This is a substitution. `subs` will replace symbols in an expression with different expressions. The syntax is `expr.subs(old, new)` or `expr.subs({oldi: newi})` or `expr.subs([(old1, new1), (old2, new2), ...])`. You can read about the different forms with `help(Expr.subs)`.

    >>> eqs = Eq(a, x + y), Eq(b, x*y)

Examples of converting eqs to dict or list for subs

    >>> dict([i.args for i in eqs])
    {a: x + y, b: x*y}
    >>> [i.args for i in eqs]
    [(a, x + y), (b, x*y)]

Using one of those forms to do the substitution:

    >>> (a*b).subs(_)
    x*y*(x + y)

Oscar Benjamin

unread,
Jul 13, 2019, 7:00:32 AM7/13/19
to sympy
Hi Gary,

I wonder if we are overcomplicating things here. Does this do what you want?

x, y = symbols('x, y')
a = x + y
b = x * y
print(a * b)

--
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+un...@googlegroups.com.
> To post to this group, send email to sy...@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/2a6226b4-0928-4753-a691-a659a9035072%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Gary Pajer

unread,
Jul 13, 2019, 4:40:28 PM7/13/19
to sympy
Oscar,

That does seem to do what I want ... I think.
What's the difference between this and Chris Smith's answer?   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?)       

I took things a little farther:

q,r,s = symbols('q r s')
q = x+y
r = x*y
print(q*r)
q*r
s = q*r + 1
s
s.subs(x,3)

Everything worked.     Is it the case that I made things overly complicated from the beginning?  I'm confused.  I don't know how Sympy works, and would appreciate a better description than the tutorial.

Thanks.

On Saturday, July 13, 2019 at 7:00:32 AM UTC-4, Oscar wrote:
Hi Gary,

I wonder if we are overcomplicating things here. Does this do what you want?

x, y = symbols('x, y')
a = x + y
b = x * y
print(a * b)

--
Oscar

On Tue, 9 Jul 2019 at 03:11, Gary Pajer <gary...@gmail.com> wrote:
>
> I'm sure I'm not the first person having trouble getting started.  If there is a good tutorial, more detailed than the sympy docs tutorial, please point me to it!
>
> I want to display the product of two expressions.  Suppose I have
>
> a, b, x, y = symbols('a b x y')
> ex1 = Eq(a, x + y)
> ex2 = Eq(b, x * y)
>
> How do I display a * b in terms of x and y?
>
> Thainks.
>
> --
> 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 sy...@googlegroups.com.

Oscar Benjamin

unread,
Jul 13, 2019, 5:36:28 PM7/13/19
to sympy
On Sat, 13 Jul 2019 at 21:40, Gary Pajer <gary....@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

David Bailey

unread,
Jul 14, 2019, 8:29:07 AM7/14/19
to sy...@googlegroups.com
On 13/07/2019 22:36, Oscar Benjamin wrote:

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

I am also fairly new to Python and sympy, but I think I have pushed through this pain barrier.

As far as I can see, you really need to use symbols() or var() to set up variables which are going to be used as symbols in bits of algebra or calculus, but you do not need to make a variable symbolic if it is just going to be used to hold expressions or for some other pythonic purpose, such as a counter. However, no harm is done (someone correct me if I am wrong) if you create a lot of symbols, which you can do with :

from sympy.abc import *

because if, say, you subsequently assign X=42 that will harmlessly destroy the sympy symbol attached to x which will become pythonic again, but you obviously want to use it as a constant so that is fine.

I think a good strategy is to either save some letters for use as pythonic variables, or only use variables with more than one character as pythonic variables (they don't look nice in expressions, anyway)

If you want to use x in expressions, then it is probably best not to assign it to 42 (as above) but to leave it symbolic, and use subs to replace it in an expression by 42.

David





    


Oscar Benjamin

unread,
Jul 14, 2019, 8:38:29 AM7/14/19
to sympy
Everything you say here is correct. You need to have a clean
separation in your mind between the Python variable names and the
sympy Symbol objects. When you fully understand all of these things
the results below will not be surprising:
```
>>> z = Symbol('x')
>>> z
x
>>> x = z**2
>>> x
x**2
```
(I don't generally recommend writing code like that though)

--
Oscar
Reply all
Reply to author
Forward
0 new messages