Hi Jose,

On Mon, Jul 27, 2009 at 10:33 PM, Jose Guzman<[email protected]> wrote:
>
> Hi there!
>
> I am trying to find some parameters with the least squares algorithm
> within Sage (v 4.0.2) . Unfortunately, when I try to use the
> scipy.optimize.leastsq() method within Sage I get the following error:
>
>  >>> array cannot be safely cast to required type.
>
> Strange enough,  if I do the same directly with iPython I can perform
> the computation properly.  Do I have to cast my original data to some
> special type in Sage (i.e RealNumbers)???. If yes, how? I tried lists,
> tuples and NumPy arrays but  It did not work.
>
> here the simplified code:
>
> # some dummy  x,y data for testing
> x = np.arange(0,3.0,0.25)
> y =
> np.array([0.0,0.3066,1.2263,2.7591,4.9050,7.6641,11.0363,15.0216,19.6200,24.8316,30.6562,37.0941],dtype=float)
>
> # define a residuals function
> def residuals(p,x,y):
>    k,degree = p
>    err = y -(k*(x**degree))
>    return err
>
> # initial guess, k = 4.0, exp = 3?
> pinit = [4.0,3]
>
> #compute the leastsq algorithm
> leastsq(residuals,pinit,args=(x,y))

I got this far:

----------------------------------------------------------------------
| Sage Version 4.1, Release Date: 2009-07-09                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: import numpy as np
sage: import scipy.optimize
sage: x = np.arange(0, 3.0, 0.25)
sage: y = np.array([0.0, 0.3066, 1.2263, 2.7591, 4.9050, 7.6641,
11.0363, 15.0216, 19.6200, 24.8316, 30.6562, 37.0941], dtype=float)
sage: def residuals(p, x, y):
....:     k, degree = p
....:     err = y -(k*(x^degree))
....:     return err
....:
sage: pinit = [4.0, 3]
sage: scipy.optimize.leastsq(residuals, pinit, args=(x,y))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/mvngu/.sage/temp/sage.math.washington.edu/28595/_home_mvngu__sage_init_sage_0.py
in <module>()

/usr/local/sage/local/lib/python2.6/site-packages/scipy/optimize/minpack.pyc
in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol,
gtol, maxfev, epsfcn, factor, diag, warning)
    268         if (maxfev == 0):
    269             maxfev = 200*(n+1)
--> 270         retval =
_minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
    271     else:
    272         if col_deriv:

TypeError: array cannot be safely cast to required type

As mentioned at the thread

http://groups.google.com/group/sage-devel/browse_thread/thread/fd1414514c5d83b2

the problem is that at the moment there is little support for
automatic data conversion between NumPy and Sage. Here are some
tickets to make this happen:

#6497
http://trac.sagemath.org/sage_trac/ticket/6497

#5081
http://trac.sagemath.org/sage_trac/ticket/5081

#6506
http://trac.sagemath.org/sage_trac/ticket/6506

-- 
Regards
Minh Van Nguyen

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to