[SciPy-User] scipy.optimize.fixed_point -- TypeError: can't multiply sequence by non-int of type 'float'

Warren Weckesser warren.weckesser at enthought.com
Wed Sep 5 08:33:05 EDT 2012


On Tue, Sep 4, 2012 at 2:06 PM, Ted To <rainexpected at theo.to> wrote:

> Hi,
>
> I'm having trouble figuring out what I'm doing wrong here.  I have a
> function br(p,q) defined where p and q are lists of length 2 and I'm
> trying to compute the fixed point of br for a given q.  E.g.,
> fixed_point(br,x0=p,args=(q,)).  The problem is that I get a TypeError
> (the Traceback at the end of this message).  I don't know if this could
> be the cause or not but br is defined as the argmin of another function
> which I've defined (the code is currently pretty ugly but you can find
> it at http://pastebin.com/rEc6cKfd).
>
> Any help or suggestions will be much appreciated.
>
> Thanks,
> Ted To
>
> TypeError                                 Traceback (most recent call last)
> /usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in
> execfile(fname, *where)
>     176             else:
>     177                 filename = fname
> --> 178             __builtin__.execfile(filename, *where)
>
>
> /home/ted/texfiles/smoking/quality-computations/quality-provision-equilibrium.py
> in <module>()
>      66   return fixed_point(br,x0=p,args=(q,))
>      67
> ---> 68 print eqP(p,q)
>      69
>      70 def gstar(q):
>
>
> /home/ted/texfiles/smoking/quality-computations/quality-provision-equilibrium.py
> in eqP(p, q)
>      63
>      64 def eqP(p,q):
> ---> 65   print fixed_point(br,x0=p,args=(q,))
>      66   return fixed_point(br,x0=p,args=(q,))
>      67
>
> /usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in
> fixed_point(func, x0, args, xtol, maxiter)
>     509             p1 = func(p0, *args)
>     510             p2 = func(p1, *args)
> --> 511             d = p2 - 2.0 * p1 + p0
>     512             p = where(d == 0, p2, p0 - (p1 - p0)*(p1 - p0) / d)
>     513             relerr = where(p0 == 0, p, (p-p0)/p0)
>
> TypeError: can't multiply sequence by non-int of type 'float'
>


I suspect your function is returning a list in some cases.  In the
multivariate case, the fixed_point function expects the return value of
your function to be a numpy array.  Modify your code to ensure that br
always return a numpy array, and see if that fixes the problem.  (One
option might be to pass in the parameters as arrays instead of lists. .e.g
fixed_point(br, x0=p, args=(array(q),)), but that depends on how br is
implemented.)

Warren


_______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20120905/ad50a736/attachment.html>


More information about the SciPy-User mailing list