ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 Feb 2012 09:21:47 +0100Piecewise assumptions (for integration)https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/All right, still with these integration problems, and I don't know all the subtleties of passing extra-arguments to Maxima (ok, I reckon that @kcrisman doesn't stop pointing out Maxima flags now and then when some expert uses them but some list would be very handy).
What I want is to integrate a function with the domain of integration broken into pieces. The problem is that the maxima engine requires different assumptions for each piece but an assumption seems to tie a variable globally.
**Example**: In fact, this example is still related to the [double integral thread over there](http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals). After a little but tedious pen and paper work, I could get rid of the absolute value by breaking the domain of integration into pieces but then I'm stuck again. Independently of my own shortcomings and maybe the hard nature of what I tried to compute, in my sense, the remaining problem causes are mainly twofold, we need to talk to Maxima (pass assumptions) and the assumption mechanisms in Sage are somehow weak.
This is what I tried to get around these shortcomings and to answer the above question:
# This could take extra-arguments for the integral function (algorithm, ...) but I don't know all of them, so let's leave this as is for now.
def integral_assumptions(f, var, lbound, ubound, extra_assumptions):
old_assumptions= assumptions() # Keep current assumptions for later restore
assume(extra_assumptions)
result = integral(f, var, lbound, ubound)
forget()
assume(old_assumptions) # Unfortunately, extra assumptions don't stay local
return result
Different problems arise:
1- If the integral call breaks (and this often occurs), the old_assumptions are not restored .. ok, this one should be easy and dealt with some exception handling but I don't know the Sage coding guideline here.
x,y,u,v,p,k,b=var('x,y,u,v,p,k,b') # It seems enough to just say var('x,y,u,v,p,k,b') but I'm not sure
n(x)=1/sqrt(2*pi) * exp(-1/2*x^2) # I would have loved to be able to get this directly from Maxima but okay, it was just a few keystrokes away.
# I need to split at -sqrt(k-1)*v
alpha_neg(v,k,p)=integral_assumptions((u)^p*(u+sqrt(k-1)*v)^p*n(v)*n(u), u, 0, -sqrt(k-1)*v, [k-1 > 0, v < 0])
integral(alpha_neg(v, k, p), v, -oo, 0) # Error
alpha_neg(v,k,2) # Still an error but just to find the culprit
2- One **big** problem is the way Sage handles assumptions: they are global and (but maybe that *feature* is because of the fact that ...) they can't be made more stringent. Namely `assume(x>=0); assume(x>0)` doesn't yield x>0. It would have also been handy to be able to say forget(x>0) and it would give back x>=0 and have a forget_about(x) function which forgets everything about x ..
To be clear, I'm not against global assumptions but I just want some way to enforce extra assumptions locally.
3- Another bonus feature would have been the possibility of attaching a set of assumptions to an expression/function/whatnot. In fact I started with:
#inner_neg as we're on the part where v<0.
inner_neg(v, k, p, i_lo, i_hi)=integral_assumptions((u)^p*(u+sqrt(k-1)*v)^p*n(v)*n(u), u, i_lo, i_hi, v < 0)
g(v,k)=inner_neg(v, k, 1, 0, oo) # -sqrt(k-1)*v) # Ok with oo
If I instead ask with `-sqrt(k-1)*v`, Maxima again complains about vSun, 12 Feb 2012 19:29:54 +0100https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/Comment by Green diod for <div class="snippet"><p>All right, still with these integration problems, and I don't know all the subtleties of passing extra-arguments to Maxima (ok, I reckon that <a href="/users/41/kcrisman/">@kcrisman</a> doesn't stop pointing out Maxima flags now and then when some expert uses them but some list would be very handy).</p>
<p>What I want is to integrate a function with the domain of integration broken into pieces. The problem is that the maxima engine requires different assumptions for each piece but an assumption seems to tie a variable globally.</p>
<p><strong>Example</strong>: In fact, this example is still related to the <a href="http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals">double integral thread over there</a>. After a little but tedious pen and paper work, I could get rid of the absolute value by breaking the domain of integration into pieces but then I'm stuck again. Independently of my own shortcomings and maybe the hard nature of what I tried to compute, in my sense, the remaining problem causes are mainly twofold, we need to talk to Maxima (pass assumptions) and the assumption mechanisms in Sage are somehow weak.</p>
<p>This is what I tried to get around these shortcomings and to answer the above question:</p>
<pre><code># This could take extra-arguments for the integral function (algorithm, ...) but I don't know all of them, so let's leave this as is for now.
def integral_assumptions(f, var, lbound, ubound, extra_assumptions):
old_assumptions= assumptions() # Keep current assumptions for later restore
assume(extra_assumptions)
result = integral(f, var, lbound, ubound)
forget()
assume(old_assumptions) # Unfortunately, extra assumptions don't stay local
return result
</code></pre>
<p>Different problems arise:</p>
<p>1- If the integral call breaks (and this often occurs), the old_assumptions are not restored .. ok, this one should be easy and dealt with some exception handling but I don't know the Sage coding guideline here.</p>
<pre><code>x,y,u,v,p,k,b=var('x,y,u,v,p,k,b') # It seems enough to just say var('x,y,u,v,p,k,b') but I'm not sure
n(x)=1/sqrt(2*pi) * exp(-1/2*x^2) # I would have loved to be able to get this directly from Maxima but okay, it was just a few keystrokes away.
# I need to split at -sqrt(k-1)*v
alpha_neg(v,k,p)=integral_assumptions((u)^p*(u+sqrt(k-1)*v)^p*n(v)*n(u), u, 0, -sqrt(k-1)*v, [k-1 > 0, v < 0])
integral(alpha_neg(v, k, p), v, -oo, 0) # Error
alpha_neg(v,k,2) # Still an error but just to find the culprit
</code></pre>
<p>2- One <strong>big</strong> problem is the way Sage handles assumptions: they are global and (but maybe that <em>feature</em> is because of the fact that ...) they can't be made more stringent. Namely <code>assume(x>=0); assume(x>0)</code> doesn't yield x>0. It would have also been handy to be able to say forget(x>0) and it would give back x>=0 and have a forget_about(x) function which forgets everything about x .. </p>
<p>To be clear, I'm not against global assumptions but I just ...<span class="expander"> <a>(more)</a></span></p></div>https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?comment=20288#post-id-20288See the updated question. Yeah, it's along the same lines as your example and the problem is that Maxima may need different sets of assumptions for each part of the domain.Mon, 13 Feb 2012 16:51:49 +0100https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?comment=20288#post-id-20288Comment by kcrisman for <div class="snippet"><p>All right, still with these integration problems, and I don't know all the subtleties of passing extra-arguments to Maxima (ok, I reckon that <a href="/users/41/kcrisman/">@kcrisman</a> doesn't stop pointing out Maxima flags now and then when some expert uses them but some list would be very handy).</p>
<p>What I want is to integrate a function with the domain of integration broken into pieces. The problem is that the maxima engine requires different assumptions for each piece but an assumption seems to tie a variable globally.</p>
<p><strong>Example</strong>: In fact, this example is still related to the <a href="http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals">double integral thread over there</a>. After a little but tedious pen and paper work, I could get rid of the absolute value by breaking the domain of integration into pieces but then I'm stuck again. Independently of my own shortcomings and maybe the hard nature of what I tried to compute, in my sense, the remaining problem causes are mainly twofold, we need to talk to Maxima (pass assumptions) and the assumption mechanisms in Sage are somehow weak.</p>
<p>This is what I tried to get around these shortcomings and to answer the above question:</p>
<pre><code># This could take extra-arguments for the integral function (algorithm, ...) but I don't know all of them, so let's leave this as is for now.
def integral_assumptions(f, var, lbound, ubound, extra_assumptions):
old_assumptions= assumptions() # Keep current assumptions for later restore
assume(extra_assumptions)
result = integral(f, var, lbound, ubound)
forget()
assume(old_assumptions) # Unfortunately, extra assumptions don't stay local
return result
</code></pre>
<p>Different problems arise:</p>
<p>1- If the integral call breaks (and this often occurs), the old_assumptions are not restored .. ok, this one should be easy and dealt with some exception handling but I don't know the Sage coding guideline here.</p>
<pre><code>x,y,u,v,p,k,b=var('x,y,u,v,p,k,b') # It seems enough to just say var('x,y,u,v,p,k,b') but I'm not sure
n(x)=1/sqrt(2*pi) * exp(-1/2*x^2) # I would have loved to be able to get this directly from Maxima but okay, it was just a few keystrokes away.
# I need to split at -sqrt(k-1)*v
alpha_neg(v,k,p)=integral_assumptions((u)^p*(u+sqrt(k-1)*v)^p*n(v)*n(u), u, 0, -sqrt(k-1)*v, [k-1 > 0, v < 0])
integral(alpha_neg(v, k, p), v, -oo, 0) # Error
alpha_neg(v,k,2) # Still an error but just to find the culprit
</code></pre>
<p>2- One <strong>big</strong> problem is the way Sage handles assumptions: they are global and (but maybe that <em>feature</em> is because of the fact that ...) they can't be made more stringent. Namely <code>assume(x>=0); assume(x>0)</code> doesn't yield x>0. It would have also been handy to be able to say forget(x>0) and it would give back x>=0 and have a forget_about(x) function which forgets everything about x .. </p>
<p>To be clear, I'm not against global assumptions but I just ...<span class="expander"> <a>(more)</a></span></p></div>https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?comment=20299#post-id-20299Can you give an example of this? I also have a lot of trouble figuring out how to pass the right flags to Maxima. Do you mean something like `\int_{[0,1]\cup [2,3]}f(x)dx`? It's true that the way we do assumptions makes them global-ish.Mon, 13 Feb 2012 13:45:49 +0100https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?comment=20299#post-id-20299Answer by kcrisman for <div class="snippet"><p>All right, still with these integration problems, and I don't know all the subtleties of passing extra-arguments to Maxima (ok, I reckon that <a href="/users/41/kcrisman/">@kcrisman</a> doesn't stop pointing out Maxima flags now and then when some expert uses them but some list would be very handy).</p>
<p>What I want is to integrate a function with the domain of integration broken into pieces. The problem is that the maxima engine requires different assumptions for each piece but an assumption seems to tie a variable globally.</p>
<p><strong>Example</strong>: In fact, this example is still related to the <a href="http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals">double integral thread over there</a>. After a little but tedious pen and paper work, I could get rid of the absolute value by breaking the domain of integration into pieces but then I'm stuck again. Independently of my own shortcomings and maybe the hard nature of what I tried to compute, in my sense, the remaining problem causes are mainly twofold, we need to talk to Maxima (pass assumptions) and the assumption mechanisms in Sage are somehow weak.</p>
<p>This is what I tried to get around these shortcomings and to answer the above question:</p>
<pre><code># This could take extra-arguments for the integral function (algorithm, ...) but I don't know all of them, so let's leave this as is for now.
def integral_assumptions(f, var, lbound, ubound, extra_assumptions):
old_assumptions= assumptions() # Keep current assumptions for later restore
assume(extra_assumptions)
result = integral(f, var, lbound, ubound)
forget()
assume(old_assumptions) # Unfortunately, extra assumptions don't stay local
return result
</code></pre>
<p>Different problems arise:</p>
<p>1- If the integral call breaks (and this often occurs), the old_assumptions are not restored .. ok, this one should be easy and dealt with some exception handling but I don't know the Sage coding guideline here.</p>
<pre><code>x,y,u,v,p,k,b=var('x,y,u,v,p,k,b') # It seems enough to just say var('x,y,u,v,p,k,b') but I'm not sure
n(x)=1/sqrt(2*pi) * exp(-1/2*x^2) # I would have loved to be able to get this directly from Maxima but okay, it was just a few keystrokes away.
# I need to split at -sqrt(k-1)*v
alpha_neg(v,k,p)=integral_assumptions((u)^p*(u+sqrt(k-1)*v)^p*n(v)*n(u), u, 0, -sqrt(k-1)*v, [k-1 > 0, v < 0])
integral(alpha_neg(v, k, p), v, -oo, 0) # Error
alpha_neg(v,k,2) # Still an error but just to find the culprit
</code></pre>
<p>2- One <strong>big</strong> problem is the way Sage handles assumptions: they are global and (but maybe that <em>feature</em> is because of the fact that ...) they can't be made more stringent. Namely <code>assume(x>=0); assume(x>0)</code> doesn't yield x>0. It would have also been handy to be able to say forget(x>0) and it would give back x>=0 and have a forget_about(x) function which forgets everything about x .. </p>
<p>To be clear, I'm not against global assumptions but I just ...<span class="expander"> <a>(more)</a></span></p></div> https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?answer=13273#post-id-13273 1- If the integral call breaks (and this often occurs), the old_assumptions are not restored ..
ok, this one should be easy and dealt with some exception handling but I don't know the Sage
coding guideline here.
Yes, this is because you didn't include a [try/except clause](http://docs.python.org/tutorial/errors.html#handling-exceptions) in your code. Luckily, this is pretty easy to do; you could put it around
assume(extra_assumptions)
result = integral(f, var, lbound, ubound)
forget()
assume(old_assumptions)
and that would work pretty well.
The second issue is also present in Maxima.
(%i1) assume(x>0);
(%o1) [x > 0]
(%i2) assume(x>1);
(%o2) [x > 1]
(%i5) facts();
(%o5) [x > 0, x > 1]
(%i6) is(x>1);
(%o6) true
(%i7) is(x>0);
(%o7) true
> Maxima's deduction mechanism is not very strong; there are many obvious consequences which cannot be determined by is.
As for the rest, such local assumptions are called *contexts*, I believe. Maxima *does* [support this](http://maxima.sourceforge.net/docs/manual/en/maxima_11.html#contexts), but we haven't integrated it in Sage yet. There *is* [a ticket for this](http://trac.sagemath.org/sage_trac/ticket/3277), however, though it has languished.
----------
By the way, if you are almost always using only functionality from Maxima, it wouldn't be the worst idea to see if just using Maxima is better for you, or using the `.maxima_methods()` method. You would get the fine-grained control over all sorts of things that Maxima excels in. Sage is great, but for some people doing group theory, it has more overhead than they need, and they would just use GAP; maybe this is the case for your specific use case.Tue, 14 Feb 2012 09:21:47 +0100https://ask.sagemath.org/question/8710/piecewise-assumptions-for-integration/?answer=13273#post-id-13273