Ask Your Question

Change of variable in an integration

asked 2012-01-28 18:59:27 +0200

Green diod gravatar image

updated 2015-01-13 18:18:12 +0200

FrédéricC gravatar image

How to indicate a change of variable to Sage in an integration when Sage seems clueless?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2012-01-28 19:48:46 +0200

Shashank gravatar image

updated 2012-01-30 20:47:05 +0200

Here is an example you can look at.

Edit: If you need to preload the function use the following link


Sorry for the delay. Here is the full solution to your question

def changevar(f, eqn, newvar):
    dx = diff(eqn.rhs(),newvar)
    return f.substitute(eqn)*dx 
ans=changevar(x*cos(x^2+1), x==eqn, u)
edit flag offensive delete link more


That's nice, thanks. But two more questions about that: 1. How to store and load such python functions for further use in other sessions? 2. If we compute a definite integral, how to add the bounds ?

Green diod gravatar imageGreen diod ( 2012-01-29 06:10:57 +0200 )edit

I have updated the answer about the first point. I will get back regarding the second one.

Shashank gravatar imageShashank ( 2012-01-29 18:25:41 +0200 )edit

I have included how to implement change of variables with limits.

Shashank gravatar imageShashank ( 2012-01-30 20:47:36 +0200 )edit

Thank you so much!!

Green diod gravatar imageGreen diod ( 2012-01-31 11:56:36 +0200 )edit

@Shashank: is this function something you think would be worth adding in some form to the Sage library?

kcrisman gravatar imagekcrisman ( 2012-02-11 21:31:06 +0200 )edit

answered 2012-02-12 11:28:20 +0200

Green diod gravatar image

updated 2012-02-13 13:58:03 +0200

kcrisman gravatar image

Based on Shashank comment, but see extra questions below:

def changevar_definite(f, eqn, newvar, oldvar, a, b):
   old_assumptions= assumptions() # Keep current assumptions for later restore
   assume(oldvar > a, oldvar < b) # This is bad (Maxima pb), see below
   h(oldvar)= solve(eqn, newvar)[0].rhs()
   g(newvar)= solve(eqn, oldvar)[0].rhs()
   dx = diff(g, newvar)
   assume(old_assumptions) # Unfortunately, extra assumptions don't stay local
   return f.substitute(oldvar==g(newvar))*dx, h(a), h(b)

Okay, let's try it

integrand=changevar_definite(x*cos(x^2+1), u==x^2+1, u, x, 0, 2)
integrand # Output: (u |--> 1/2*cos(u), 1, 5)


integral(integrand[0](u), u, integrand[1], integrand[2])# -1/2*sin(1) + 1/2*sin(5)

But the following also works:

changevar_definite(x*cos(x^2+1), x==sqrt(u-1), u, x, 0, 2)

Now, the latter call breaks if I have assume(oldvar >= a, oldvar <= b) instead as I had earlier, because Maxima asks about x being positive or zero. In fact, the assumptions mechanism is too sticky, what I change locally has global side effect. A worse problem is I can't force a more stringent assumption locally (say going from x >= 0 to x > 0).

1- How to efficiently handle those assumptions?

2- How to pass extra-assumptions as arguments to my changevar_definite function?

edit flag offensive delete link more


Your second question could be handled by allowing some kind of `*args` or `**kwds` argument, which would unpack them. Probably `**kwds`, though I'm not sure how to pass > or < that way. Certainly in some convoluted way.

kcrisman gravatar imagekcrisman ( 2012-02-13 14:01:43 +0200 )edit

The first question is probably not possible per se without doing a significant amount of work on assumptions. Assumptions are global. Sage's assumptions are also fairly weak, just as Maxima's are.

kcrisman gravatar imagekcrisman ( 2012-02-13 14:02:57 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-01-28 18:59:27 +0200

Seen: 1,606 times

Last updated: Feb 13 '12