Re: [sympy] Newbie: display product

2019-07-14 Thread Oscar Benjamin
On Sun, 14 Jul 2019 at 13:29, David Bailey  wrote:
>
> 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.

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

-- 
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/CAHVvXxTJmhrApFgxjN6pfHEvM70gWD0AfabEATXhQisGm0qctQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] Newbie: display product

2019-07-14 Thread David Bailey

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





--
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/499a73c5-9595-3836-a694-d81791a52414%40dbailey.co.uk.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] Newbie: display product

2019-07-13 Thread Oscar Benjamin
On Sat, 13 Jul 2019 at 21:40, Gary Pajer  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.


Re: [sympy] Newbie: display product

2019-07-13 Thread Gary Pajer
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 > 
> 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 . 
> > 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. 
>

-- 
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/8de86548-0ff4-4848-bd8b-7de3bb5ba5ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [sympy] Newbie: display product

2019-07-13 Thread Oscar Benjamin
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  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 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/2a6226b4-0928-4753-a691-a659a9035072%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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/CAHVvXxSVN5f_-BNXgBda%3DWrRhbEiF9nc%2B32oCjDOT_6HGoQvwQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Newbie: display product

2019-07-08 Thread Gagandeep Singh (B17CS021)
So you want to multiply the two equations? 

-- 
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/ab8682c1-1897-44dd-a8d7-cb19e7fd5b19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[sympy] Newbie: display product

2019-07-08 Thread Gary Pajer
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 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/2a6226b4-0928-4753-a691-a659a9035072%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.