Another problem with cse under bleeding edge sympy

29 views
Skip to first unread message

Larry Wigton

unread,
Sep 26, 2012, 12:01:00 PM9/26/12
to sy...@googlegroups.com
Problem with passing my own names for intermediate variables to cse.

***************************************************
Python code:

#! /usr/bin/env python

from sympy import *

class Myname:
    def __init__(self):
       self.value = -1
    def __iter__(self):
       return self
    def next(self):
       self.value += 1
       val = '%s%.2d' % ('tt',self.value)
       return val

pg00=Symbol('pg00')
pg01=Symbol('pg01')
pg02=Symbol('pg02')
pg03=Symbol('pg03')

eq1= pg03**2
eq2= 2*pg01*pg03
eq3= pg00 + pg01*pg03**2 - pg02

eq = [eq1,eq2,eq3]
print eq
(red,rep) = cse(eq)
print red
print rep

print eq
My = Myname()
Myi = iter(My)
(red,rep) = cse(eq,Myi)
print red
print rep

*******************************************************

Output when using sympy 7.1

[pg03**2, 2*pg01*pg03, pg00 + pg01*pg03**2 - pg02]
[(x0, pg03**2)]
[x0, 2*pg01*pg03, pg00 + pg01*x0 - pg02]
[pg03**2, 2*pg01*pg03, pg00 + pg01*pg03**2 - pg02]
[('tt00', pg03**2)]
[tt00, 2*pg01*pg03, pg00 + pg01*tt00 - pg02]

***************************************************

Output when using bleeding edge version of sympy:


[pg03**2, 2*pg01*pg03, pg00 + pg01*pg03**2 - pg02]
[(x0, pg03**2)]
[x0, 2*pg01*pg03, pg00 + pg01*x0 - pg02]
[pg03**2, 2*pg01*pg03, pg00 + pg01*pg03**2 - pg02]
Traceback (most recent call last):
  File "mytest4.py", line 36, in <module>
    (red,rep) = cse(eq,Myi)
  File "/acct/lbw9902/gitstuff/sympy/sympy/simplify/cse_main.py", line 313, in cse
    reduced_exprs[j] = update(expr)
  File "/acct/lbw9902/gitstuff/sympy/sympy/simplify/cse_main.py", line 307, in <lambda>
    update = lambda x: x.xreplace({subtree: sym, 1/subtree: 1/sym})
TypeError: unsupported operand type(s) for /: 'int' and 'str'

*************************************************************************

So bleeding edge code fails is this case when I try using my own
names for intermediate variables.  I should point out this only happens
sometimes.  Most of the time the bleeding edge code behaves properly.

I am guessing you sympy experts can find a work around for me.

    Larry Wigton

Chris Smith

unread,
Sep 26, 2012, 1:42:19 PM9/26/12
to sy...@googlegroups.com
> class Myname:
> def __init__(self):
> self.value = -1
> def __iter__(self):
> return self
> def next(self):
> self.value += 1
> val = '%s%.2d' % ('tt',self.value)

That should be `Symbol('%s%.2d' % ('tt',self.value))`; note that
numbered_symbols('tt') will give you the same iterator.

```
>>> numbered_symbols('tt')
<generator object numbered_symbols at 0xb20039c>
>>> [_.next() for i in range(4)]
[tt0, tt1, tt2, tt3]

```
/Chris

Larry Wigton

unread,
Sep 26, 2012, 3:27:38 PM9/26/12
to sy...@googlegroups.com
Thank you for telling me about needing a call to Symbol.

It is funny that cse worked all the time without it, and bleeding edge sympy
worked most of the time without it.

My iterator returns tt00, tt01,...

numbered_symobols returns tt0, tt1,...

This made a difference for me because later on I want to replace the tt guys.

So for example if I replaced t1 by something I would clobber t11, t12 etc.
That is why I made my own iterator.

    Larry Wigton
Reply all
Reply to author
Forward
0 new messages