Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)[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 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)[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]) # 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 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)[0].rhs()
   g(newvar)= solve(eqn, oldvar)[0].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[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 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?

click to hide/show revision 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)[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?