Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

erf(0) should return 0 #8983

Closed
sagetrac-rossk mannequin opened this issue May 17, 2010 · 26 comments
Closed

erf(0) should return 0 #8983

sagetrac-rossk mannequin opened this issue May 17, 2010 · 26 comments

Comments

@sagetrac-rossk
Copy link
Mannequin

sagetrac-rossk mannequin commented May 17, 2010

Currently it doesnt...

sage: erf(0)
erf(0)

Apply:

CC: @burcin @kcrisman

Component: symbolics

Keywords: erf

Author: Benjamin Jones

Reviewer: Burcin Erocal, Douglas McNeil

Merged: sage-5.0.beta5

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

@sagetrac-rossk sagetrac-rossk mannequin added this to the sage-5.0 milestone May 17, 2010
@sagetrac-rossk sagetrac-rossk mannequin self-assigned this May 17, 2010
@sagetrac-rossk sagetrac-rossk mannequin added t: enhancement and removed t: bug labels May 17, 2010
@burcin
Copy link

burcin commented May 17, 2010

comment:2

Here is the relevant thread on sage-devel:

http://groups.google.com/group/sage-devel/t/d236e80bf7826bff

The main problem is this:

sage: erf(0)
erf(0)

We should just return 0.

@burcin
Copy link

burcin commented May 17, 2010

Changed author from RossK to none

@burcin burcin changed the title solve(erf(x)==0,x) should return x==0 as a solution erf(0) should return 0 May 17, 2010
@burcin
Copy link

burcin commented Aug 28, 2010

Changed keywords from erf zero to erf

@sagetrac-dsm
Copy link
Mannequin

sagetrac-dsm mannequin commented Feb 20, 2011

comment:5

Beginner reporting for duty! So I have a patch which sets erf(0) to 0 -- or, rather, to parent(x)(0) if x==0, so that it's the "right" zero; I think this is the right idiom -- and adds complex arguments from mpmath.

But I have a few questions that came up along the way:

(1) In 4.6.1, solve(erf(x)==0,x) seems to work for me already, before the patch. Did I get my binaries confused somehow, or did something change elsewhere?

(2) It'd be nice if #8720 (fixing str(CC(0))) were finished, it surprised me when writing tests for the zero type preservation. Maybe I should have a look, I think it's mostly fixing some doctests left.

(3) My patch broke a doctest in functions/special.py, which expected erf(0.5) to be evaluated to 0.520499877813047, but the existing doctests for erf demand that erf(2) = erf(2). I can get that behaviour, namely to evaluate for reals and to hold for integers except for zero, but I don't quite follow it.

(4) Since I'm doing a bit more, should I open an enhancement ticket instead?

@kcrisman
Copy link
Member

comment:6

You should always feel free to add patches - it's MUCH easier to tell what people are talking about, even if the patch is really, really preliminary.

(1) In 4.6.1, solve(erf(x)==0,x) seems to work for me already, before the patch. Did I get my binaries confused somehow, or did something change elsewhere?

This must be from an improvement in Maxima during the several recent upgrades. I've updated the description.

(3) My patch broke a doctest in functions/special.py, which expected erf(0.5) to be evaluated to 0.520499877813047, but the existing doctests for erf demand that erf(2) = erf(2). I can get that behaviour, namely to evaluate for reals and to hold for integers except for zero, but I don't quite follow it.

No, the usual practice is to not evaluate (give symbolic back) for "exact" rings and evaluate (give numeric back) for "inexact" rings. There is some disagreement among developers about exactly what these words mean, but basically erf(x),erf(1/2),erf(2),erf(e) should all return something symbolic and erf(.1),erf(RDF(1)), etc. should return something numeric. I.e. erf2)!=erf(2.).

(4) Since I'm doing a bit more, should I open an enhancement ticket instead?

I believe there already is a ticket for the complex pieces at #1173, similarly at #9044. If you have a good solution to the whole thing, you could do it at one of those and then say this ticket can be closed when they are (if you have also documented that it's fixed.

@kcrisman

This comment has been minimized.

@sagetrac-dsm
Copy link
Mannequin

sagetrac-dsm mannequin commented Feb 24, 2011

