[PyGiNaC-users] Python lists of GiNaC objects?
Status: Alpha
                
                Brought to you by:
                
                    jbrandmeyer
                    
                
            | 
      
      
      From: Richard B. K. <kr...@gi...> - 2005-09-14 21:56:03
      
     | 
| Hi,
I've just tried to hack a continued fraction routine into PyGiNaC:
diff -r1.7 numeric.cpp
17a18
> #include <cln/real.h>
111a113,146
> static PyObject*
> do_contfrac(const GiNaC::numeric x, unsigned len)
> {
>       boost::python::list result;
>       if (!x.is_real()) {
>               // Complex?
>               Py_INCREF(result.ptr());
>               return result.ptr();
>       }
>       cln::cl_R x_ = cln::the<cln::cl_R>(x.to_cl_N());
>       bool negative = false;
>       if (cln::minusp(x_)) {
>               negative = true;
>               x_ = -x_;
>       }
>       for (unsigned r=0; r<len; ++r) {
>               // Split x into integral and fractional part.
>               cln::cl_R_div_t x_split = cln::floor2(x_);
>               if (negative) {
>                       result.append(boost::python::object(GiNaC::ex(-GiNaC::numeric(x_split.quotient))));
>                       negative = false;
>               } else {
>                       result.append(boost::python::object(GiNaC::ex(GiNaC::numeric(x_split.quotient))));
>               }
>               x_ = x_split.remainder;
>               if (cln::zerop(x_))
>                       break;
>               // Invert x.
>               x_ = cln::recip(x_);
>       }
>       Py_INCREF(result.ptr());
>       return result.ptr();
> }
>
156a192
>       def( "contfrac", &do_contfrac, "continued fraction of a real number.");
However, when I use it:
>>> l=contfrac(3.1415926,2)
>>> print l
[<cginac.numeric object at 0xb7dbf974>, <cginac.numeric object at 0xb7dbf9bc>]
>>> print l[0]
3
>>> print l[1]
7
>>> print l[0:1]
[<cginac.numeric object at 0xb7dbf974>]
I wish to print [3,7].  What am I missing?
Thanks in advance
  -richy.
-- 
Richard B. Kreckel
<http://www.ginac.de/~kreckel/>
 |