Revision history [back]

Based on Shashank comment, but see extra questions:

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 shitty (Maxima pb), see below
h(oldvar)= solve(eqn, newvar).rhs()
g(newvar)= solve(eqn, oldvar).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(u), u, integrand, integrand) # -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 did 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?

Based on Shashank comment, but see extra questions: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 shitty (Maxima pb), see below
h(oldvar)= solve(eqn, newvar).rhs()
g(newvar)= solve(eqn, oldvar).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(u), u, integrand, integrand) # integrand)# -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 did 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?

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 shitty (Maxima pb), see below
h(oldvar)= solve(eqn, newvar).rhs()
g(newvar)= solve(eqn, oldvar).rhs()
dx = diff(g,newvar)
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(u), u, integrand, integrand)# -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 did 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? 4 No.4 Revision

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 shitty bad (Maxima pb), see below
h(oldvar)= solve(eqn, newvar).rhs()
g(newvar)= solve(eqn, oldvar).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(u), u, integrand, integrand)# -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?