Opened 10 years ago
Closed 9 years ago
#11513 closed enhancement (fixed)
add is_trivial_zero() method to symbolic expressions
Reported by: | burcin | Owned by: | burcin |
---|---|---|---|
Priority: | major | Milestone: | sage-5.0 |
Component: | symbolics | Keywords: | sd35.5 |
Cc: | kcrisman | Merged in: | sage-5.0.beta0 |
Authors: | Burcin Erocal | Reviewers: | Benjamin Jones, Paul Zimmermann |
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
A fast way to check if an expression is zero is important in many places, see for example comment:15:ticket:11143. We should put the numerical check mentioned there in a method (_is_numerically_zero()
) of symbolic expressions (sage.symbolic.expression.Expression
).
Attachments (3)
Change History (26)
comment:1 Changed 10 years ago by
- Cc kcrisman added
Changed 10 years ago by
comment:2 Changed 10 years ago by
I attached a patch providing a function which doesn't do much. It should eventually check (reasonably quickly I hope) if expressions like (pi-1)*pi - (pi-2)*pi - pi)
or e^I^pi +1
are zero. This function can be used to provide a template for implementing #11143 while someone tries to improve it.
comment:3 Changed 10 years ago by
I thought this was a computationally unsolvable problem in general? Maybe I'm missing something here.
comment:4 Changed 10 years ago by
That's why the function name says numerically. It might have said trivially as well. This is just supposed to be a quick check. Maybe the examples in comment:2 were too ambitious.
comment:5 Changed 10 years ago by
comment:6 Changed 10 years ago by
Also, what happened to the use of CIF mentioned at #11143? I assume it's commented out because it's slower, or less reliable, or something?
comment:7 follow-up: ↓ 8 Changed 9 years ago by
- Keywords sd35.5 added
I'm puzzled about this ticket. Why doesn't the is_zero
method suffice? As said by Karl-Dieter,
for general expressions the problem is undecidable, thus if you want to check expressions that
*reduce* to zero, the name of the method should reflect the fact that there could be false
negatives.
Paul
comment:8 in reply to: ↑ 7 Changed 9 years ago by
Replying to zimmerma:
I'm puzzled about this ticket. Why doesn't the
is_zero
method suffice? As said by Karl-Dieter, for general expressions the problem is undecidable, thus if you want to check expressions that *reduce* to zero, the name of the method should reflect the fact that there could be false negatives.
Hi Paul, I wanted to ask you about this in Warwick. I got caught up in the linear algebra stuff and this slipped my mind.
is_zero()
usually ends up calling maxima, which is very slow especially in the context of automatic evaluation of symbolic functions.
At the moment, many symbolic functions (see sage/functions/generalized.py
for example) use code similar to the following to test if an argument is zero within a reasonable time: (BTW, this code should not initialize CIF
on every call.)
try: approx_x = ComplexIntervalField()(x) if bool(approx_x.imag() == 0): # x is real if bool(approx_x.real() == 0): # x is zero return None else: return 0 except: # x is symbolic pass
The reason for this ticket was to move this to a separate function to avoid code duplication. If this function can detect pi + (pi - 1)*pi - pi^2 == 0
or (pi - 1)*x - pi*x + x == 0
it would be even better. In this context, false negatives are not a problem. We should just avoid false positives. It's also OK if this test is not purely numeric. Any suggestions for a better name for this function is also welcome of course.
comment:9 Changed 9 years ago by
Dear Burcin,
The reason for this ticket was to move this to a separate function to avoid code duplication.
ok I understand. But I don't like the proposed solution, since:
(1) it might say a number is zero whereas it is not (if the number is tiny, but due to rounding
errors it gets numerically evaluated to zero, because the precision used is fixed)
(2) it might say a zero number is not (again due to rounding errors)
It should be the responsibility of the user only to make such approximations.
What happens if your function _is_numerically_zero()
only checks for *exact* integer or
real or complex zero? Do many doctests fail?
Paul
comment:10 Changed 9 years ago by
The _is_numerically_zero()
has only been used in a few new symbolic functions, we haven't tested replacing the heavily duplicated test code that Burcin is talking about with this new method to see if many doctests fail. My impression is that it is useful to have such a method for automatic simplifications like e.g. erf(0)
should return 0
not erf(0)
, for instance to address #8983.
Changed 9 years ago by
comment:11 follow-up: ↓ 17 Changed 9 years ago by
- Description modified (diff)
- Milestone changed from sage-4.8 to sage-5.0
- Status changed from new to needs_review
After playing around with ComplexIntervalField
, different precisions and symbolic expressions, I finally realized that they cannot perform magic and detect zero. :)
attachment:trac_11513-is_trivally_zero.patch implements a method is_trivially_zero()
instead. This is just a wrapper around pynac's is_zero()
method, which amounts to the exact integer or real/complex zero check Paul suggested.
Can we review this quickly and turn to another ticket that requires magic: #9953?
comment:12 Changed 9 years ago by
- Reviewers set to Benjamin Jones
- Status changed from needs_review to positive_review
I think this is certainly sufficient for our needs with fast evaluation and automatic simplification of symbolic functions.
Positive Review.
The patch looks good, applies cleanly to Sage-4.8.alpha6, and all doctests in the file pass. I'm going to work now on updating various tickets that depended on the name _is_numerically_zero
.
comment:13 Changed 9 years ago by
Sorry, I didn't mean to change the status so quick. I'll defer to others that are interested, but it gets a positive review from me anyhow.
comment:14 Changed 9 years ago by
- Status changed from positive_review to needs_work
comment:15 Changed 9 years ago by
- Status changed from needs_work to needs_review
comment:16 follow-up: ↓ 18 Changed 9 years ago by
- Description modified (diff)
Should this be an underscore method like the previous one?
comment:17 in reply to: ↑ 11 Changed 9 years ago by
comment:18 in reply to: ↑ 16 Changed 9 years ago by
Replying to kcrisman:
Should this be an underscore method like the previous one?
I don't see any reason to hide this from the user. We also removed the leading underscore from is_symbol()
, is_numeric()
, etc. recently.
comment:19 follow-up: ↓ 21 Changed 9 years ago by
the patch looks good to me. Since this a new function, and it is not used anywhere yet, the
doctests should still run, but I will try to be sure. Just a minor point: I'd prefer as name
is_trivial_zero
which is simpler.
Paul
comment:20 Changed 9 years ago by
- Summary changed from add _is_numerically_zero() method to symbolic expressions to add is_trivially_zero() method to symbolic expressions
I change the summary to better reflect the new function name.
Paul
Changed 9 years ago by
comment:21 in reply to: ↑ 19 Changed 9 years ago by
- Description modified (diff)
- Summary changed from add is_trivially_zero() method to symbolic expressions to add is_trivial_zero() method to symbolic expressions
Replying to zimmerma:
Just a minor point: I'd prefer as name
is_trivial_zero
which is simpler.
Done, see attachment:trac_11513-is_trival_zero.patch.
comment:22 Changed 9 years ago by
- Reviewers changed from Benjamin Jones to Benjamin Jones, Paul Zimmermann
- Status changed from needs_review to positive_review
all doctests pass (with the previous patch). However, since the change is trivial, I give a positive review.
Paul
comment:23 Changed 9 years ago by
- Merged in set to sage-5.0.beta0
- Resolution set to fixed
- Status changed from positive_review to closed
temporary patch