[SciPy-user] symbolic strings

Ryan Krauss ryanlists at gmail.com
Fri Feb 3 12:23:34 CST 2006

Thanks Travis, it sounds like I am on the right track.  I defined
checks in the __mul__ and __div__ methods to look for compound
expressions and add parentheses.

I am running into a weird problem though.  I broke the expression into
a numerator and denominator to get the parentheses to work, but when I
try a=anum/aden*-1
I get an error: TypeError: unsupported operand type(s) for /: 'symstr'
and 'symstr'.  But symstr does have a __div__ method and when I run
the code with the ipython pdb magic turned on, I have no trouble
executing temp=anum/aden*-1 and getting the correct result.  Why does
it work in the debugger and not in the script?

Below is a chunk of the ipython session.



/home/ryan/rwkpython/TMM/feedback/__init__.py in GetAugMat(self, s)
     55         anum=L0*m1*r1*s**2-c1*s-k1
     56         aden=c1*s+k1
---> 57         a=anum/aden*-1
     58 #        a=(-1)*(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)

TypeError: unsupported operand type(s) for /: 'symstr' and 'symstr'
/home/ryan/rwkpython/TMM/feedback/__init__.py in GetAugMat()
     56         aden=c1*s+k1
---> 57         a=anum/aden*-1
     58 #        a=(-1)*(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)

ipdb> temp=anum/aden*-1
ipdb> temp
ipdb> type(anum)
<class 'rwkmisc.symstr'>
ipdb> type(aden)
<class 'rwkmisc.symstr'>
ipdb> rwkmisc.symstr(1)

On 2/3/06, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> Ryan Krauss wrote:
> >I may be getting in over my head, but I think this can work.  I do a
> >lot of analysis with matrices using the transfer matrix method.
> >Sometimes I want to do numeric work with them and sometimes I use
> >python to generate an input script for Maxima.  Right now I have two
> >methods for my transfer matrix element, one that outputs a numeric
> >matrix and one that outputs a string for Maxima.  I am working on a
> >new element and would like to combined these two expressions by
> >creating a symbolic string method so that expressions like
> >a=-(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)
> >
> >make sense whether L0, m1, r1, s, etc are floats or symstrs (my new
> >class).  I overrode the __mul__, __add__, __div__,__sub__, and __pow__
> >methods but don't know if there are methods to override to deal with
> >the parenthesis and the unary - out in front.  Can this be done with
> >only a little bit more work?  Basically I want the output of this
> >expression to be 'a=-(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)' if it is a
> >string and a float or complex if the variables are numbers.
> >
> >
> This can be done, but you would basically have to add the parenthesis in
> yourself as you build up your string...
> Yes, the unary '-' can be over-written (see __neg__ special method).
> -Travis
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list