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
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
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
Probably var(repr(var)) is the wrong thing to do here... I have a
feeling I've seen this code before.
- Robert
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
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
+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
>
> 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
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
http://en.wikipedia.org/wiki/Riemann%E2%80%93Stieltjes_integral
Gonzalo
I am personally fine with Robert Dodier's suggestion, i.e., A above
and don't "damn the user".
William