the possibility to use sympy to calculate equations like z = part1 (+/-) part2 (+/-) part3 ... (+/-) partN, etc.

28 views
Skip to first unread message

Dos18i2-se Beaverov

unread,
Apr 4, 2016, 9:32:53 AM4/4/16
to sympy
Hi, I would like to calculate equations like this:

z = part1 (+/-) part2 (+/-) part3 ... (+/-) partN.

In case of 3 parts it`s rather easy to code all variants of equations (z1 = part1 + part2 + part3; z2 = part1 + part2 - part3; z3 = part1 - part2 - part3; z4 = part1 - part2 + part3). But in case of four or higher numbers of equation parts it seems borring.
Can I use sympy for this task? Could you please give some simple example or some key words for search in the google?
Thanks in advance.

Alan Bromborsky

unread,
Apr 4, 2016, 9:44:06 AM4/4/16
to sy...@googlegroups.com
You could write a python function to calculate all the different sign combinations where the input to the function would be [part1,...,partn] and return a list of sympy expressions [z1,...,zn].  The sign arrays could be generated by enumerating all n-digit binary numbers where a 0 digit would be + and a 1 digit -.

--
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/9892df70-b70f-4aa5-b92d-5563efcb4600%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dos18i2-se Beaverov

unread,
Apr 4, 2016, 9:45:35 AM4/4/16
to sympy
I found asmeurer comment here: http://stackoverflow.com/questions/27872250/plus-minus-operator-for-python.
It`s very useful but I`m still stacked:
from sympy import *

pm
= Symbol(u'±') # The u is not needed in Python 3. I used ± just for pretty printing purposes. It has no special meaning.


k
= 0
l
= 0
m
= 0


f1
= 1; f2 = 2; f3 = 3
# expr = k*f1 + pm*f2 + pm*f3
# expr1 = k*f1; expr2 = l*f2; expr3 = m*f3
# expr = expr1 + pm*expr2 + pm*expr3

expr1list
= []


# expr2pos = expr2.subs(pm, 1)
# expr2neg = expr2.subs(pm, -1)
# expr2list = []
# expr2list.append(expr1.subs(pm, 1))
# expr2list.append(expr1.subs(pm, -1))
#
# expr3pos = expr3.subs(pm, 1)
# expr3neg = expr3.subs(pm, -1)
# expr3list = []
# expr3list.append(expr1.subs(pm, 1))
# expr3list.append(expr1.subs(pm, -1))

for k in range(0,3):
   
for l in range(0,3):
       
for m in range(0,3):
           
if k+l+m == 3:
                expr1
= k*f1; expr2 = l*f2#; expr3 = m*f3
                expr
= expr1 + pm*expr2
                   
                expr1pos
= expr.subs(pm, 1)
                expr1neg
= expr.subs(pm, -1)
                   
                expr1list
.append(expr.subs(pm, 1))
                expr1list
.append(expr.subs(pm, -1))
                   
               
for p1 in expr1list:

                   
print (k,l,m)
                   
print expr1list
                   
print expr
                   
print 'next triple'

gives a lot of errors. The last this one: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb1' in position 0: ordinal not in range(128).
But if I try asmeurer`s example from the link above it works fine.
Could anybody help for the newbe?


понедельник, 4 апреля 2016 г., 16:32:53 UTC+3 пользователь Dos18i2-se Beaverov написал:

Aaron Meurer

unread,
Apr 4, 2016, 12:28:51 PM4/4/16
to sy...@googlegroups.com
You can use itertools.product:

import itertools

parts = (x, y, z, t)

N = len(parts)
P = itertools.product(*[(-1, 1) for i in range(N-1)])
eqs = [parts[0] + sum(i*p for i, p in zip(a, parts[1:])) for a in P]

for this example, it gives

In [49]: eqs
Out[49]: [-t + x - y - z, t + x - y - z, -t + x - y + z, t + x - y +
z, -t + x + y - z, t + x + y - z, -t + x + y + z, t + x + y + z]

Aaron Meurer


On Mon, Apr 4, 2016 at 8:39 AM, Dos18i2-se Beaverov <dos1...@gmail.com> wrote:

Dos18i2-se Beaverov

unread,
Apr 4, 2016, 4:01:56 PM4/4/16
to sympy
Wow! Perfect job! Thank you very much.

понедельник, 4 апреля 2016 г., 19:28:51 UTC+3 пользователь Aaron Meurer написал:
Reply all
Reply to author
Forward
0 new messages