Serious bug in integral using Maxima?

20 views
Skip to first unread message

Golam Mortuza Hossain

unread,
Aug 18, 2009, 6:51:52 AM8/18/09
to sage-...@googlegroups.com
Hi,

While testing new integral SFunction class for Sage,
I encountered this weird bug.

----------
sage: f(x) = function('f',x)

sage: f(x).integral(x)
integrate(f(x), x)

sage: f(x).integral(x^2)
x^2*f(x)
-----------

It appears to be a Maxima bug
----------
(%i10) integrate(f(x), x^2);
2
(%o10) x f(x)
----------

However, even "integral" of calculus.py seems to do crazy thing as well.
In line 566, it does

elif not is_SymbolicVariable(v):
v = var(repr(v))

Above imply

--------
sage: v = var(repr(x^2))
sage: v
x^2
sage: v.diff(x)
0
-------

The next commented out line in calculus.py seems to be the right
thing do here!!

Cheers,
Golam

rjf

unread,
Aug 18, 2009, 7:42:21 PM8/18/09
to sage-devel
did you mean to integrate with respect to "x^2" ?

Well, x^2 doesn't occur in f(x). So let's rename x^2 as y.
What is the integral of f(x) with respect to y?

It is y*f(x).

substituting back x^2 for y, you get x^2*f(x).

Or did you mean something else? Certainly Maxima expects "the variable
of integration" as
the second argument. Anything else is asking for trouble.

William Stein

unread,
Aug 18, 2009, 8:27:32 PM8/18/09
to sage-...@googlegroups.com
On Tue, Aug 18, 2009 at 3:51 AM, Golam Mortuza
Hossain<gmho...@gmail.com> wrote:
>
> Hi,
>
> While testing new integral SFunction class for Sage,
> I encountered this weird bug.
>
> ----------
> sage: f(x) = function('f',x)
>
> sage: f(x).integral(x)
> integrate(f(x), x)
>
> sage: f(x).integral(x^2)
> x^2*f(x)
> -----------

Indeed, what does that mean? If forced to, I would interpret this as

int f(x) d(x^2) = int f(x) 2 x dx
= 2x integrate(f(x),x)

So I think the Sage/Maxima answer of x^2*f(x) is bizarre.

Matheamatica just considers this input to be invalid:

sage: mathematica.eval('Integrate[f[x],x^2]')

2
Integrate::ilim: Invalid integration variable or limit(s) in x .

2
Integrate[f[x], x ]

Unless you can give a explanation of what you want integrating wrt x^2
to mean, I think we should also raise an error in Sage.


>
> It appears to be a Maxima bug
> ----------
> (%i10) integrate(f(x), x^2);
>                                     2
> (%o10)                              x  f(x)
> ----------
>
> However, even "integral" of calculus.py seems to do crazy thing as well.
> In line 566, it does
>
> elif not is_SymbolicVariable(v):
>        v = var(repr(v))
>
> Above imply
>
> --------
> sage: v = var(repr(x^2))
> sage: v
> x^2
> sage: v.diff(x)
> 0
> -------
>
> The next commented out line in calculus.py seems to be the right
> thing do here!!
>
> Cheers,
> Golam
>
> >
>



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

Golam Mortuza Hossain

unread,
Aug 18, 2009, 9:48:19 PM8/18/09
to sage-...@googlegroups.com
Hi,

On Tue, Aug 18, 2009 at 8:42 PM, rjf<fat...@gmail.com> wrote:
>
> did you mean to integrate with respect to "x^2" ?

Yes.

> Well, x^2 doesn't occur in   f(x).  So let's rename x^2 as y.
> What is the integral of f(x) with respect to y?
>
> It is y*f(x).
>
> substituting back x^2 for y,  you get x^2*f(x).

Are you saying during integration maxima is not aware
that "x^2" is the square of "x"?

Take following examples:
----------
(%i4) integrate(log(x), x);
(%o4) x log(x) - x

(%i5) integrate(log(x^2), x^2);
2
(%o5) 2 x log(x)
----------

