1
+ # ruff: noqa: ERA001
2
+
1
3
from typing import assert_type
2
4
3
5
import numpy as np
4
6
import optype .numpy as onp
5
7
6
- from scipy import differentiate
8
+ from scipy . differentiate import derivative , hessian , jacobian
7
9
from scipy .differentiate ._differentiate import _DerivativeResult0D , _DerivativeResultND , _HessianResult , _JacobianResult
8
10
9
11
# Test scalar
@@ -24,19 +26,27 @@ def f_f64_1nd_0d(x: onp.Array[onp.AtLeast1D, np.float64]) -> np.float64: ...
24
26
def f_f64_1nd_nd (x : onp .Array [onp .AtLeast1D , np .float64 ]) -> onp .ArrayND [np .float64 ]: ...
25
27
def f_f64_0d_arg (x : np .float64 , a : float ) -> np .float64 : ...
26
28
29
+ # NOTE: the commented out assertions only work on numpy 2.1+, so we instead check for assignability
30
+
27
31
###
28
32
# derivative
29
33
30
- assert_type (differentiate .derivative (f_f64_0d , 1.0 ), _DerivativeResult0D [np .float64 ])
31
- assert_type (differentiate .derivative (f_f64_0d , f64_0d ), _DerivativeResult0D [np .float64 ])
32
- assert_type (differentiate .derivative (f_f64_0d , i32_0d ), _DerivativeResult0D [np .float64 ])
33
- assert_type (differentiate .derivative (f_f32_0d , f32_0d ), _DerivativeResult0D [np .float32 ])
34
- assert_type (differentiate .derivative (f_f64_1d , f64_1d ), _DerivativeResultND [np .float64 , tuple [int ]])
35
- assert_type (differentiate .derivative (f_f64_nd , f64_2d ), _DerivativeResultND [np .float64 , tuple [int , int ]])
36
- assert_type (differentiate .derivative (f_f64_0d_arg , 1.0 , args = (2.0 ,)), _DerivativeResult0D [np .float64 ])
37
- assert_type (differentiate .derivative (f_f64_0d , 1.0 , tolerances = {"atol" : 0.1 }), _DerivativeResult0D [np .float64 ])
34
+ assert_type (derivative (f_f64_0d , 1.0 ), _DerivativeResult0D [np .float64 ])
35
+ assert_type (derivative (f_f64_0d , f64_0d ), _DerivativeResult0D [np .float64 ])
36
+ assert_type (derivative (f_f64_0d , i32_0d ), _DerivativeResult0D [np .float64 ])
37
+ assert_type (derivative (f_f32_0d , f32_0d ), _DerivativeResult0D [np .float32 ])
38
+
39
+ # assert_type(derivative(f_f64_1d, f64_1d), _DerivativeResultND[np.float64, tuple[int]])
40
+ # assert_type(derivative(f_f64_nd, f64_2d), _DerivativeResultND[np.float64, tuple[int, int]])
41
+ # assert_type(derivative(f_f64_1d, f64_1d, <...>), _DerivativeResultND[np.float64, tuple[int]])
42
+ _0 : _DerivativeResultND [np .float64 , tuple [int ]] = derivative (f_f64_1d , f64_1d )
43
+ _1 : _DerivativeResultND [np .float64 , tuple [int , int ]] = derivative (f_f64_nd , f64_2d )
44
+ _2 : _DerivativeResultND [np .float64 , tuple [int ]] = derivative (f_f64_1d , f64_1d , initial_step = f64_1d , step_direction = i64_1d )
45
+
46
+ assert_type (derivative (f_f64_0d_arg , 1.0 , args = (2.0 ,)), _DerivativeResult0D [np .float64 ])
47
+ assert_type (derivative (f_f64_0d , 1.0 , tolerances = {"atol" : 0.1 }), _DerivativeResult0D [np .float64 ])
38
48
assert_type (
39
- differentiate . derivative (
49
+ derivative (
40
50
f_f64_0d ,
41
51
1.0 ,
42
52
args = (),
@@ -51,12 +61,8 @@ assert_type(
51
61
),
52
62
_DerivativeResult0D [np .float64 ],
53
63
)
54
- assert_type (
55
- differentiate .derivative (f_f64_1d , f64_1d , initial_step = f64_1d , step_direction = i64_1d ),
56
- _DerivativeResultND [np .float64 , tuple [int ]],
57
- )
58
64
59
- res_der_0d = differentiate . derivative (f_f64_0d , 1.0 )
65
+ res_der_0d = derivative (f_f64_0d , 1.0 )
60
66
assert_type (res_der_0d .success , np .bool_ )
61
67
assert_type (res_der_0d .status , np .int32 )
62
68
assert_type (res_der_0d .nfev , np .int32 )
@@ -65,32 +71,38 @@ assert_type(res_der_0d.x, np.float64)
65
71
assert_type (res_der_0d .df , np .float64 )
66
72
assert_type (res_der_0d .error , np .float64 )
67
73
68
- res_der_nd = differentiate .derivative (f_f64_1d , f64_1d )
69
- assert_type (res_der_nd .success , onp .Array1D [np .bool_ ])
70
- assert_type (res_der_nd .status , onp .Array1D [np .int32 ])
71
- assert_type (res_der_nd .nfev , onp .Array1D [np .int32 ])
72
- assert_type (res_der_nd .nit , onp .Array1D [np .int32 ])
73
- assert_type (res_der_nd .x , onp .Array1D [np .float64 ])
74
- assert_type (res_der_nd .df , onp .Array1D [np .float64 ])
75
- assert_type (res_der_nd .error , onp .Array1D [np .float64 ])
74
+ res_der_nd = derivative (f_f64_1d , f64_1d )
75
+ # assert_type(res_der_nd.success, onp.Array1D[np.bool_])
76
+ # assert_type(res_der_nd.status, onp.Array1D[np.int32])
77
+ # assert_type(res_der_nd.nfev, onp.Array1D[np.int32])
78
+ # assert_type(res_der_nd.nit, onp.Array1D[np.int32])
79
+ # assert_type(res_der_nd.x, onp.Array1D[np.float64])
80
+ # assert_type(res_der_nd.df, onp.Array1D[np.float64])
81
+ # assert_type(res_der_nd.error, onp.Array1D[np.float64])
82
+ _3 : onp .Array1D [np .bool_ ] = res_der_nd .success
83
+ _4 : onp .Array1D [np .int32 ] = res_der_nd .status
84
+ _5 : onp .Array1D [np .int32 ] = res_der_nd .nfev
85
+ _6 : onp .Array1D [np .int32 ] = res_der_nd .nit
86
+ _7 : onp .Array1D [np .float64 ] = res_der_nd .x
87
+ _8 : onp .Array1D [np .float64 ] = res_der_nd .df
88
+ _9 : onp .Array1D [np .float64 ] = res_der_nd .error
76
89
77
90
###
78
91
# jacobian
79
92
80
- assert_type (differentiate . jacobian (f_f64_1nd_0d , f64_1d ), _JacobianResult [np .float64 , onp .AtLeast1D ])
81
- assert_type (differentiate . jacobian (f_f64_1nd_nd , f64_1d ), _JacobianResult [np .float64 , onp .AtLeast1D ])
93
+ assert_type (jacobian (f_f64_1nd_0d , f64_1d ), _JacobianResult [np .float64 , onp .AtLeast1D ])
94
+ assert_type (jacobian (f_f64_1nd_nd , f64_1d ), _JacobianResult [np .float64 , onp .AtLeast1D ])
82
95
assert_type (
83
- differentiate . jacobian (
96
+ jacobian (
84
97
f_f64_1nd_0d , f64_1d , tolerances = {"atol" : 0.1 }, maxiter = 15 , order = 6 , initial_step = 0.1 , step_factor = 1.8 , step_direction = 0
85
98
),
86
99
_JacobianResult [np .float64 , onp .AtLeast1D ],
87
100
)
88
101
assert_type (
89
- differentiate .jacobian (f_f64_1nd_nd , f64_2d , initial_step = f64_1d , step_direction = i64_1d ),
90
- _JacobianResult [np .float64 , onp .AtLeast1D ],
102
+ jacobian (f_f64_1nd_nd , f64_2d , initial_step = f64_1d , step_direction = i64_1d ), _JacobianResult [np .float64 , onp .AtLeast1D ]
91
103
)
92
104
93
- res_jac = differentiate . jacobian (f_f64_1nd_0d , f64_1d )
105
+ res_jac = jacobian (f_f64_1nd_0d , f64_1d )
94
106
assert_type (res_jac .status , onp .Array [onp .AtLeast1D , np .int32 ])
95
107
assert_type (res_jac .df , onp .Array [onp .AtLeast1D , np .float64 ])
96
108
assert_type (res_jac .error , onp .Array [onp .AtLeast1D , np .float64 ])
@@ -101,16 +113,14 @@ assert_type(res_jac.success, onp.Array[onp.AtLeast1D, np.bool_])
101
113
###
102
114
# hessian
103
115
104
- assert_type (differentiate . hessian (f_f64_1nd_0d , f64_1d ), _HessianResult [np .float64 , onp .AtLeast2D ])
116
+ assert_type (hessian (f_f64_1nd_0d , f64_1d ), _HessianResult [np .float64 , onp .AtLeast2D ])
105
117
assert_type (
106
- differentiate .hessian (
107
- f_f64_1nd_0d , f64_1d , tolerances = {"atol" : 0.1 }, maxiter = 25 , order = 10 , initial_step = 0.05 , step_factor = 2.5
108
- ),
118
+ hessian (f_f64_1nd_0d , f64_1d , tolerances = {"atol" : 0.1 }, maxiter = 25 , order = 10 , initial_step = 0.05 , step_factor = 2.5 ),
109
119
_HessianResult [np .float64 , onp .AtLeast2D ],
110
120
)
111
- assert_type (differentiate . hessian (f_f64_1nd_0d , f64_2d , initial_step = f64_1d ), _HessianResult [np .float64 , onp .AtLeast2D ])
121
+ assert_type (hessian (f_f64_1nd_0d , f64_2d , initial_step = f64_1d ), _HessianResult [np .float64 , onp .AtLeast2D ])
112
122
113
- res_hes = differentiate . hessian (f_f64_1nd_0d , f64_1d )
123
+ res_hes = hessian (f_f64_1nd_0d , f64_1d )
114
124
assert_type (res_hes .status , onp .Array [onp .AtLeast2D , np .int32 ])
115
125
assert_type (res_hes .error , onp .Array [onp .AtLeast2D , np .float64 ])
116
126
assert_type (res_hes .nfev , onp .Array [onp .AtLeast2D , np .int32 ])
0 commit comments