comment:7

@kcrisman: okay, understood. Will propose under #1173.

@kcrisman
Copy link
Member

comment:8

If #1173 isn't going to happen anytime soon because of #11513, a very simple patch here would be nice. It would document

sage: solve(erf(x)==0,x)
[x == 0]

as mentioned above already works, and take care of the issue in the summary, of course.

@benjaminfjones
Copy link
Contributor

comment:9

I think the right thing to do here is to depend on #11513. There are several ways one could address the issue in the summary about erf(0) not returning 0, but when a good patch for 11513 appears, the code being touched here would need to be changed again.

@benjaminfjones
Copy link
Contributor

Attachment: trac_8983.patch.gz

make erf(0) return 0

@benjaminfjones
Copy link
Contributor

Author: Benjamin Jones

@benjaminfjones
Copy link
Contributor

comment:10

I uploaded a simple patch now that #11513 is merged.

@sagetrac-dsm
Copy link
Mannequin

sagetrac-dsm mannequin commented Feb 13, 2012

comment:11

Looks good, but I think we should return a Sage zero (or at least match the type):


sage: erf(0)
0
sage: parent(erf(0))
<type 'int'>

@benjaminfjones
Copy link
Contributor

comment:12

That's a good point, this patch addresses the issue by returning the input x instead of 0 when a python 0 would have been returned in the previous patch.

sage: erf(0)
0
sage: type(erf(0))
<type 'sage.rings.integer.Integer'>
sage: parent(erf(0))
Integer Ring

@benjaminfjones
Copy link
Contributor

Attachment: trac_8983_v2.patch.gz

make erf(0) return 0

@benjaminfjones

This comment has been minimized.

@sagetrac-dsm
Copy link
Mannequin

sagetrac-dsm mannequin commented Feb 13, 2012

comment:14

Okay, looks good to me (or at least none of my obvious tricks could break it).

@burcin
Copy link

burcin commented Feb 13, 2012

Reviewer: Burcin Erocal, Douglas McNeil

@burcin
Copy link

burcin commented Feb 13, 2012

comment:15

Thanks for the patch Benjamin. It looks great and gets the job done. However, I'd be much happier if there were a couple more doctests. There are quite a few branches in the new _eval_() function, but the patch adds only one doctest.

One more suggestion: It might be better to leave the last return None statement outside the ifs. IMHO, the following is more compact and readable.

if not isinstance(x, Expression): 
    if is_inexact(x): 
        return self._evalf_(x, parent=parent(x)) 
    elif x == 0: 
        return x 
elif x.is_trivial_zero(): 
        return x 
return None 

@kcrisman
Copy link
Member

comment:16

Replying to @sagetrac-dsm:

Looks good, but I think we should return a Sage zero (or at least match the type):

Is this what you were referring to at #10133 comment:2, dsm?

More generally, does anyone think the present way of ensuring erf(0) returns an Integer (or even Symbolic Expression) is a good one for #10133? Or is it better to address this at the Pynac level?

@benjaminfjones
Copy link
Contributor

comment:17

Replying to @burcin:
That's a good suggestion. I modified the _eval_ method accordingly and added several doctests verifying that all branches are reached. Maybe that's overkill, but it took me a while to find an input that actually reached the x.is_trivial_zero() branch.

@benjaminfjones

This comment has been minimized.

@burcin
Copy link

burcin commented Feb 14, 2012

comment:19

Replying to @benjaminfjones:

Maybe that's overkill, but it took me a while to find an input that actually reached the x.is_trivial_zero() branch.

It's not overkill at all, after adding _a_ doctest to every function, perhaps we will measure the coverage in terms of percentage of lines executed with those tests. Thanks for spending the time and giving this function 100% coverage. :)

BTW, erf(SR(0)) should be a simple way of hitting the is_trivially_zero() branch.

@benjaminfjones
Copy link
Contributor

Attachment: trac_8983_v3.patch.gz

make erf(0) return 0, with added doctests and simplified branching in _eval_

@benjaminfjones
Copy link
Contributor

comment:20

Ok, I simplified that last doctest in _eval_.

@jdemeyer
Copy link

Merged: sage-5.0.beta5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants