# Change of variable in an integration

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

edit retag close merge delete

Sort by » oldest newest most voted

Here is an example you can look at.

http://www.sagenb.org/home/pub/1927/

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

Edit2:

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
var('u')
assume(x>0)
xmin=0
xmax=2
eqn=sqrt(u-1)
limits(x)=solve(eqn==x,u)[0].rhs()
limits(2)
ans=changevar(x*cos(x^2+1), x==eqn, u)
integrate(ans,(u,limits(xmin),limits(xmax))).n()

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 ?

( 2012-01-28 23:10:57 -0500 )edit

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

( 2012-01-29 11:25:41 -0500 )edit

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

( 2012-01-30 13:47:36 -0500 )edit

Thank you so much!!

( 2012-01-31 04:56:36 -0500 )edit

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

( 2012-02-11 14:31:06 -0500 )edit

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)
forget()
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)


Eventually

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?

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.

( 2012-02-13 07:01:43 -0500 )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.

( 2012-02-13 07:02:57 -0500 )edit