"log" is certainly aware of it.

> Certainly Maxima expects "the variable of integration" as
> the second argument.  Anything else is asking for trouble.

Then maxima should throw error instead of giving a wrong
answer silently. This is rather bizarre given maxima is known
to be over-cautious during integration and sometime
it asks too many questions, even something like
--------
(%i6) integrate(1/x, x, 0, 1);
Is x + 1 positive, negative, or zero?
--------

Cheers,
Golam

Golam Mortuza Hossain

unread,
Aug 18, 2009, 9:55:29 PM8/18/09
to sage-...@googlegroups.com
Hi,

On Tue, Aug 18, 2009 at 9:27 PM, William Stein<wst...@gmail.com> wrote:
>
>> ----------
>> sage: f(x) = function('f',x)
>>
>> sage: f(x).integral(x)
>> integrate(f(x), x)
>>
>> sage: f(x).integral(x^2)
>> x^2*f(x)
>> -----------
>
> Indeed, what does that mean?  If forced to, I would interpret this as
>
>   int f(x) d(x^2)  = int f(x) 2 x dx
>                         = 2x integrate(f(x),x)
>
> So I think the Sage/Maxima answer of x^2*f(x) is bizarre.
>
> Matheamatica just considers this input to be invalid:
>
> sage: mathematica.eval('Integrate[f[x],x^2]')
>
>                                                              2
> Integrate::ilim: Invalid integration variable or limit(s) in x .
>
>                         2
>        Integrate[f[x], x ]
>
> Unless you can give a explanation of what you want integrating wrt x^2
> to mean, I think we should also raise an error in Sage.


I tried that input out of curiosity during testing. I was expecting
a TypeError but instead I got an answer !!

I agree, we should raise an error. Ironically, in "calculus.py" the raise
error line (556) has been commented out for some reason.
-----------


elif not is_SymbolicVariable(v):
v = var(repr(v))

#raise TypeError, 'must integrate with respect to a variable'
---------


Cheers,
Golam

Robert Bradshaw

unread,
Aug 18, 2009, 11:37:59 PM8/18/09
to sage-...@googlegroups.com

Probably var(repr(var)) is the wrong thing to do here... I have a
feeling I've seen this code before.

- Robert


rjf

unread,
Aug 19, 2009, 1:40:33 AM8/19/09
to sage-devel
Can you anticipate all the possible ways of misusing a command, and
catch them all
with a suitable error message? I think that's pretty hard with a
system that has essentially
free-form input of any length, attached to fairly vague semantics
(like --- let's see what
Maxima does with this ---).

RJF



On Aug 18, 8:37 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:

rjf

unread,
Aug 19, 2009, 1:57:58 AM8/19/09
to sage-devel


On Aug 18, 6:48 pm, Golam Mortuza Hossain <gmhoss...@gmail.com> wrote:
> Hi,
>
> On Tue, Aug 18, 2009 at 8:42 PM, rjf<fate...@gmail.com> wrote:
>
> > did you mean to integrate with respect to "x^2" ?
>
> Yes.

Well then, since you meant to do that, what response would you
consider correct?
An error message, or William's interpretation of d(x^2) as 2xdx?
or something else?

>
> > Well, x^2 doesn't occur in   f(x).  So let's rename x^2 as y.
> > What is the integral of f(x) with respect to y?
>
> > It is y*f(x).
>
> > substituting back x^2 for y,  you get x^2*f(x).
>
> Are you saying during integration maxima is not aware
> that "x^2" is the square of "x"?

Right, because your "variable of integration" is not in the integrand.
If I search for x^2 in f(x), I get no matches.
>
> Take following examples:
> ----------
> (%i4) integrate(log(x), x);
> (%o4)                            x log(x) - x

This is a correct usage, and the answer is correct.

>
> (%i5) integrate(log(x^2), x^2);
>                                      2
> (%o5)                             2 x  log(x)
> ----------
>
> "log" is certainly aware of it.

