@@ -27,7 +27,7 @@ AUTHORS:
2727# https://www.gnu.org/licenses/
2828# ****************************************************************************
2929
30- from cysignals.signals cimport sig_on, sig_off
30+ from cysignals.signals cimport sig_on, sig_off, sig_block, sig_unblock
3131from memory_allocator cimport MemoryAllocator
3232
3333from sage.rings.real_double import RDF
@@ -50,6 +50,7 @@ cdef class compiled_integrand:
5050 return 0
5151
5252cdef double c_f(double t, void * params) noexcept:
53+ sig_block()
5354 cdef double value
5455 cdef PyFunctionWrapper wrapper
5556 wrapper = < PyFunctionWrapper> params
@@ -58,16 +59,16 @@ cdef double c_f(double t, void *params) noexcept:
5859 value = wrapper.the_function(t, wrapper.the_parameters)
5960 else :
6061 value = wrapper.the_function(t)
61- except Exception as msg :
62+ except Exception as e :
6263 try :
63- if str (msg ).strip():
64- print (msg )
64+ if str (e ).strip():
65+ print (e )
6566 else :
6667 print (f" Unable to evaluate function at {t}" )
6768 except Exception :
6869 pass
69- return 0
70-
70+ value = 0
71+ sig_unblock()
7172 return value
7273
7374
@@ -121,12 +122,12 @@ def numerical_integral(func, a, b=None,
121122
122123 EXAMPLES:
123124
124- To integrate the function `x^ 2` from 0 to 1, we do ::
125+ To integrate the function `x^ 2` from 0 to 1, we do::
125126
126127 sage: numerical_integral( x^ 2, 0, 1, max_points=100)
127128 ( 0. 3333333333333333, 3. 700743415417188e-15)
128129
129- To integrate the function `\s in( x) ^ 3 + \s in( x) ` we do ::
130+ To integrate the function `\s in( x) ^ 3 + \s in( x) ` we do::
130131
131132 sage: numerical_integral( sin( x) ^ 3 + sin( x) , 0, pi)
132133 ( 3. 333333333333333, 3. 700743415417188e-14)
@@ -177,18 +178,18 @@ def numerical_integral(func, a, b=None,
177178 + Infinity or -Infinity in the interval argument. For example::
178179
179180 sage: f = exp( -x)
180- sage: numerical_integral( f, 0, + Infinity) # random output
181+ sage: numerical_integral( f, 0, + Infinity) # abs tol 1e-6
181182 ( 0. 99999999999957279, 1. 8429811298996553e-07)
182183
183184 Note the coercion to the real field RR, which prevents underflow::
184185
185186 sage: f = exp( -x** 2)
186- sage: numerical_integral( f, -Infinity, + Infinity) # random output
187+ sage: numerical_integral( f, -Infinity, + Infinity) # abs tol 1e-6
187188 ( 1. 7724538509060035, 3. 4295192165889879e-08)
188189
189190 One can integrate any real-valued callable function::
190191
191- sage: numerical_integral( lambda x: abs( zeta( x)) , [1.1,1.5 ]) # random output
192+ sage: numerical_integral( lambda x: abs( zeta( x)) , [1.1,1.5 ]) # abs tol 1e-6
192193 ( 1. 8488570602160455, 2. 052643677492633e-14)
193194
194195 We can also numerically integrate symbolic expressions using either this
@@ -264,6 +265,33 @@ def numerical_integral(func, a, b=None,
264265 sage: h( x) = x
265266 sage: numerical_integral( h,0,1) [0 ] # abs tol 1e-8
266267 0. 5
268+
269+ Ensure :issue:`30379` is fixed::
270+
271+ sage: g( x) = gamma_inc( 2,11/5) * x
272+ sage: numerical_integral( g( x) , 2, 5) # abs tol 1e-10
273+ ( 3. 722986120974418, 4. 1333449118656977e-14)
274+
275+ Check interruptibility::
276+
277+ sage: from sage. doctest. util import ensure_interruptible_after
278+ sage: def f( x) :
279+ .... : sleep( 0. 01r)
280+ .... : return x
281+ sage: with ensure_interruptible_after( 0. 3) : numerical_integral( f, 0, 1)
282+
283+ Unfortunately the current implementation means if a single function call
284+ takes a long time then it will take a while to be interrupted,
285+ but cysignals does not yet support such sophisticated nested exception::
286+
287+ sage: def f( x) :
288+ .... : sleep( 1r)
289+ .... : return x
290+ sage: with ensure_interruptible_after( 0. 5) : numerical_integral( f, 0, 1)
291+ Traceback ( most recent call last) :
292+ ...
293+ RuntimeError: Function is not interruptible within 0. 5000 seconds, only after 1. 0... seconds
294+
267295 """
268296 cdef double abs_err # step size
269297 cdef double result
@@ -570,7 +598,7 @@ def monte_carlo_integral(func, xl, xu, size_t calls, algorithm='plain',
570598 cdef gsl_monte_plain_state* state_plain = NULL
571599 cdef gsl_monte_miser_state* state_miser = NULL
572600 cdef gsl_monte_vegas_state* state_vegas = NULL
573- cdef gsl_rng_type * type_rng
601+ cdef const gsl_rng_type * type_rng
574602 cdef gsl_rng * _rng
575603 cdef size_t dim
576604 cdef double * _xl
0 commit comments