Ask Your Question

Change of variable in an integration

asked 2012-01-28 11:59:27 -0600

Green diod gravatar image

updated 2015-01-13 11:18:12 -0600

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 12:48:46 -0600

Shashank gravatar image

updated 2012-01-30 13:47:05 -0600

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-28 23:10:57 -0600 )edit

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

Shashank gravatar imageShashank ( 2012-01-29 11:25:41 -0600 )edit

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

Shashank gravatar imageShashank ( 2012-01-30 13:47:36 -0600 )edit

Thank you so much!!

Green diod gravatar imageGreen diod ( 2012-01-31 04:56:36 -0600 )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 14:31:06 -0600 )edit

answered 2012-02-12 04:28:20 -0600

Green diod gravatar image

updated 2012-02-13 06:58:03 -0600

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 07:01:43 -0600 )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 07:02:57 -0600 )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 11:59:27 -0600

Seen: 541 times

Last updated: Feb 13 '12