This is an incorrect usage. Following the recipe I gave above, I
think that
Maxima first of all simplified log(x^2) to 2*log(x). The integrand
does not
contain any x^2. y. Then the
problem is integrate(2*log(x),y). Which is 2*y*log(x). Now
substitute x^2 for y.
You get the answer above.

>
> > Certainly Maxima expects "the variable of integration" as
> > the second argument.  Anything else is asking for trouble.
>
> Then maxima should throw error instead of giving a wrong
> answer silently.

Maybe. or maybe it should return the integral unevaluated. or maybe
it
should try to compute the differential.


> This is rather bizarre given maxima is known
> to be over-cautious during integration and sometime
> it asks too many questions, even something like
> --------
> (%i6) integrate(1/x, x, 0, 1);
> Is  x + 1  positive, negative, or zero?

When Maxima asks questions, it is not being cautious..
The program asks questions so it can provide a correct answer.
As it happens, it sometimes asks questions when it could deduce the
answer.
This is a deficiency in the deduction system, not integration.
You are probably using an outdated Maxima, since the current one
does not ask questions, but just says the integral is divergent.

You could try integrate(1/x,x,a,b) and then it asks questions to try
to
determine if a<=0<=b.


Ondrej Certik

unread,
Aug 19, 2009, 2:00:20 AM8/19/09
to sage-...@googlegroups.com
On Tue, Aug 18, 2009 at 5:27 PM, William Stein<wst...@gmail.com> wrote:
>
> On Tue, Aug 18, 2009 at 3:51 AM, Golam Mortuza
> Hossain<gmho...@gmail.com> wrote:
>>
>> Hi,
>>
>> While testing new integral SFunction class for Sage,
>> I encountered this weird bug.
>>
>> ----------
>> sage: f(x) = function('f',x)
>>
>> sage: f(x).integral(x)
>> integrate(f(x), x)
>>
>> sage: f(x).integral(x^2)
>> x^2*f(x)
>> -----------
>
> Indeed, what does that mean?  If forced to, I would interpret this as
>
> int f(x) d(x^2) = int f(x) 2 x dx
> = 2x integrate(f(x),x)
>
> So I think the Sage/Maxima answer of x^2*f(x) is bizarre.

well, I would interpret it differently:

int f(x) d(x^2) = int f(x) 2 x dx

= 2 integrate(x*f(x),x)


in sympy we just raise an exception:

In [1]: integrate(sin(x), x)
Out[1]: -cos(x)

In [2]: integrate(sin(x), x**2)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/home/ondrej/repos/sympy/<ipython console> in <module>()

/home/ondrej/repos/sympy/sympy/utilities/decorator.pyc in
threaded_decorator(expr, *args, **kwargs)
54 return Add(*[ func(f, *args, **kwargs) for
f in expr.args ])
55 else:
---> 56 return func(expr, *args, **kwargs)
57
58 threaded_decorator.__doc__ = func.__doc__

