Opened 6 years ago

Closed 6 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 burcin)

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 6 years ago.
temporary patch
trac_11513-is_trivally_zero.patch (1.6 KB) - added by burcin 6 years ago.
trac_11513-is_trival_zero.patch (1.6 KB) - added by burcin 6 years ago.

Download all attachments as: .zip

Change History (26)

comment:1 Changed 6 years ago by kcrisman

  • Cc kcrisman added

Changed 6 years ago by burcin

temporary patch

comment:2 Changed 6 years ago by burcin

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 6 years ago by kcrisman

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

comment:4 Changed 6 years ago by burcin

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 6 years ago by kcrisman

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 6 years ago by kcrisman

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: Changed 6 years ago by zimmerma

  • 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 6 years ago by burcin

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 6 years ago by zimmerma

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 6 years ago by benjaminfjones

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 6 years ago by burcin

comment:11 follow-up: Changed 6 years ago by burcin

  • Authors set to Burcin Erocal
  • 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 6 years ago by benjaminfjones

  • 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 6 years ago by benjaminfjones

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 6 years ago by benjaminfjones

  • Status changed from positive_review to needs_work

comment:15 Changed 6 years ago by benjaminfjones

  • Status changed from needs_work to needs_review

comment:16 follow-up: Changed 6 years ago by kcrisman

  • Description modified (diff)

Should this be an underscore method like the previous one?

comment:17 in reply to: ↑ 11 Changed 6 years ago by kcrisman

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 6 years ago by burcin

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: Changed 6 years ago by zimmerma

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 6 years ago by zimmerma

  • 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 6 years ago by burcin

comment:21 in reply to: ↑ 19 Changed 6 years ago by burcin

  • 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 6 years ago by zimmerma

  • 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 6 years ago by jdemeyer

  • Merged in set to sage-5.0.beta0
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.