<s> from __future__ import division , print_function , absolute_import <EOL> import warnings <EOL> import numpy as np <EOL> from numpy import cos , sin , pi <EOL> from numpy . testing import TestCase , run_module_suite , assert_equal , assert_almost_equal , assert_allclose , assert_ <EOL> from scipy . integrate import ( quadrature , romberg , romb , newton_cotes , <EOL> cumtrapz , quad , simps ) <EOL> from scipy . integrate . quadrature import AccuracyWarning <EOL> class TestQuadrature ( TestCase ) : <EOL> def quad ( self , x , a , b , args ) : <EOL> raise NotImplementedError <EOL> def test_quadrature ( self ) : <EOL> def myfunc ( x , n , z ) : <EOL> return cos ( n * x - z * sin ( x ) ) / pi <EOL> val , err = quadrature ( myfunc , <NUM_LIT:0> , pi , ( <NUM_LIT:2> , <NUM_LIT> ) ) <EOL> table_val = <NUM_LIT> <EOL> assert_almost_equal ( val , table_val , decimal = <NUM_LIT:7> ) <EOL> def test_quadrature_rtol ( self ) : <EOL> def myfunc ( x , n , z ) : <EOL> return <NUM_LIT> * cos ( n * x - z * sin ( x ) ) / pi <EOL> val , err = quadrature ( myfunc , <NUM_LIT:0> , pi , ( <NUM_LIT:2> , <NUM_LIT> ) , rtol = <NUM_LIT> ) <EOL> table_val = <NUM_LIT> * <NUM_LIT> <EOL> assert_allclose ( val , table_val , rtol = <NUM_LIT> ) <EOL> def test_quadrature_miniter ( self ) : <EOL> def myfunc ( x , n , z ) : <EOL> return cos ( n * x - z * sin ( x ) ) / pi <EOL> table_val = <NUM_LIT> <EOL> for miniter in [ <NUM_LIT:5> , <NUM_LIT> ] : <EOL> val , err = quadrature ( myfunc , <NUM_LIT:0> , pi , ( <NUM_LIT:2> , <NUM_LIT> ) , miniter = miniter ) <EOL> assert_almost_equal ( val , table_val , decimal = <NUM_LIT:7> ) <EOL> assert_ ( err < <NUM_LIT:1.0> ) <EOL> def test_quadrature_single_args ( self ) : <EOL> def myfunc ( x , n ) : <EOL> return <NUM_LIT> * cos ( n * x - <NUM_LIT> * sin ( x ) ) / pi <EOL> val , err = quadrature ( myfunc , <NUM_LIT:0> , pi , args = <NUM_LIT:2> , rtol = <NUM_LIT> ) <EOL> table_val = <NUM_LIT> * <NUM_LIT> <EOL> assert_allclose ( val , table_val , rtol = <NUM_LIT> ) <EOL> def test_romberg ( self ) : <EOL> def myfunc ( x , n , z ) : <EOL> return cos ( n * x - z * sin ( x ) ) / pi <EOL> val = romberg ( myfunc , <NUM_LIT:0> , pi , args = ( <NUM_LIT:2> , <NUM_LIT> ) ) <EOL> table_val = <NUM_LIT> <EOL> assert_almost_equal ( val , table_val , decimal = <NUM_LIT:7> ) <EOL> def test_romberg_rtol ( self ) : <EOL> def myfunc ( x , n , z ) : <EOL> return <NUM_LIT> * cos ( n * x - z * sin ( x ) ) / pi <EOL> val = romberg ( myfunc , <NUM_LIT:0> , pi , args = ( <NUM_LIT:2> , <NUM_LIT> ) , rtol = <NUM_LIT> ) <EOL> table_val = <NUM_LIT> * <NUM_LIT> <EOL> assert_allclose ( val , table_val , rtol = <NUM_LIT> ) <EOL> def test_romb ( self ) : <EOL> assert_equal ( romb ( np . arange ( <NUM_LIT> ) ) , <NUM_LIT> ) <EOL> def test_romb_gh_3731 ( self ) : <EOL> x = np . arange ( <NUM_LIT:2> ** <NUM_LIT:4> + <NUM_LIT:1> ) <EOL> y = np . cos ( <NUM_LIT> * x ) <EOL> val = romb ( y ) <EOL> val2 , err = quad ( lambda x : np . cos ( <NUM_LIT> * x ) , x . min ( ) , x . max ( ) ) <EOL> assert_allclose ( val , val2 , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , category = AccuracyWarning ) <EOL> val3 = romberg ( lambda x : np . cos ( <NUM_LIT> * x ) , x . min ( ) , x . max ( ) , <EOL> divmax = <NUM_LIT:4> ) <EOL> assert_allclose ( val , val3 , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> def test_non_dtype ( self ) : <EOL> import math <EOL> valmath = romberg ( math . sin , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> expected_val = <NUM_LIT> <EOL> assert_almost_equal ( valmath , expected_val , decimal = <NUM_LIT:7> ) <EOL> def test_newton_cotes ( self ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:1> <EOL> wts , errcoff = newton_cotes ( n , <NUM_LIT:1> ) <EOL> assert_equal ( wts , n * np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) ) <EOL> assert_almost_equal ( errcoff , - n ** <NUM_LIT:3> / <NUM_LIT> ) <EOL> n = <NUM_LIT:2> <EOL> wts , errcoff = newton_cotes ( n , <NUM_LIT:1> ) <EOL> assert_almost_equal ( wts , n * np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT:1.0> ] ) / <NUM_LIT> ) <EOL> assert_almost_equal ( errcoff , - n ** <NUM_LIT:5> / <NUM_LIT> ) <EOL> n = <NUM_LIT:3> <EOL> wts , errcoff = newton_cotes ( n , <NUM_LIT:1> ) <EOL> assert_almost_equal ( wts , n * np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> ] ) / <NUM_LIT> ) <EOL> assert_almost_equal ( errcoff , - n ** <NUM_LIT:5> / <NUM_LIT> ) <EOL> n = <NUM_LIT:4> <EOL> wts , errcoff = newton_cotes ( n , <NUM_LIT:1> ) <EOL> assert_almost_equal ( wts , n * np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) / <NUM_LIT> ) <EOL> assert_almost_equal ( errcoff , - n ** <NUM_LIT:7> / <NUM_LIT> ) <EOL> def test_newton_cotes2 ( self ) : <EOL> """<STR_LIT>""" <EOL> x = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> y = x ** <NUM_LIT:2> <EOL> wts , errcoff = newton_cotes ( x ) <EOL> exact_integral = <NUM_LIT> / <NUM_LIT:3> <EOL> numeric_integral = np . dot ( wts , y ) <EOL> assert_almost_equal ( numeric_integral , exact_integral ) <EOL> x = np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> y = x ** <NUM_LIT:2> <EOL> wts , errcoff = newton_cotes ( x ) <EOL> exact_integral = <NUM_LIT> <EOL> numeric_integral = np . dot ( wts , y ) <EOL> assert_almost_equal ( numeric_integral , exact_integral ) <EOL> def test_simps ( self ) : <EOL> y = np . arange ( <NUM_LIT> ) <EOL> assert_equal ( simps ( y ) , <NUM_LIT> ) <EOL> assert_equal ( simps ( y , dx = <NUM_LIT:0.5> ) , <NUM_LIT:64> ) <EOL> assert_equal ( simps ( y , x = np . linspace ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT> ) ) , <NUM_LIT:32> ) <EOL> y = np . arange ( <NUM_LIT:4> ) <EOL> x = <NUM_LIT:2> ** y <EOL> assert_equal ( simps ( y , x = x , even = '<STR_LIT>' ) , <NUM_LIT> ) <EOL> assert_equal ( simps ( y , x = x , even = '<STR_LIT>' ) , <NUM_LIT> ) <EOL> assert_equal ( simps ( y , x = x , even = '<STR_LIT>' ) , <NUM_LIT> ) <EOL> class TestCumtrapz ( TestCase ) : <EOL> def test_1d ( self ) : <EOL> x = np . linspace ( - <NUM_LIT:2> , <NUM_LIT:2> , num = <NUM_LIT:5> ) <EOL> y = x <EOL> y_int = cumtrapz ( y , x , initial = <NUM_LIT:0> ) <EOL> y_expected = [ <NUM_LIT:0.> , - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT:0.> ] <EOL> assert_allclose ( y_int , y_expected ) <EOL> y_int = cumtrapz ( y , x , initial = None ) <EOL> assert_allclose ( y_int , y_expected [ <NUM_LIT:1> : ] ) <EOL> def test_y_nd_x_nd ( self ) : <EOL> x = np . arange ( <NUM_LIT:3> * <NUM_LIT:2> * <NUM_LIT:4> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> y = x <EOL> y_int = cumtrapz ( y , x , initial = <NUM_LIT:0> ) <EOL> y_expected = np . array ( [ [ [ <NUM_LIT:0.> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ] ) <EOL> assert_allclose ( y_int , y_expected ) <EOL> shapes = [ ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ) ] <EOL> for axis , shape in zip ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , shapes ) : <EOL> y_int = cumtrapz ( y , x , initial = <NUM_LIT> , axis = axis ) <EOL> assert_equal ( y_int . shape , ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ) ) <EOL> y_int = cumtrapz ( y , x , initial = None , axis = axis ) <EOL> assert_equal ( y_int . shape , shape ) <EOL> def test_y_nd_x_1d ( self ) : <EOL> y = np . arange ( <NUM_LIT:3> * <NUM_LIT:2> * <NUM_LIT:4> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> x = np . arange ( <NUM_LIT:4> ) ** <NUM_LIT:2> <EOL> ys_expected = ( <EOL> np . array ( [ [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ] ) , <EOL> np . array ( [ [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ] ) , <EOL> np . array ( [ [ [ <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ] ) ) <EOL> for axis , y_expected in zip ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , ys_expected ) : <EOL> y_int = cumtrapz ( y , x = x [ : y . shape [ axis ] ] , axis = axis , initial = None ) <EOL> assert_allclose ( y_int , y_expected ) <EOL> def test_x_none ( self ) : <EOL> y = np . linspace ( - <NUM_LIT:2> , <NUM_LIT:2> , num = <NUM_LIT:5> ) <EOL> y_int = cumtrapz ( y ) <EOL> y_expected = [ - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT:0.> ] <EOL> assert_allclose ( y_int , y_expected ) <EOL> y_int = cumtrapz ( y , initial = <NUM_LIT> ) <EOL> y_expected = [ <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT:0.> ] <EOL> assert_allclose ( y_int , y_expected ) <EOL> y_int = cumtrapz ( y , dx = <NUM_LIT:3> ) <EOL> y_expected = [ - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT:0.> ] <EOL> assert_allclose ( y_int , y_expected ) <EOL> y_int = cumtrapz ( y , dx = <NUM_LIT:3> , initial = <NUM_LIT> ) <EOL> y_expected = [ <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT:0.> ] <EOL> assert_allclose ( y_int , y_expected ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
