Opened 11 years ago

Closed 11 years ago

#11513 closed enhancement (fixed)

add is_trivial_zero() method to symbolic expressions

Reported by: Burcin Erocal Owned by: Burcin Erocal
Priority: major Milestone: sage-5.0
Component: symbolics Keywords: sd35.5
Cc: Karl-Dieter Crisman Merged in: sage-5.0.beta0
Authors: Burcin Erocal Reviewers: Benjamin Jones, Paul Zimmermann
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by Burcin Erocal)

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

Apply trac_11513-is_trival_zero.patch.

Attachments (3)

trac_11513-is_numerically_zero.patch (1.0 KB) - added by Burcin Erocal 11 years ago.
temporary patch
trac_11513-is_trivally_zero.patch (1.6 KB) - added by Burcin Erocal 11 years ago.
trac_11513-is_trival_zero.patch (1.6 KB) - added by Burcin Erocal 11 years ago.

Download all attachments as: .zip

Change History (26)

comment:1 Changed 11 years ago by Karl-Dieter Crisman

Cc: Karl-Dieter Crisman added

Changed 11 years ago by Burcin Erocal

temporary patch

comment:2 Changed 11 years ago by Burcin Erocal

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 11 years ago by Karl-Dieter Crisman

I thought this was a computationally unsolvable problem in general? Maybe I'm missing something here.

comment:4 Changed 11 years ago by Burcin Erocal

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 11 years ago by Karl-Dieter Crisman

So what does this exactly check? Just if something is a numeric Ginac object that Ginac knows is zero? So we won't get any false positives? That is my main concern, for instance with #1173 or #11143 - we wouldn't want mathematical incorrectness to get a speedup.

comment:6 Changed 11 years ago by Karl-Dieter Crisman

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 Changed 11 years ago by Paul Zimmermann

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 11 years ago by Burcin Erocal

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 11 years ago by Paul Zimmermann

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 11 years ago by Benjamin Jones

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 11 years ago by Burcin Erocal

comment:11 Changed 11 years ago by Burcin Erocal

Authors: Burcin Erocal
Description: modified (diff)
Milestone: sage-4.8sage-5.0
Status: newneeds_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 11 years ago by Benjamin Jones

Reviewers: Benjamin Jones
Status: needs_reviewpositive_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 11 years ago by Benjamin Jones

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 11 years ago by Benjamin Jones

Status: positive_reviewneeds_work

comment:15 Changed 11 years ago by Benjamin Jones

Status: needs_workneeds_review

comment:16 Changed 11 years ago by Karl-Dieter Crisman

Description: modified (diff)

Should this be an underscore method like the previous one?

comment:17 in reply to:  11 Changed 11 years ago by Karl-Dieter Crisman

Can we review this quickly and turn to another ticket that requires magic: #9953?

So is that a dup of #9627 or not? I'm not sure, myself.

comment:18 in reply to:  16 Changed 11 years ago by Burcin Erocal

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 Changed 11 years ago by Paul Zimmermann

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 11 years ago by Paul Zimmermann

Summary: add _is_numerically_zero() method to symbolic expressionsadd is_trivially_zero() method to symbolic expressions

I change the summary to better reflect the new function name.

Paul

Changed 11 years ago by Burcin Erocal

comment:21 in reply to:  19 Changed 11 years ago by Burcin Erocal

Description: modified (diff)
Summary: add is_trivially_zero() method to symbolic expressionsadd 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 11 years ago by Paul Zimmermann

Reviewers: Benjamin JonesBenjamin Jones, Paul Zimmermann
Status: needs_reviewpositive_review

all doctests pass (with the previous patch). However, since the change is trivial, I give a positive review.

Paul

comment:23 Changed 11 years ago by Jeroen Demeyer

Merged in: sage-5.0.beta0
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.