sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.44k stars 481 forks source link

Fix inherently failing random_expr doctest #24425

Closed rwst closed 6 years ago

rwst commented 6 years ago

The docs for random_expr read:

  This function will often raise an error because it tries to create
  an erroneous expression (such as a division by zero).

It has the following doctest:

        sage: from sage.symbolic.random_tests import *
        sage: set_random_seed(53)
        sage: random_expr(50, nvars=3, coeff_generator=CDF.random_element) # random
        (v1^(0.97134084277 + 0.195868299334*I)/csc(-pi + v1^2 + v3) + sgn(1/
...

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.

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.

Component: symbolics

Author: Ralf Stephan

Branch: a17755c

Reviewer: Marc Mezzarobba

Issue created by migration from https://trac.sagemath.org/ticket/24425

rwst commented 6 years ago
comment:1

The lazy_import is not even necessary for trigger.

rwst commented 6 years ago

Description changed:

--- 
+++ 
@@ -47,4 +47,4 @@

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.

-Of course the actual bug is that random_expr changes behaviour when unrelated code is changed. +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).

rwst commented 6 years ago

Description changed:

--- 
+++ 
@@ -1,50 +1,18 @@
-Applying the following minimal patch makes a completely unrelated doctest fail:
+The docs for `random_expr` read:

-diff --git a/src/sage/functions/all.py b/src/sage/functions/all.py -index e7f1ea0efa..d26f83f2f9 100644 ---- a/src/sage/functions/all.py -+++ b/src/sage/functions/all.py -@@ -4,7 +4,7 @@ from sage.misc.lazy_import import lazy_import

-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). +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.

rwst commented 6 years ago

Branch: u/rws/fix_inherently_failing_random_expr_doctest

rwst commented 6 years ago

Commit: a17755c

rwst commented 6 years ago

Author: Ralf Stephan

rwst commented 6 years ago

New commits:

a17755c24425: Fix inherently failing random_expr doctest
mezzarobba commented 6 years ago
comment:6

I don't fully understand the context, but the change looks reasonable and really unlikely to break anything—and this ticket has been languishing for months.

mezzarobba commented 6 years ago

Reviewer: Marc Mezzarobba

vbraun commented 6 years ago

Changed branch from u/rws/fix_inherently_failing_random_expr_doctest to a17755c

rwst commented 6 years ago

Changed commit from a17755c to none

rwst commented 6 years ago
comment:8

Thanks. Oddly enough, #24212 depends on this.