numerical_integral(x*y,0,1,[pi]) or numerical_integral(x*y,0,1,y=pi) ?

101 views
Skip to first unread message

Nathann Cohen

unread,
Jan 16, 2015, 12:20:02 AM1/16/15
to Sage devel
Hello everybody,

The function numerical_integral is meant to compute an integral in one
dimension.

It takes as mandatory arguments:
- A function (dependin on any number of variables)
- The bounds of the integration domain

As you can see, the integration variable is not given explicitly. As
the function must be numerical, all other symbolic variables involved
in f must be given a numerical value: this is done with the 'params'
parameter:

numerical_integral(a+b+c+d+e+f+g+h, 0,1,[3,4,5,6,7,8,9])

This will replace the values of variables "b,c,d,e,f,g,h" with the
values [3,4,5,6,7,8,9] before any actual integration, but it never
appears explicitly WHICH of the variables will receive each value.

This 'params' argument is confusing and can lead to mistakes (see
comments from #17507), but how should it be changed ?

1) We remove it, making it mandatory for f to depend on one variable
only (not very cool for newcomers?)
2) We request params to be a dictionary ({b:3,c:9,...}) instead of a list
3) We use **args for that numerical_integral(x*y,0,1,y=pi)

What would be best according to you ? I also ask this question because
I know nothing of how symbolics work in Sage, and I do not know if
similar functions have similar standards.

Nathann

William Stein

unread,
Jan 16, 2015, 12:29:06 AM1/16/15
to sage-devel
We (probably me and *Josh Kantor*? it was a while ago) wrote
numerical_integral in Sage before we had symbolics, and barely touched
it since. I was writing an example for the new SMC button bar to
illustrate numerical_integral yesterday and was struck by how archaic
it felt, in precisely some of the ways you mention above. Even having
it return a list of two numbers, rather than an interval (?) or just a
number and having an option to get the error, feels funny to me. But
of course we wrote numerical_integral before Sage had interval
arithmetic too.

I would recommend looking up what Maple and Mathematica (and Matlab)
do regarding their numerical integration API and summarizing each here
before making any decisions. All their docs are easily available
online, and I tend to thing that doing something consistent with
Mathematica (say), if it is *reasonably* sensible in Sage, is often
the best approach for this sort of thing, because it makes it easier
for people who are used to using Mathematica. I definitely don't
recommend just coming up for some completely new API for numerical
integration without even looking at what Mathematica does, though I
think that's probably what Josh Kantor and I did long ago to Sage...

William


--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Nathann Cohen

unread,
Jan 16, 2015, 1:44:20 AM1/16/15
to Sage devel
Hello,

> I would recommend looking up what Maple and Mathematica (and Matlab)
> do regarding their numerical integration API and summarizing each here
> before making any decisions.

If this is what it takes to fix this code as you think it should, then
I must step back. While reviewing #17507 I wondered about this
problem, but I am scared by the amount of time it would take me to
learn about symbolics and integrations in Sage, and even more to
compare our interface with softwares I never used (and for which I do
not have a licence). Perhaps others, more versed in these domains,
will know how to act.

Nathann

William Stein

unread,
Jan 16, 2015, 1:52:29 AM1/16/15
to sage-devel
Given the enormous amount of energy, focus and talent you have, I'm
surprised that you don't want to look at how other people solve these
API problems, e.g., [1] and [2]. I'm sorry if my suggestion to look
at them discouraged you. It's definitely *only* a suggestion -- you
can certainly ignore it. It's just been my experience that learning
from other resources and constantly fighting "not invented here
syndrome" is generally very beneficial to the Sage project. Anyway,
I'm sure somebody else will pipe up soon.

[1] http://reference.wolfram.com/language/ref/NIntegrate.html
[2] http://www.maplesoft.com/support/help/Maple/view.aspx?path=evalf/Int

Actually, I just looked at [1], and it's almost exactly consistent
with our "integrate" command, so we really should rewrite
numerical_integral to have an API that is as close to [1] as
possible...

William

>
> Nathann
>
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.

William Stein

unread,
Jan 16, 2015, 1:54:28 AM1/16/15
to sage-devel
(Amusingly, I've spent all evening staring at and modifying code you
wrote Nathann, which is saving me a lot of time :-). Thanks!)

kcrisman

unread,
Jan 16, 2015, 10:24:25 AM1/16/15
to sage-...@googlegroups.com


On Friday, January 16, 2015 at 1:52:29 AM UTC-5, William wrote:
On Thu, Jan 15, 2015 at 10:44 PM, Nathann Cohen <nathan...@gmail.com> wrote:
> Hello,
>
>> I would recommend looking up what Maple and Mathematica (and Matlab)
>> do regarding their numerical integration API and summarizing each here
>> before making any decisions.
>
> If this is what it takes to fix this code as you think it should, then
> I must step back. While reviewing #17507 I wondered about this
> problem, but I am scared by the amount of time it would take me to
> learn about symbolics and integrations in Sage, and even more to
> compare our interface with softwares I never used (and for which I do
> not have a licence). Perhaps others, more versed in these domains,
> will know how to act.

Given the enormous amount of energy, focus and talent you have, I'm
surprised that you don't want to look at how other people solve these
API problems, e.g., [1] and [2].  I'm sorry if my suggestion to look
at them discouraged you.  It's definitely *only* a suggestion -- you
can certainly ignore it.    It's just been my experience that learning
from other resources and constantly fighting "not invented here
syndrome" is generally very beneficial to the Sage project.    Anyway,
I'm sure somebody else will pipe up soon.


I should point out two things you will hopefully find helpful.


In particular, http://trac.sagemath.org/ticket/2787 and http://trac.sagemath.org/ticket/7763 are already open for precisely this terrible situation.  No one (including me) has found the time and appropriate way to do this.  Note that we even have two different numerical integration methods - GSL and Maxima - with others no doubt available somewhere in components.  Anyway, I would read those to start off if you really want to do this.

Secondly, I didn't even know about the params, so I guess that would be useful to advertise better once it's fixed!

- kcrisman
Reply all
Reply to author
Forward
0 new messages