error in factor

1 view
Skip to first unread message

Neal Becker

unread,
Nov 2, 2009, 2:31:51 PM11/2/09
to sy...@googlegroups.com
Out[7]: (1 + s)*(1 + 0.618*s + s**2)*(1 + 1.618*s + s**2)
...
In [10]: expand (_)
Out[10]: 1 + 3.236*s + 5.235924*s**2 + 5.235924*s**3 + 3.236*s**4 + s**5

In [11]: factor(_)
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (33, 0))

---------------------------------------------------------------------------
CoefficientError Traceback (most recent call last)

/home/nbecker/jupiter/<ipython console> in <module>()

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/polys/factortools.pyc in factor(f, *symbols, **flags)
78 return f
79
---> 80 coeff, factors = poly_factors(f, *symbols, **flags)
81
82 result = 1 # XXX: don't include coeff in the leading factor

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/polys/factortools.pyc in poly_factors(f, *symbols, **flags)
35
36 F = Poly(f, *symbols)
---> 37 denom, F = F.as_integer()
38
39 cont, factors = zzX_factor(zzX_from_poly(F))

/usr/lib/python2.6/site-packages/sympy-0.6.5-
py2.6.egg/sympy/polys/polynomial.pyc in as_integer(self)
1295 coeffs.append(Integer(int(coeff)))
1296 else:
-> 1297 raise CoefficientError("%s is not a rational number" %
coeff)
1298
1299 denom = sympify(denom)

CoefficientError: 3.23600000000000 is not a rational number

In [12]:

Ondrej Certik

unread,
Nov 2, 2009, 3:08:16 PM11/2/09
to sy...@googlegroups.com
Hi Neal!

On Mon, Nov 2, 2009 at 11:31 AM, Neal Becker <ndbe...@gmail.com> wrote:
>
> Out[7]: (1 + s)*(1 + 0.618*s + s**2)*(1 + 1.618*s + s**2)
> ...
> In [10]: expand (_)
> Out[10]: 1 + 3.236*s + 5.235924*s**2 + 5.235924*s**3 + 3.236*s**4 + s**5
>
> In [11]: factor(_)
> ERROR: An unexpected error occurred while tokenizing input
> The following traceback may be corrupted or invalid
> The error message is: ('EOF in multi-line statement', (33, 0))

I think this bug is fixed by the Mateusz new polynomials branch. We
didn't manage to merge it yet, but we'll do it soon.

Ondrej

smichr

unread,
Nov 3, 2009, 12:23:05 AM11/3/09
to sympy
> On Mon, Nov 2, 2009 at 11:31 AM, Neal Becker <ndbeck...@gmail.com> wrote:
>
> > Out[7]: (1 + s)*(1 + 0.618*s + s**2)*(1 + 1.618*s + s**2)
> > ...
> > In [10]: expand (_)
> > Out[10]: 1 + 3.236*s + 5.235924*s**2 + 5.235924*s**3 + 3.236*s**4 + s**5
>
> > In [11]: factor(_)
> > ERROR: An unexpected error occurred while tokenizing input

You can try the poor-man's work around: expand it, multiply by a
scaling factor, turn all reals to integers, and then factor and scale
down the results:

###
>>> # make a helper function
>>> def reals2ints(eq):
... for a in eq.atoms(Real):
... eq=eq.subs(a, int(a))
... return eq
...

>>> e #here's the starting point
(1 + s)*(1 + 0.618*s + s**2)*(1.0 + 1.618*s + s**2)

>>> e.expand() # to expose all reals
1 + 3.236*s + 5.235924*s**2 + 5.235924*s**3 + 3.236*s**4 + s**5

>>> _*1e12 # only 1e6 is needed, but 12 digits of precision could always be used by default
1000000000000.0 + 3236000000000.0*s + 5235924000000.0*s**2 +
5235924000000.0*s**3 + 3236000000000.0*s**4 + 1000000000000.0*s**5

>>> reals2ints(_)
1000000000000 + 3236000000000*s + 5235924000000*s**2 +
5235924000000*s**3 + 3236000000000*s**4 + 1000000000000*s**5

>>> factor(_)/1e12
4.0e-6*(1 + s)*(500 + 309*s + 500*s**2)*(500 + 809*s + 500*s**2)

You can distribute the 1e-6 if you want...

/c
Reply all
Reply to author
Forward
0 new messages