<s> from __future__ import division <EOL> import math <EOL> from itertools import product <EOL> import numpy as np <EOL> from numpy . testing import ( assert_raises , assert_allclose , assert_equal , <EOL> assert_ , run_module_suite ) <EOL> from scipy . sparse import csr_matrix , csc_matrix , lil_matrix <EOL> from scipy . optimize . _numdiff import ( <EOL> _adjust_scheme_to_bounds , approx_derivative , check_derivative , <EOL> group_columns ) <EOL> def test_group_columns ( ) : <EOL> structure = [ <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> ] <EOL> for transform in [ np . asarray , csr_matrix , csc_matrix , lil_matrix ] : <EOL> A = transform ( structure ) <EOL> order = np . arange ( <NUM_LIT:6> ) <EOL> groups_true = np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> groups = group_columns ( A , order ) <EOL> assert_equal ( groups , groups_true ) <EOL> order = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> groups_true = np . array ( [ <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> groups = group_columns ( A , order ) <EOL> assert_equal ( groups , groups_true ) <EOL> groups_1 = group_columns ( A ) <EOL> groups_2 = group_columns ( A ) <EOL> assert_equal ( groups_1 , groups_2 ) <EOL> class TestAdjustSchemeToBounds ( object ) : <EOL> def test_no_bounds ( self ) : <EOL> x0 = np . zeros ( <NUM_LIT:3> ) <EOL> h = np . ones ( <NUM_LIT:3> ) * <NUM_LIT> <EOL> inf_lower = np . empty_like ( x0 ) <EOL> inf_upper = np . empty_like ( x0 ) <EOL> inf_lower . fill ( - np . inf ) <EOL> inf_upper . fill ( np . inf ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:1> , '<STR_LIT>' , inf_lower , inf_upper ) <EOL> assert_allclose ( h_adjusted , h ) <EOL> assert_ ( np . all ( one_sided ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:2> , '<STR_LIT>' , inf_lower , inf_upper ) <EOL> assert_allclose ( h_adjusted , h ) <EOL> assert_ ( np . all ( one_sided ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:1> , '<STR_LIT>' , inf_lower , inf_upper ) <EOL> assert_allclose ( h_adjusted , h ) <EOL> assert_ ( np . all ( ~ one_sided ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:2> , '<STR_LIT>' , inf_lower , inf_upper ) <EOL> assert_allclose ( h_adjusted , h ) <EOL> assert_ ( np . all ( ~ one_sided ) ) <EOL> def test_with_bound ( self ) : <EOL> x0 = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> , - <NUM_LIT> ] ) <EOL> lb = - np . ones ( <NUM_LIT:3> ) <EOL> ub = np . ones ( <NUM_LIT:3> ) <EOL> h = np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> h_adjusted , _ = _adjust_scheme_to_bounds ( x0 , h , <NUM_LIT:1> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , h ) <EOL> h_adjusted , _ = _adjust_scheme_to_bounds ( x0 , h , <NUM_LIT:2> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] ) * <NUM_LIT> ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:1> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . abs ( h ) ) <EOL> assert_ ( np . all ( ~ one_sided ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:2> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] ) * <NUM_LIT> ) <EOL> assert_equal ( one_sided , np . array ( [ False , True , True ] ) ) <EOL> def test_tight_bounds ( self ) : <EOL> lb = np . array ( [ - <NUM_LIT> , - <NUM_LIT> ] ) <EOL> ub = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> x0 = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> ] ) <EOL> h = np . array ( [ - <NUM_LIT:0.1> , - <NUM_LIT:0.1> ] ) <EOL> h_adjusted , _ = _adjust_scheme_to_bounds ( x0 , h , <NUM_LIT:1> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT> , - <NUM_LIT> ] ) ) <EOL> h_adjusted , _ = _adjust_scheme_to_bounds ( x0 , h , <NUM_LIT:2> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT> , - <NUM_LIT> ] ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:1> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT> , - <NUM_LIT> ] ) ) <EOL> assert_equal ( one_sided , np . array ( [ False , True ] ) ) <EOL> h_adjusted , one_sided = _adjust_scheme_to_bounds ( <EOL> x0 , h , <NUM_LIT:2> , '<STR_LIT>' , lb , ub ) <EOL> assert_allclose ( h_adjusted , np . array ( [ <NUM_LIT> , - <NUM_LIT> ] ) ) <EOL> assert_equal ( one_sided , np . array ( [ False , True ] ) ) <EOL> class TestApproxDerivativesDense ( object ) : <EOL> def fun_scalar_scalar ( self , x ) : <EOL> return np . sinh ( x ) <EOL> def jac_scalar_scalar ( self , x ) : <EOL> return np . cosh ( x ) <EOL> def fun_scalar_vector ( self , x ) : <EOL> return np . array ( [ x [ <NUM_LIT:0> ] ** <NUM_LIT:2> , np . tan ( x [ <NUM_LIT:0> ] ) , np . exp ( x [ <NUM_LIT:0> ] ) ] ) <EOL> def jac_scalar_vector ( self , x ) : <EOL> return np . array ( <EOL> [ <NUM_LIT:2> * x [ <NUM_LIT:0> ] , np . cos ( x [ <NUM_LIT:0> ] ) ** - <NUM_LIT:2> , np . exp ( x [ <NUM_LIT:0> ] ) ] ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def fun_vector_scalar ( self , x ) : <EOL> return np . sin ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) * np . log ( x [ <NUM_LIT:0> ] ) <EOL> def wrong_dimensions_fun ( self , x ) : <EOL> return np . array ( [ x ** <NUM_LIT:2> , np . tan ( x ) , np . exp ( x ) ] ) <EOL> def jac_vector_scalar ( self , x ) : <EOL> return np . array ( [ <EOL> x [ <NUM_LIT:1> ] * np . cos ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) * np . log ( x [ <NUM_LIT:0> ] ) + <EOL> np . sin ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) / x [ <NUM_LIT:0> ] , <EOL> x [ <NUM_LIT:0> ] * np . cos ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) * np . log ( x [ <NUM_LIT:0> ] ) <EOL> ] ) <EOL> def fun_vector_vector ( self , x ) : <EOL> return np . array ( [ <EOL> x [ <NUM_LIT:0> ] * np . sin ( x [ <NUM_LIT:1> ] ) , <EOL> x [ <NUM_LIT:1> ] * np . cos ( x [ <NUM_LIT:0> ] ) , <EOL> x [ <NUM_LIT:0> ] ** <NUM_LIT:3> * x [ <NUM_LIT:1> ] ** - <NUM_LIT:0.5> <EOL> ] ) <EOL> def jac_vector_vector ( self , x ) : <EOL> return np . array ( [ <EOL> [ np . sin ( x [ <NUM_LIT:1> ] ) , x [ <NUM_LIT:0> ] * np . cos ( x [ <NUM_LIT:1> ] ) ] , <EOL> [ - x [ <NUM_LIT:1> ] * np . sin ( x [ <NUM_LIT:0> ] ) , np . cos ( x [ <NUM_LIT:0> ] ) ] , <EOL> [ <NUM_LIT:3> * x [ <NUM_LIT:0> ] ** <NUM_LIT:2> * x [ <NUM_LIT:1> ] ** - <NUM_LIT:0.5> , - <NUM_LIT:0.5> * x [ <NUM_LIT:0> ] ** <NUM_LIT:3> * x [ <NUM_LIT:1> ] ** - <NUM_LIT> ] <EOL> ] ) <EOL> def fun_parametrized ( self , x , c0 , c1 = <NUM_LIT:1.0> ) : <EOL> return np . array ( [ np . exp ( c0 * x [ <NUM_LIT:0> ] ) , np . exp ( c1 * x [ <NUM_LIT:1> ] ) ] ) <EOL> def jac_parametrized ( self , x , c0 , c1 = <NUM_LIT:0.1> ) : <EOL> return np . array ( [ <EOL> [ c0 * np . exp ( c0 * x [ <NUM_LIT:0> ] ) , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , c1 * np . exp ( c1 * x [ <NUM_LIT:1> ] ) ] <EOL> ] ) <EOL> def fun_with_nan ( self , x ) : <EOL> return x if np . abs ( x ) <= <NUM_LIT> else np . nan <EOL> def jac_with_nan ( self , x ) : <EOL> return <NUM_LIT:1.0> if np . abs ( x ) <= <NUM_LIT> else np . nan <EOL> def fun_zero_jacobian ( self , x ) : <EOL> return np . array ( [ x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] , np . cos ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) ] ) <EOL> def jac_zero_jacobian ( self , x ) : <EOL> return np . array ( [ <EOL> [ x [ <NUM_LIT:1> ] , x [ <NUM_LIT:0> ] ] , <EOL> [ - x [ <NUM_LIT:1> ] * np . sin ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) , - x [ <NUM_LIT:0> ] * np . sin ( x [ <NUM_LIT:0> ] * x [ <NUM_LIT:1> ] ) ] <EOL> ] ) <EOL> def fun_non_numpy ( self , x ) : <EOL> return math . exp ( x ) <EOL> def jac_non_numpy ( self , x ) : <EOL> return math . exp ( x ) <EOL> def test_scalar_scalar ( self ) : <EOL> x0 = <NUM_LIT:1.0> <EOL> jac_diff_2 = approx_derivative ( self . fun_scalar_scalar , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_diff_3 = approx_derivative ( self . fun_scalar_scalar , x0 ) <EOL> jac_diff_4 = approx_derivative ( self . fun_scalar_scalar , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_true = self . jac_scalar_scalar ( x0 ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_4 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_scalar_vector ( self ) : <EOL> x0 = <NUM_LIT:0.5> <EOL> jac_diff_2 = approx_derivative ( self . fun_scalar_vector , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_diff_3 = approx_derivative ( self . fun_scalar_vector , x0 ) <EOL> jac_diff_4 = approx_derivative ( self . fun_scalar_vector , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_true = self . jac_scalar_vector ( np . atleast_1d ( x0 ) ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_4 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_vector_scalar ( self ) : <EOL> x0 = np . array ( [ <NUM_LIT> , - <NUM_LIT:0.5> ] ) <EOL> jac_diff_2 = approx_derivative ( self . fun_vector_scalar , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_diff_3 = approx_derivative ( self . fun_vector_scalar , x0 ) <EOL> jac_diff_4 = approx_derivative ( self . fun_vector_scalar , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_true = self . jac_vector_scalar ( x0 ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_4 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_vector_vector ( self ) : <EOL> x0 = np . array ( [ - <NUM_LIT> , <NUM_LIT> ] ) <EOL> jac_diff_2 = approx_derivative ( self . fun_vector_vector , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_diff_3 = approx_derivative ( self . fun_vector_vector , x0 ) <EOL> jac_diff_4 = approx_derivative ( self . fun_vector_vector , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_true = self . jac_vector_vector ( x0 ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_4 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_wrong_dimensions ( self ) : <EOL> x0 = <NUM_LIT:1.0> <EOL> assert_raises ( RuntimeError , approx_derivative , <EOL> self . wrong_dimensions_fun , x0 ) <EOL> f0 = self . wrong_dimensions_fun ( np . atleast_1d ( x0 ) ) <EOL> assert_raises ( ValueError , approx_derivative , <EOL> self . wrong_dimensions_fun , x0 , f0 = f0 ) <EOL> def test_custom_rel_step ( self ) : <EOL> x0 = np . array ( [ - <NUM_LIT:0.1> , <NUM_LIT:0.1> ] ) <EOL> jac_diff_2 = approx_derivative ( self . fun_vector_vector , x0 , <EOL> method = '<STR_LIT>' , rel_step = <NUM_LIT> ) <EOL> jac_diff_3 = approx_derivative ( self . fun_vector_vector , x0 , <EOL> rel_step = <NUM_LIT> ) <EOL> jac_true = self . jac_vector_vector ( x0 ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_options ( self ) : <EOL> x0 = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) <EOL> c0 = - <NUM_LIT:1.0> <EOL> c1 = <NUM_LIT:1.0> <EOL> lb = <NUM_LIT:0.0> <EOL> ub = <NUM_LIT> <EOL> f0 = self . fun_parametrized ( x0 , c0 , c1 = c1 ) <EOL> rel_step = np . array ( [ - <NUM_LIT> , <NUM_LIT> ] ) <EOL> jac_true = self . jac_parametrized ( x0 , c0 , c1 ) <EOL> jac_diff_2 = approx_derivative ( <EOL> self . fun_parametrized , x0 , method = '<STR_LIT>' , rel_step = rel_step , <EOL> f0 = f0 , args = ( c0 , ) , kwargs = dict ( c1 = c1 ) , bounds = ( lb , ub ) ) <EOL> jac_diff_3 = approx_derivative ( <EOL> self . fun_parametrized , x0 , rel_step = rel_step , <EOL> f0 = f0 , args = ( c0 , ) , kwargs = dict ( c1 = c1 ) , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_with_bounds_2_point ( self ) : <EOL> lb = - np . ones ( <NUM_LIT:2> ) <EOL> ub = np . ones ( <NUM_LIT:2> ) <EOL> x0 = np . array ( [ - <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert_raises ( ValueError , approx_derivative , <EOL> self . fun_vector_vector , x0 , bounds = ( lb , ub ) ) <EOL> x0 = np . array ( [ - <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) <EOL> jac_diff = approx_derivative ( self . fun_vector_vector , x0 , <EOL> method = '<STR_LIT>' , bounds = ( lb , ub ) ) <EOL> jac_true = self . jac_vector_vector ( x0 ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> def test_with_bounds_3_point ( self ) : <EOL> lb = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) <EOL> ub = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> x0 = np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> ] ) <EOL> jac_true = self . jac_vector_vector ( x0 ) <EOL> jac_diff = approx_derivative ( self . fun_vector_vector , x0 ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( self . fun_vector_vector , x0 , <EOL> bounds = ( lb , np . inf ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( self . fun_vector_vector , x0 , <EOL> bounds = ( - np . inf , ub ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( self . fun_vector_vector , x0 , <EOL> bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> def test_tight_bounds ( self ) : <EOL> x0 = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> lb = x0 - <NUM_LIT> <EOL> ub = x0 + <NUM_LIT> <EOL> jac_true = self . jac_vector_vector ( x0 ) <EOL> jac_diff = approx_derivative ( <EOL> self . fun_vector_vector , x0 , method = '<STR_LIT>' , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( <EOL> self . fun_vector_vector , x0 , method = '<STR_LIT>' , <EOL> rel_step = <NUM_LIT> , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( <EOL> self . fun_vector_vector , x0 , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff , jac_true , rtol = <NUM_LIT> ) <EOL> jac_diff = approx_derivative ( <EOL> self . fun_vector_vector , x0 , rel_step = <NUM_LIT> , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_true , jac_diff , rtol = <NUM_LIT> ) <EOL> def test_bound_switches ( self ) : <EOL> lb = - <NUM_LIT> <EOL> ub = <NUM_LIT> <EOL> x0 = <NUM_LIT:0.0> <EOL> jac_true = self . jac_with_nan ( x0 ) <EOL> jac_diff_2 = approx_derivative ( <EOL> self . fun_with_nan , x0 , method = '<STR_LIT>' , rel_step = <NUM_LIT> , <EOL> bounds = ( lb , ub ) ) <EOL> jac_diff_3 = approx_derivative ( <EOL> self . fun_with_nan , x0 , rel_step = <NUM_LIT> , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> x0 = <NUM_LIT> <EOL> jac_true = self . jac_with_nan ( x0 ) <EOL> jac_diff_2 = approx_derivative ( <EOL> self . fun_with_nan , x0 , method = '<STR_LIT>' , rel_step = <NUM_LIT> , <EOL> bounds = ( lb , ub ) ) <EOL> jac_diff_3 = approx_derivative ( <EOL> self . fun_with_nan , x0 , rel_step = <NUM_LIT> , bounds = ( lb , ub ) ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> def test_non_numpy ( self ) : <EOL> x0 = <NUM_LIT:1.0> <EOL> jac_true = self . jac_non_numpy ( x0 ) <EOL> jac_diff_2 = approx_derivative ( self . jac_non_numpy , x0 , <EOL> method = '<STR_LIT>' ) <EOL> jac_diff_3 = approx_derivative ( self . jac_non_numpy , x0 ) <EOL> assert_allclose ( jac_diff_2 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_allclose ( jac_diff_3 , jac_true , rtol = <NUM_LIT> ) <EOL> assert_raises ( TypeError , approx_derivative , self . jac_non_numpy , x0 , <EOL> ** dict ( method = '<STR_LIT>' ) ) <EOL> def test_check_derivative ( self ) : <EOL> x0 = np . array ( [ - <NUM_LIT> , <NUM_LIT:10> ] ) <EOL> accuracy = check_derivative ( self . fun_vector_vector , <EOL> self . jac_vector_vector , x0 ) <EOL> assert_ ( accuracy < <NUM_LIT> ) <EOL> accuracy = check_derivative ( self . fun_vector_vector , <EOL> self . jac_vector_vector , x0 ) <EOL> assert_ ( accuracy < <NUM_LIT> ) <EOL> x0 = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> accuracy = check_derivative ( self . fun_zero_jacobian , <EOL> self . jac_zero_jacobian , x0 ) <EOL> assert_ ( accuracy == <NUM_LIT:0> ) <EOL> accuracy = check_derivative ( self . fun_zero_jacobian , <EOL> self . jac_zero_jacobian , x0 ) <EOL> assert_ ( accuracy == <NUM_LIT:0> ) <EOL> class TestApproxDerivativeSparse ( object ) : <EOL> def __init__ ( self ) : <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> self . n = <NUM_LIT:50> <EOL> self . lb = - <NUM_LIT:0.1> * ( <NUM_LIT:1> + np . arange ( self . n ) ) <EOL> self . ub = <NUM_LIT:0.1> * ( <NUM_LIT:1> + np . arange ( self . n ) ) <EOL> self . x0 = np . empty ( self . n ) <EOL> self . x0 [ : : <NUM_LIT:2> ] = ( <NUM_LIT:1> - <NUM_LIT> ) * self . lb [ : : <NUM_LIT:2> ] <EOL> self . x0 [ <NUM_LIT:1> : : <NUM_LIT:2> ] = ( <NUM_LIT:1> - <NUM_LIT> ) * self . ub [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> self . J_true = self . jac ( self . x0 ) <EOL> def fun ( self , x ) : <EOL> e = x [ <NUM_LIT:1> : ] ** <NUM_LIT:3> - x [ : - <NUM_LIT:1> ] ** <NUM_LIT:2> <EOL> return np . hstack ( ( <NUM_LIT:0> , <NUM_LIT:3> * e ) ) + np . hstack ( ( <NUM_LIT:2> * e , <NUM_LIT:0> ) ) <EOL> def jac ( self , x ) : <EOL> n = x . size <EOL> J = np . zeros ( ( n , n ) ) <EOL> J [ <NUM_LIT:0> , <NUM_LIT:0> ] = - <NUM_LIT:4> * x [ <NUM_LIT:0> ] <EOL> J [ <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:6> * x [ <NUM_LIT:1> ] ** <NUM_LIT:2> <EOL> for i in range ( <NUM_LIT:1> , n - <NUM_LIT:1> ) : <EOL> J [ i , i - <NUM_LIT:1> ] = - <NUM_LIT:6> * x [ i - <NUM_LIT:1> ] <EOL> J [ i , i ] = <NUM_LIT:9> * x [ i ] ** <NUM_LIT:2> - <NUM_LIT:4> * x [ i ] <EOL> J [ i , i + <NUM_LIT:1> ] = <NUM_LIT:6> * x [ i + <NUM_LIT:1> ] ** <NUM_LIT:2> <EOL> J [ - <NUM_LIT:1> , - <NUM_LIT:1> ] = <NUM_LIT:9> * x [ - <NUM_LIT:1> ] ** <NUM_LIT:2> <EOL> J [ - <NUM_LIT:1> , - <NUM_LIT:2> ] = - <NUM_LIT:6> * x [ - <NUM_LIT:2> ] <EOL> return J <EOL> def structure ( self , n ) : <EOL> A = np . zeros ( ( n , n ) , dtype = int ) <EOL> A [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> A [ <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:1> , n - <NUM_LIT:1> ) : <EOL> A [ i , i - <NUM_LIT:1> : i + <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> A [ - <NUM_LIT:1> , - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> A [ - <NUM_LIT:1> , - <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> return A <EOL> def test_all ( self ) : <EOL> A = self . structure ( self . n ) <EOL> order = np . arange ( self . n ) <EOL> groups_1 = group_columns ( A , order ) <EOL> np . random . shuffle ( order ) <EOL> groups_2 = group_columns ( A , order ) <EOL> for method , groups , l , u in product ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ groups_1 , groups_2 ] , <EOL> [ - np . inf , self . lb ] , [ np . inf , self . ub ] ) : <EOL> J = approx_derivative ( self . fun , self . x0 , method = method , <EOL> bounds = ( l , u ) , sparsity = ( A , groups ) ) <EOL> assert_ ( isinstance ( J , csr_matrix ) ) <EOL> assert_allclose ( J . toarray ( ) , self . J_true , rtol = <NUM_LIT> ) <EOL> rel_step = <NUM_LIT> * np . ones_like ( self . x0 ) <EOL> rel_step [ : : <NUM_LIT:2> ] *= - <NUM_LIT:1> <EOL> J = approx_derivative ( self . fun , self . x0 , method = method , <EOL> rel_step = rel_step , sparsity = ( A , groups ) ) <EOL> assert_allclose ( J . toarray ( ) , self . J_true , rtol = <NUM_LIT> ) <EOL> def test_no_precomputed_groups ( self ) : <EOL> A = self . structure ( self . n ) <EOL> J = approx_derivative ( self . fun , self . x0 , sparsity = A ) <EOL> assert_allclose ( J . toarray ( ) , self . J_true , rtol = <NUM_LIT> ) <EOL> def test_equivalence ( self ) : <EOL> structure = np . ones ( ( self . n , self . n ) , dtype = int ) <EOL> groups = np . arange ( self . n ) <EOL> for method in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> J_dense = approx_derivative ( self . fun , self . x0 , method = method ) <EOL> J_sparse = approx_derivative ( <EOL> self . fun , self . x0 , sparsity = ( structure , groups ) , method = method ) <EOL> assert_equal ( J_dense , J_sparse . toarray ( ) ) <EOL> def test_check_derivative ( self ) : <EOL> def jac ( x ) : <EOL> return csr_matrix ( self . jac ( x ) ) <EOL> accuracy = check_derivative ( self . fun , jac , self . x0 , <EOL> bounds = ( self . lb , self . ub ) ) <EOL> assert_ ( accuracy < <NUM_LIT> ) <EOL> accuracy = check_derivative ( self . fun , jac , self . x0 , <EOL> bounds = ( self . lb , self . ub ) ) <EOL> assert_ ( accuracy < <NUM_LIT> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_module_suite ( ) </s>
