[GiNaC-list] Python bindings to GiNaC

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Mon Jan 10 05:52:03 CET 2005


On Sun, 2005-01-09 at 20:45 +0200, Matti Peltomäki wrote:
> Hi, 
> 
> On Tue, 4 Jan 2005, Jonathan Brandmeyer wrote:
> 
> > So have I, and I have support for a larger number of classes including
> 
> I find this project of your pretty interesting. I played around with it a 
> little bit (was able to build it just fine etc.). However, I stumbled into
> a problem. 
> 
> I wanted to check out the use of lsolve() inspired by check/exam_lsolve.cpp

See also bin/check_lsolve.py in the PyGiNaC source tree.  It's basically
a straight pythonization of the C++ code.

> in GiNaC source tree. Namely, I try to solve 3*x+5==8 for x. Since both 
> the equation and the answer are instances of relational, one has to access 
> the rhs and lhs to extract the answer. Now, it seems that if another side 
> of a relational is numeric, unwrap_ex() does not seem to be able to unwrap
> it properly, throwing the exception "Could not unwrap an ex of unknown type."
> This seems strange since unwrap_ex() throws this exception if the tinfo of the 
> object does not match any one listed in ex.cpp (and TINFO_numeric is listed). 
> This does not happen, if the numeric is a term in an add, for instance. 
> 
> Is this something that I do wrong or what is going on in the relational?
> 
> Below is a short example showing what I did and which errors I got. I run 
> python as './run python2.3' in the root of pyginac source tree after having
> succesfully built it with scons. 

I've got basically (exactly?) the same configuration as you, but I get
this result instead:

Python 2.3.4 (#2, Dec  3 2004, 13:53:17)
[GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ginac import *
>>> x = symbol('x')
>>> eq = 3*x+5 == 8
>>> eq.lhs
<cginac.add object at 0xb7e34dec>
>>> eq.rhs
<cginac.numeric object at 0xb7de5614>
>>> answer = lsolve([eq], [x])
>>> print answer
[<cginac.relational object at 0xb7e39f2c>]
>>> print [str(x) for x in answer]
['x==1']
>>> print answer[0].lhs
x
>>> print answer[0].rhs
1


> >>> from ginac import *;
> >>> x = symbol("x");
> >>> eq = ( 3*x + 5 == 8);
> >>> eq.rhs[1]
> <cginac.numeric object at 0xb7dda4f4>
> >>> print(eq.rhs[1])
> 5

This is really weird.  The only way you should be able to get this is if
eq was defined to be 8 == 3*x + 5.

What is the value of ginac.version?

> >>> eq.lhs  
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> RuntimeError: Could not unwrap an ex of unknown type.
> >>> answer = lsolve([eq], [x])
> >>> answer
> [<cginac.relational object at 0xb7e43e3c>]
> >>> answer[0].rhs
> <cginac.symbol object at 0xb7e3d8cc>
> >>> print(answer[0].rhs)
> x
> >>> answer[0].lhs     
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> RuntimeError: Could not unwrap an ex of unknown type.
> >>> 





More information about the GiNaC-list mailing list