Skip to content

Commit b66d785

Browse files
committed
Call sig_block() in integral callback
1 parent f4adc25 commit b66d785

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/sage/calculus/integration.pyx

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3131
from memory_allocator cimport MemoryAllocator
3232

3333
from sage.rings.real_double import RDF
@@ -50,6 +50,7 @@ cdef class compiled_integrand:
5050
return 0
5151

5252
cdef 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 `\sin(x)^3 + \sin(x)` we do ::
130+
To integrate the function `\sin(x)^3 + \sin(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

src/sage/doctest/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,14 +851,14 @@ def ensure_interruptible_after(seconds: float, max_wait_after_interrupt: float =
851851
sage: with ensure_interruptible_after(1) as data: uninterruptible_sleep(2r)
852852
Traceback (most recent call last):
853853
...
854-
RuntimeError: Function is not interruptible within 1.0000 seconds, only after 2.00... seconds
855-
sage: data # abs tol 0.01
854+
RuntimeError: Function is not interruptible within 1.0000 seconds, only after 2.0... seconds
855+
sage: data # abs tol 0.1
856856
{'alarm_raised': True, 'elapsed': 2.0}
857857
sage: with ensure_interruptible_after(1): uninterruptible_sleep(2r); raise RuntimeError
858858
Traceback (most recent call last):
859859
...
860-
RuntimeError: Function is not interruptible within 1.0000 seconds, only after 2.00... seconds
861-
sage: data # abs tol 0.01
860+
RuntimeError: Function is not interruptible within 1.0000 seconds, only after 2.0... seconds
861+
sage: data # abs tol 0.1
862862
{'alarm_raised': True, 'elapsed': 2.0}
863863
864864
::
@@ -867,7 +867,7 @@ def ensure_interruptible_after(seconds: float, max_wait_after_interrupt: float =
867867
Traceback (most recent call last):
868868
...
869869
ValueError
870-
sage: data # abs tol 0.01
870+
sage: data # abs tol 0.1
871871
{'alarm_raised': False, 'elapsed': 0.0}
872872
"""
873873
from cysignals.alarm import alarm, cancel_alarm, AlarmInterrupt

src/sage/libs/gsl/rng.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cdef extern from "gsl/gsl_rng.h":
7070
cdef gsl_rng_type *gsl_rng_default
7171
unsigned long int gsl_rng_default_seed
7272

73-
gsl_rng *gsl_rng_alloc ( gsl_rng_type * T)
73+
gsl_rng *gsl_rng_alloc ( const gsl_rng_type * T)
7474
int gsl_rng_memcpy (gsl_rng * dest, gsl_rng * src)
7575
gsl_rng *gsl_rng_clone ( gsl_rng * r)
7676

0 commit comments

Comments
 (0)