Ask Your Question

Two ways of integrating x↦xⁿsin(x) give contradictory results. Bug?

asked 2017-01-04 21:44:16 +0200

anonymous user


updated 2017-01-05 03:03:50 +0200

tmonteil gravatar image

First way:


gives just

integrate(x^n*sin(x), x)

not very informative, let us try to add an assumption to get nicer results.

Second way:



1/4*(((-1)^n - 1)*gamma(n + 1, I*x) - ((-1)^n - 1)*gamma(n + 1, -I*x))*(-1)^(-1/2*n)

Uhm, looks better, but... wait, isn't (-1)^n-1 equal to 0 for even values of n ? That would make the whole thing equal to 0 for even n.

I = integral(x^n*sin(x),x)
for k in range(10):
    print I.subs(n==2*k)

prints only 0s. Weird, non-zero functions should not have zero integrals.

Third way :

Let us try to do the integration with particular values of n.

for n in range(5):
    print integral(x^n*sin(x),x)


-x*cos(x) + sin(x)
-(x^2 - 2)*cos(x) + 2*x*sin(x)
-(x^3 - 6*x)*cos(x) + 3*(x^2 - 2)*sin(x)

Looks better, but is clearly different from the previous answer.


I am working on the cloud, with SageMath 7.4 kernel. Is this a bug or did I misunderstood the meaning of the 'integer'assumption ?

If this is a bug, how should I report it, is posting this question here enough ?

P.S. I did read the wiki page about reporting bugs, but, gosh, is it really necessary to have a google account in order to report a bug ? Both sage-devel and sage-support are on Google Groups.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2017-01-04 23:18:02 +0200

mforets gravatar image

updated 2017-01-05 00:41:53 +0200

You can get an expression for the indefinite integral in terms of hypergeometric functions with the additional argument algorithm='sympy'. It seems to me, as you point out, that algorithm='maxima' (default, I guess) gives a wrong answer for even $n$.

edit flag offensive delete link more

answered 2017-01-04 23:56:40 +0200

The default algorithm for integration is Maxima, and it is known to have issues with the branches of exponential integrals: here's one Trac ticket with a related integration error. I've opened a new ticket for this error.

As pointed out by @mforets, you can use Sympy to get an answer valid for any n without the assumption:


Here's a live example of the resulting hypergeometric function.

And in answer to your final question, reporting the bug here is sufficient. Thanks!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-01-04 21:44:16 +0200

Seen: 189 times

Last updated: Jan 04 '17