/home/ondrej/repos/sympy/sympy/integrals/integrals.pyc in
integrate(*args, **kwargs)
480 """
481 new_args = [sympify(arg) for arg in args]
--> 482 integral = Integral(*new_args, **kwargs)
483
484 if isinstance(integral, Integral):

/home/ondrej/repos/sympy/sympy/integrals/integrals.pyc in __new__(cls,
function, *symbols, **assumptions)
46 continue
47
---> 48 raise ValueError("Invalid integration variable
or limits: %s" % str(symbols))
49 else:
50 # no symbols provided -- let's compute full antiderivative


ValueError: Invalid integration variable or limits: (x**2,)

Ondrej

Robert Dodier

unread,
Aug 19, 2009, 2:17:47 AM8/19/09
to sage-devel
William Stein wrote:

> Unless you can give a explanation of what you want integrating wrt x^2
> to mean, I think we should also raise an error in Sage.

That would be unfortunate. Faced with some unrecognized construct,
the mathematical thing to do is to just leave it be. Whether it's
meaningful is for the user to decide. You don't know what
integrate(f(x), g(x)) means. Why not let someone else come
up with an interpretation? Why must you close that door?

Incidentally, integrate(f(x), g(x)) = integrate(f(g^(-1)(y)), y), when
g^(-1) is well defined, seems plausible. I'm not saying Sage should
apply such an identity, only that Sage should not prevent the user
from applying it.

I agree that Maxima's result for integrate(f(x), x^2) is spurious.

FWIW

Robert Dodier

Ondrej Certik

unread,
Aug 19, 2009, 2:34:17 AM8/19/09
to sage-...@googlegroups.com
On Tue, Aug 18, 2009 at 11:17 PM, Robert Dodier<robert...@gmail.com> wrote:
>
> William Stein wrote:
>
>> Unless you can give a explanation of what you want integrating wrt x^2
>> to mean, I think we should also raise an error in Sage.
>
> That would be unfortunate. Faced with some unrecognized construct,
> the mathematical thing to do is to just leave it be. Whether it's
> meaningful is for the user to decide. You don't know what
> integrate(f(x), g(x)) means. Why not let someone else come
> up with an interpretation? Why must you close that door?

Is there any other interpretation besides:

int f(x) d g(x) = int f(x) * dg/dx dx

? Example used in physics:

int P(cos(x)) sin(x) dx = int P(cos(x)) dcos(x)

where P is a Legendre polynomial.

Ondrej

Simon King

unread,
Aug 19, 2009, 4:38:45 AM8/19/09
to sage-devel


On 19 Aug., 08:34, Ondrej Certik <ond...@certik.cz> wrote:
...
> Is there any other interpretation besides:
>
> int f(x)  d g(x)  = int f(x) * dg/dx    dx
>
> ? Example used in physics:
>
> int P(cos(x)) sin(x) dx = int P(cos(x)) dcos(x)
>
> where P is a Legendre polynomial.

Indeed this seems like a natural interpretation, and it would be
convenient (at least for physicists, perhaps also for engineers) if f
(x).integral(g(x)) would return "int f(x) d g(x)", rather than to try
and match g(x) as a sub-expression of f(x).

Of course, said physicist/engineer could compute d g(x) and change the
integrand accordingly, so that s/he is left with just the integration
variable x. But this is one additional step of work that would better
be automated.

On the other hand, how should Maxima know that in the end, we want the
integration variable x? For example, if g depends on two parameters
x,y, but y is supposed to be a constant parameter rather than a
variable?

What do you think about the following syntax:
f.integral(x,g) - f a function, x a variable, g (optional, default g
(x)=x) is a function
Output:
int f(x) dg(x) = int f(x) dg(x)/dx dx

If I am not mistaken, that would also be meaningful if f or g depended
on further variables.

Advantage of that syntax: g is optional, hence, existing code wouldn't
break. And the corresponding changes could be done in the Sage library
("integral") without to touch Maxima.

Cheers,
Simon

Golam Mortuza Hossain

unread,
Aug 19, 2009, 6:15:48 AM8/19/09
to sage-...@googlegroups.com
On Wed, Aug 19, 2009 at 3:17 AM, Robert Dodier<robert...@gmail.com> wrote:
>
> William Stein wrote:
>
>> Unless you can give a explanation of what you want integrating wrt x^2
>> to mean, I think we should also raise an error in Sage.
>
> That would be unfortunate. Faced with some unrecognized construct,
> the mathematical thing to do is to just leave it be. Whether it's
> meaningful is for the user to decide. You don't know what
> integrate(f(x), g(x)) means. Why not let someone else come
> up with an interpretation? Why must you close that door?
>
> Incidentally, integrate(f(x), g(x)) = integrate(f(g^(-1)(y)), y), when
> g^(-1) is well defined, seems plausible. I'm not saying Sage should
> apply such an identity, only that Sage should not prevent the user
> from applying it.

+1

Yes, I think now I agree with you. It is better to return it un-evaluated
than raising an error. As you, Ondrej and Simon pointed out that for some
g, it has un-ambiguous interpretation and may be Sage should apply
the identity where possible (definitely for expression with single variable)
before passing it to maxima.

Cheers,
Golam

rjf

unread,
Aug 19, 2009, 10:53:16 AM8/19/09
to sage-devel

Consider
integrate(f(x,y),x*y).

do you compute d(x*y) as x*dy+y*dx and compute integrate(f(x)
*x,y) + integrate(f(x)*y,x)?


Here's another interpretation of variable = x^2...

integrate(f(x),x^2) = integrate(integrate(f(x),x),x).
that is, an iterated integral. This is like

(d ^2 f(x))/dx^2 notation for derivatives, where the derivative is
definitely NOT with respect to x^2.

Blame Newton, I think, or maybe Leibniz?

RJF


Simon King

unread,
Aug 19, 2009, 11:26:45 AM8/19/09
to sage-devel
Hi!

On Aug 19, 3:53 pm, rjf <fate...@gmail.com> wrote:
> Consider
> integrate(f(x,y),x*y).
>
> do you compute   d(x*y)  as x*dy+y*dx and compute    integrate(f(x)
> *x,y) + integrate(f(x)*y,x)?
>
> Here's another interpretation of variable = x^2...
>
> integrate(f(x),x^2)  =  integrate(integrate(f(x),x),x).
> that is, an iterated integral.  This is like
>
> (d ^2 f(x))/dx^2   notation for derivatives, where the derivative is
> definitely NOT with respect to x^2.

But would you agree that my suggestion
f.integral(x[,g]) --- f function, x variable, g function (optional,
default g(x)=x)
makes sense, which should return the integral (f * dg/dx) dx ?

I think that has no ambiguity in it (even if f or g depends on further
variables), and it would just mean to extend Sage's .integral() method
so that Maxima is used in a more clever way.

Cheers,
Simon

William Stein

unread,
Aug 19, 2009, 1:30:53 PM8/19/09
to sage-...@googlegroups.com
On Tue, Aug 18, 2009 at 11:00 PM, Ondrej Certik<ond...@certik.cz> wrote:
>
> On Tue, Aug 18, 2009 at 5:27 PM, William Stein<wst...@gmail.com> wrote:
>>
>> On Tue, Aug 18, 2009 at 3:51 AM, Golam Mortuza
>> Hossain<gmho...@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> While testing new integral SFunction class for Sage,
>>> I encountered this weird bug.
>>>
>>> ----------
>>> sage: f(x) = function('f',x)
>>>
>>> sage: f(x).integral(x)
>>> integrate(f(x), x)
>>>
>>> sage: f(x).integral(x^2)
>>> x^2*f(x)
>>> -----------
>>
>> Indeed, what does that mean?  If forced to, I would interpret this as
>>
>>   int f(x) d(x^2)  = int f(x) 2 x dx
>>                         = 2x integrate(f(x),x)
>>
>> So I think the Sage/Maxima answer of x^2*f(x) is bizarre.
>
> well, I would interpret it differently:
>
> int f(x) d(x^2)  = int f(x) 2 x dx
>                     = 2 integrate(x*f(x),x)

That's exactly what I meant. I was just being very sloppy because I
was in a hurry. The point is that "int f(x) d(x^2) = int f(x) 2 x
dx" seems very reasonable. We could easily make Sage use this
interpretation even though Maxima doesn't. It woud be an additional
2-3 lines of code in calculus.py.

I am equally for either:

(1) raising an error like Mathematica does
and
(2) Use the interpretation that Ondrej and I agree upon above.

I favor (1) a little bit more than (2), because it's clear that there
is some confusion over this issue, and (1) will definitively reduce
confusion, at the expense of making some user code slightly longer
(but probably easier to understand!).

William

Tim Lahey

unread,
Aug 19, 2009, 1:40:26 PM8/19/09
to sage-...@googlegroups.com

On Aug 19, 2009, at 1:30 PM, William Stein wrote:

>
> That's exactly what I meant. I was just being very sloppy because I
> was in a hurry. The point is that "int f(x) d(x^2) = int f(x) 2 x
> dx" seems very reasonable. We could easily make Sage use this
> interpretation even though Maxima doesn't. It woud be an additional
> 2-3 lines of code in calculus.py.
>
> I am equally for either:
>
> (1) raising an error like Mathematica does
> and
> (2) Use the interpretation that Ondrej and I agree upon above.
>
> I favor (1) a little bit more than (2), because it's clear that there
> is some confusion over this issue, and (1) will definitively reduce
> confusion, at the expense of making some user code slightly longer
> (but probably easier to understand!).
>
> William
>


I'm in favour of (1). If it isn't possible to integrate with respect to
that variable, it should raise an error, not try to interpret what the
user means.

---
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://www.linkedin.com/in/timlahey

Golam Mortuza Hossain

unread,
Aug 19, 2009, 2:27:31 PM8/19/09
to sage-...@googlegroups.com
Hi,


On Wed, Aug 19, 2009 at 2:30 PM, William Stein<wst...@gmail.com> wrote:
>
> On Tue, Aug 18, 2009 at 11:00 PM, Ondrej Certik<ond...@certik.cz> wrote:

>>
>> well, I would interpret it differently:
>>
>> int f(x) d(x^2)  = int f(x) 2 x dx
>>                     = 2 integrate(x*f(x),x)
>
> That's exactly what I meant.  I was just being very sloppy because I
> was in a hurry.  The point is that "int f(x) d(x^2)  = int f(x) 2 x
> dx" seems very reasonable.    We could easily make Sage use this
> interpretation even though Maxima doesn't.  It woud be an additional
> 2-3 lines of code in calculus.py.
>
> I am equally for either:
>
> (1) raising an error like Mathematica does
> and
> (2) Use the interpretation that Ondrej and I agree upon above.
>
> I favor (1) a little bit more than (2), because it's clear that there
> is some confusion over this issue


Hmm, could you please clarify a bit? When you say "raise an error"
do you mean

(A) (damn the user!!)
-------
sage: integrate( sin(x), x^2)
....
TypeError: blah.. blah...
-------

or (B) (leave it symbolic)
-------
sage: integrate(f(x), x^2)
integrate( sin(x), x^2)
-------


(A) will have some bad consequences like

-----
sage: h = sin(x) + integrate( sin(x), x)
sage: h.subs(x==2*x)
sin(2*x) - cos(2*x)
-----
will work but
-----
sage: h = sin(x) + integrate( sin(x)/x, x)
sage: h.subs(x==2*x)
...
TypeError: .....
-----

It would be bad if we don't allow even constant scaling
of variable in the integration.

I guess, there are no ambiguity when it is an expression
containing single variable and we should process it as you
and Ondrej suggested. For other cases, we leave
it symbolic as Robert (Dodier) suggested.


Cheers,
Golam

Gonzalo Tornaria

unread,
Aug 19, 2009, 7:19:05 PM8/19/09
to sage-...@googlegroups.com
On Wed, Aug 19, 2009 at 3:17 AM, Robert Dodier<robert...@gmail.com> wrote:
>
> William Stein wrote:
>
>> Unless you can give a explanation of what you want integrating wrt x^2
>> to mean, I think we should also raise an error in Sage.
>
> That would be unfortunate. Faced with some unrecognized construct,
> the mathematical thing to do is to just leave it be. Whether it's
> meaningful is for the user to decide. You don't know what
> integrate(f(x), g(x)) means. Why not let someone else come
> up with an interpretation? Why must you close that door?

http://en.wikipedia.org/wiki/Riemann%E2%80%93Stieltjes_integral

Gonzalo

William Stein

unread,
Aug 19, 2009, 8:16:57 PM8/19/09
to sage-devel
2009/8/19 Golam Mortuza Hossain <gmho...@gmail.com>:

I am personally fine with Robert Dodier's suggestion, i.e., A above
and don't "damn the user".

William

Reply all
Reply to author
Forward
0 new messages