Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scipy.optimize.newton does not accept Sage input anymore #29210

Open
videlec opened this issue Feb 16, 2020 · 5 comments
Open

scipy.optimize.newton does not accept Sage input anymore #29210

videlec opened this issue Feb 16, 2020 · 5 comments

Comments

@videlec
Copy link
Contributor

videlec commented Feb 16, 2020

As reported in this sage-support thread, the scipy.optimize.newton became more picky about input and broke an interact.

The problem is reproducible below with sage-9.1.beta4

sage: f(t) = t**2 - 1/2
sage: df(t) = 2*t
sage: import scipy.optimize
sage: scipy.optimize.newton(f, 0.3, df)
Traceback (most recent call last)
<ipython-input-1-3a75ef95af3d> in <module>()
      2 __tmp__=var("t"); df = symbolic_expression(Integer(2)*t).function(t)
      3 import scipy.optimize
----> 4 scipy.optimize.newton(f, RealNumber('0.3'), df, maxiter=Integer(10))

/opt/sage/local/lib/python3.7/site-packages/scipy/optimize/zeros.py in newton(func, x0, fprime, args, tol, maxiter, fprime2, x1, rtol, full_output, disp)
    297                     newton_step /= 1.0 - adj
    298             p = p0 - newton_step
--> 299             if np.isclose(p, p0, rtol=rtol, atol=tol):
    300                 return _results_select(
    301                     full_output, (p, funcalls, itr + 1, _ECONVERGED))

/opt/sage/local/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2519     y = array(y, dtype=dt, copy=False, subok=True)
   2520 
-> 2521     xfin = isfinite(x)
   2522     yfin = isfinite(y)
   2523     if all(xfin) and all(yfin):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The smartest way to achieve what is wanted is certainly

sage: f_fc = fast_callable(f, float)
sage: df_fc = fast_callable(df, float)
sage: scipy.optimize.newton(f_fc, 0.3r, df_fc)
0.7071067811865475

Should we just aknowledge the incompatibility or should we try something on the numpy side?

CC: @mkoeppe @dimpase

Component: interfaces

Issue created by migration from https://trac.sagemath.org/ticket/29210

@videlec videlec added this to the sage-9.1 milestone Feb 16, 2020
@videlec

This comment has been minimized.

@mkoeppe
Copy link
Contributor

mkoeppe commented May 1, 2020

comment:3

Moving tickets to milestone sage-9.2 based on a review of last modification date, branch status, and severity.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 May 1, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Oct 24, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 2, 2021

comment:5

Moving this ticket to 9.4, as it seems unlikely that it will be merged in 9.3, which is in the release candidate stage

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Apr 2, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Aug 22, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 May 3, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@mkoeppe
Copy link
Contributor

mkoeppe commented Nov 28, 2022

comment:10

I think here we have run into the changes to numpy's casting rules. Many functions now default to casting='safe'.

sage: sage: f(t) = t**2 - 1/2
sage: y = f(0.3r)
sage: type(y)
<class 'sage.symbolic.expression.Expression'>
sage: np.array([y]).astype('float', casting='safe')
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
sage: np.array([y]).astype('float', casting='unsafe')
array([-0.41])

@kwankyu
Copy link
Collaborator

kwankyu commented Nov 29, 2022

comment:11

I guess the problem will be solved if we introduce ConstantExpression palatable to numpy as commented in #34693.

@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants