# Change of variable in an integration

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

Change of variable in an integration

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

add a comment

3

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

http://ask.sagemath.org/question/1072...

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

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 ?

0

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?

Asked: **
2012-01-28 11:59:27 -0500
**

Seen: **742 times**

Last updated: **Feb 13 '12**

Substitute variable in expression

Declare variable as function output [closed]

How to substitute a function within derivatives?

automatic substitution within functions?

Simplification of expression with exponentials.

Substitution of several variables

How to dynamically substitute a variable in a callable function?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.