[SciPy-user] symbolic strings

Robert Clewley rclewley at cam.cornell.edu
Fri Feb 3 14:42:34 CST 2006


The inability to use ** is something that I haven't yet addressed, it's 
true. I have been sidestepping this for now by doing something like

anum = L0*blah*'pow(s,2)' - c1*s    [strings are valid operands!]

There are convoluted historical reasons why I originally tried to avoid 
allowing ** in expressions (essentially, pow() provided the best 
compatibility with C and Python language targets for my models, given 
mixed integer and floating point types), but the more recent versions of 
my pseudo-parser and target "compiler" could probably handle that 
without this restriction. Basically, I should go back and support **

Not today, though :)

And when I said ModelSpec can be used "standalone", I should have 
clarified that you'd need some other PyDSTool modules to import from, 
especially parseUtils.py. It is in this module that the restriction on 
power syntax is placed (the source of your raised exception). But there be 
dragons in parseUtils.py. Well, more like Frankenstein's monster...

If you dare to fix this before I get to it, I'll model you a medal.
-Rob


On Fri, 3 Feb 2006, Ryan Krauss wrote:

> I am checking out QuantSpec and it seems very powerful and like it
> should be able to do what I need.  I am having trouble defining a
> __pow__ method though:
>
> I tried just adding this in the Quant class:
>    def __pow__(self, other):
>        return self.__combine(other, "**")
>
> following the example of;
>    def __mul__(self, other):
>        return self.__combine(other, "*")
>
> I was originally trying to evaluate this expression:
> anum=L0*m1*r1*s**2-c1*s-k1
> where everything on the right is a ModelSpec.Par
>
> I get this message:
> *** ValueError: Operator ** is not allowed. Please use the pow() call
>
> I changed to pow(s,2), but it still didn't work:
> ipdb> type(s)
> Out[3]: <class 'ModelSpec.Par'>
> ipdb> s**2
> TypeError: "unsupported operand type(s) for ** or pow(): 'Par' and 'float'"
>
> What would need to change to make Par**2 work?
>
> Thanks Rob, I think this can be really helpful.
>
> Ryan
>
> On 2/3/06, Robert Clewley <rclewley at cam.cornell.edu> wrote:
>> Ryan, you'll probably need __rdiv__ and __rmul__ methods to help with
>> this, in case the other objects involved in the construction are not
>> symbolic objects. There are a bunch of cases you have to check to add
>> braces appropriately if one of the operands is "compound", and you can
>> find an example of what's needed in my code for the QuantSpec
>> class.
>>
>> -Rob
>>
>>
>> On Fri, 3 Feb 2006, Ryan Krauss wrote:
>>
>>> 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.
>>>
>>> Thanks,
>>>
>>> Ryan
>>>
>>> /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)
>>>     59
>>> b=(-1)*((L0*m1*r1**2-L0*L1*m1*r1+Iz1*L0)*s**2+(c1*L1+c1*L0)*s+k1*L1+k1*L0)/(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
>>> '((Ll0*ml1*rl1*s**2-cj1*s-kj1)/(cj1*s+kj1))*(-1)'
>>> ipdb> type(anum)
>>> <class 'rwkmisc.symstr'>
>>> ipdb> type(aden)
>>> <class 'rwkmisc.symstr'>
>>> ipdb> rwkmisc.symstr(1)
>>> '1'
>>> ipdb>
>>>
>>>
>>> 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
>>>>
>>>
>>> _______________________________________________
>>> SciPy-user mailing list
>>> SciPy-user at scipy.net
>>> http://www.scipy.net/mailman/listinfo/scipy-user
>>>
>>
>> -----------------------------------
>> Rob Clewley
>> Research Associate
>> Department of Mathematics
>>   and Center for Applied Mathematics
>> Cornell University
>> Ithaca, NY 14853
>> www.cam.cornell.edu/~rclewley
>> Tel: 607-255-7760
>> Fax: 607-255-9860
>>
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user at scipy.net
>> http://www.scipy.net/mailman/listinfo/scipy-user
>>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>

-----------------------------------
Rob Clewley
Research Associate
Department of Mathematics
  and Center for Applied Mathematics
Cornell University
Ithaca, NY 14853
www.cam.cornell.edu/~rclewley
Tel: 607-255-7760
Fax: 607-255-9860



More information about the SciPy-user mailing list