Changes between Version 2 and Version 3 of Ticket #24425
- Timestamp:
- Dec 26, 2017, 7:25:54 AM (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #24425
-
Property
Summary
changed from
Minimize random_expr's raising exceptions
toFix inherently failing random_expr doctest
-
Property
Summary
changed from
-
Ticket #24425 – Description
v2 v3 1 Applying the following minimal patch makes a completely unrelated doctest fail:1 The docs for `random_expr` read: 2 2 {{{ 3 diff --git a/src/sage/functions/all.py b/src/sage/functions/all.py 4 index e7f1ea0efa..d26f83f2f9 100644 5 --- a/src/sage/functions/all.py 6 +++ b/src/sage/functions/all.py 7 @@ -4,7 +4,7 @@ from sage.misc.lazy_import import lazy_import 8 9 lazy_import('sage.functions.piecewise_old', 'Piecewise') # deprecated 10 lazy_import('sage.functions.piecewise', 'piecewise') 11 -lazy_import('sage.functions.error', ['erf', 'erfc', 'erfi', 'erfinv']) 12 +lazy_import('sage.functions.error', ['erf', 'erfc', 'erfi', 'erfinv', 'fresnel_test1', 'fresnel_test2']) 13 14 from .trig import ( sin, cos, sec, csc, cot, tan, 15 asin, acos, atan, 16 diff --git a/src/sage/functions/error.py b/src/sage/functions/error.py 17 index 9a9b03cd6d..ec0a574814 100644 18 --- a/src/sage/functions/error.py 19 +++ b/src/sage/functions/error.py 20 @@ -548,3 +548,15 @@ erfinv = Function_erfinv() 21 from sage.structure.sage_object import register_unpickle_override 22 register_unpickle_override('sage.functions.other', 'Function_erf', Function_erf) 23 24 +class Function_Fresnel_test1(BuiltinFunction): 25 + def __init__(self): 26 + BuiltinFunction.__init__(self, "fresnel_test1") 27 + 28 +fresnel_test1 = Function_Fresnel_test1() 29 + 30 +class Function_Fresnel_test2(BuiltinFunction): 31 + def __init__(self): 32 + BuiltinFunction.__init__(self, "fresnel_test2") 33 + 34 +fresnel_test2 = Function_Fresnel_test2() 35 + 3 This function will often raise an error because it tries to create 4 an erroneous expression (such as a division by zero). 36 5 }}} 6 It has the following doctest: 37 7 {{{ 38 File "src/sage/symbolic/random_tests.py", line 265, in sage.symbolic.random_tests.? 39 Failed example: 40 random_expr(50, nvars=3, coeff_generator=CDF.random_element) # random 8 sage: from sage.symbolic.random_tests import * 9 sage: set_random_seed(53) 10 sage: random_expr(50, nvars=3, coeff_generator=CDF.random_element) # random 11 (v1^(0.97134084277 + 0.195868299334*I)/csc(-pi + v1^2 + v3) + sgn(1/ 41 12 ... 42 File "sage/symbolic/expression.pyx", line 3518, in sage.symbolic.expression.Expression._div_ (build/cythonized/sage/symbolic/expression.cpp:25588)43 raise ZeroDivisionError("Symbolic division by zero")44 ZeroDivisionError: Symbolic division by zero45 13 }}} 46 The reason why the doctest fails is that, despite fixing the random seed, the functions and symbols picked by `random_expr` are different. This, at some point in the buildup of the expression, leads to the sub-expression `kronecker_delta(-0.6165326641917295 + 0.15117833554974136*I, e)` which immediately evaluates to `0`. The `0` progresses eventually into a denominator, causing the error. `random_expr` is not written to avoid such errors if they happen, nor does it avoid other exceptions thrown. The author of the doctest apparently just chose a random seed that happens to not throw errors. 14 Despite having a random seed the test run changes with every new builtin function introduced in `sage/functions` because the global function list changes. That's why the test was marked random. The problem however is that the test can even raise an error, as the docs state above. The"`random`" keyword does not catch this, and it would make the test useless anyway. 47 15 48 A fix that filters out zero divisions and intended exceptions thrown from code in `functions` before returning the expression would be the best solution (but it doesn't handle runtime errors from Pynac).16 Tests are meant to test the functionality of the associated code so the test and perhaps `random_expr` should be rewritten such that it allows a test that does not change with a changed global function list.