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.Wed, 29 Mar 2017 01:28:56 +0200Integrate piecewise function with change of variablehttps://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/I would like to integrate a piecewise defined function while operating a change of variable. I start by defining the function and another variable involved in the change of variable:
phi(x) = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi(x) = phi.extension(0);
h=pi/n;
h=h.n();
What I would like to do is integrate the function `phi(x/h-1)` between `0` and `pi` so I try it and results in
integral(phi(x/h-1),x,0,pi)
ValueError: substituting the piecewise variable must result in real number
So I then try to use another variable which I try to define to be 'real'
t=var('t')
assume(t,'real');
integral(phi(t/h-1),t,0,pi)
but it results in the same error... Now I try the "lambda" method since it worked when calling the `plot` function with the same change of variable; but fail again
integral(lambda t: phi(t/h-1),t,0,pi)
TypeError: unable to convert <function <lambda> at 0x16d71f140> to a symbolic expression
Now I try to use another integration method with `definite_integral` but get the same errors, only different for the "lambda" method
definite_integral(lambda x: phi(x/h-1),x,0,pi)
TypeError: cannot coerce arguments: no canonical coercion from <type 'function'> to Symbolic Ring
Is there any way around this? I really do not know what else to try...
jrojasquWed, 29 Mar 2017 01:28:56 +0200https://ask.sagemath.org/question/37114/stack overflow in computing entropyhttps://ask.sagemath.org/question/9342/stack-overflow-in-computing-entropy/Hi,
I am still new to sage, I am trying to compute use sage to do some symbolic computation. Here is my test to compute entropy of the Gaussian distribution:
x, sigma, mu,a = var('x','sigma','mu','a')
q = 1/((2*pi)^(1/2)*sigma)*exp(-(x - mu)^2/(2*sigma^2))
assume(sigma>0)
assume(mu>0)
show(q)
import sympy
integral(q,(x,-oo,oo))
integral(-q*log(q),(x,-oo,oo))
First of all, it complains if I don't assume mu is positive. It is right that sigma should be positive but why mu ?! Second, even after assuming mu>0, it produces stack overflow:
WARNING: Output truncated!
full_output.txt
;;;
;;; Binding stack overflow.
;;; Jumping to the outermost toplevel prompt
;;;
;;;
;;; Binding stack overflow.
;;; Jumping to the outermost toplevel prompt
;;;
Does anyone know why? I am sure I am missing something.kayhanSat, 22 Sep 2012 20:42:12 +0200https://ask.sagemath.org/question/9342/Integration yields ImportError: libffi.so.4: No such file or directoryhttps://ask.sagemath.org/question/8837/integration-yields-importerror-libffiso4-no-such-file-or-directory/I can't use the symbolic integration feature of Sage 4.8. I tested some simpler features that work fine, but whenever I try the integrate() function, I get
<blockquote>
<code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_8.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aW50ZWdyYXRlKHgseCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmprPuvfe/___code___.py", line 2, in <module>
exec compile(u'integrate(x,x)
File "", line 1, in <module>
File "/home/jacob/Sage/sage-4.7/local/lib/python2.6/site-packages/sage/misc/functional.py", line 716, in integral
return x.integral(*args, **kwds)
File "expression.pyx", line 8402, in sage.symbolic.expression.Expression.integral (sage/symbolic/expression.cpp:32273)
File "/home/jacob/Sage/sage-4.7/local/lib/python2.6/site-packages/sage/symbolic/integration/integral.py", line 614, in integrate
return indefinite_integral(expression, v)
File "function.pyx", line 418, in sage.symbolic.function.Function.__call__ (sage/symbolic/function.cpp:4623)
File "/home/jacob/Sage/sage-4.7/local/lib/python2.6/site-packages/sage/symbolic/integration/integral.py", line 84, in _eval_
res = integrator(f, x)
File "/home/jacob/Sage/sage-4.7/local/lib/python2.6/site-packages/sage/symbolic/integration/external.py", line 19, in maxima_integrator
result = maxima.sr_integral(expression,v)
File "lazy_import.pyx", line 167, in sage.misc.lazy_import.LazyImport.__getattr__ (sage/misc/lazy_import.c:1395)
File "lazy_import.pyx", line 110, in sage.misc.lazy_import.LazyImport._get_object (sage/misc/lazy_import.c:1047)
File "/home/jacob/Sage/sage-4.7/local/lib/python2.6/site-packages/sage/interfaces/maxima_lib.py", line 72, in <module>
from sage.libs.ecl import *
ImportError: libffi.so.4: cannot open shared object file: No such file or directory</code></blockquote>
I assume the problem occurred because I broke something when I upgraded from 4.7 to 4.8. However, I don't know how to make Sage reinstall its copy of libffi, or make it re-link its libraries, or whatever.
If Sage doesn't have its own libffi and is instead looking for the copy installed on my system, then I know the problem: my system doesn't have "libffi.so.4," it has "libffi.so.5." However, I don't know how to make sage look for libffi.so.5 instead of libffi.so.4, and I don't know what the significance of the number suffix is.
Thank you for your assistance.renevihtFri, 30 Mar 2012 14:08:51 +0200https://ask.sagemath.org/question/8837/problem computing a numeric double integralhttps://ask.sagemath.org/question/8270/problem-computing-a-numeric-double-integral/So, I'd like to compute
$$
\int_{0}^{2\pi} \int_{0}^{2\pi} \left( \frac{1}{\left| \gamma(u) - \gamma(v)\right|^2} - \frac{1}{D(\gamma(u), \gamma(v))^2}\right) \left| \gamma'(u) \right| \left| \gamma'(v)\right| du dv
$$
where $\gamma : [0,2\pi] \to \mathbb{R}^3$ is a curve, and $D(\gamma(u), \gamma(v))$ is the length of the shortest path (on the curve) between $\gamma(u)$ and $\gamma(v)$ (the geodesic arclength).
Here's my code:
def speed(gamma):
"""returns norm{gamma'}, the speed of the curve gamma(t)"""
return gamma.diff(t).norm().function(t)
def arcLength(gamma,a,b):
"""Returns the arc-length of a curve, gamma = f(t), from t=a to t=b """
return abs(numerical_integral(speed(gamma), a, b)[0])
def geodesicArcLength(gamma,a,b):
"""finds the smallest distance between points t=a and t=b on a curve, gamma(t), t \in [0,2*pi]"""
totalLength = arcLength(gamma,0,2*pi)
if a <= b:
lengthOfPath1 = arcLength(gamma,a,b)
else:
lengthOfPath1 = arcLength(gamma,b,a)
if lengthOfPath1 <= (totalLength/2.0):
return lengthOfPath1
else:
return totalLength - lengthOfPath1
def euclideanDistance(gamma,a,b):
"""finds the euclidean distance on gamma(t), a curve, between gamma(a) and gamma(b)"""
return norm(gamma(a) - gamma(b))
def mobiusEnergyIntegrand(gamma,u,v):
return speed(gamma)(u)*speed(gamma)(v)/(euclideanDistance(gamma,u,v)^(-2) - geodesicArcLength(gamma,u,v)^(-2))
def calcMobiusEnergy(gamma):
return numerical_integral( lambda v: numerical_integral( lambda u: mobiusEnergyIntegrand(gamma,u,v) , (u,0,2*pi) )[0] , (v,0,2*pi) )
However, when I try and test this on something simple like
sage: circle(t) = (cos(t), sin(t), 0)
sage: calcMobiusEnergy(circle)
I get
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in calcMobiusEnergy(gamma)
/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/gsl/integration.so in sage.gsl.integration.numerical_integral (sage/gsl/integration.c:2432)()
/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__float__ (sage/symbolic/expression.cpp:5543)()
TypeError: unable to simplify to float approximation
Is my entire strategy just not right? Does anyone have any ideas on how to make this work? I used the response to [this old question](http://ask.sagemath.org/question/68/double-integral) as a model for the function "calcMobiusEnergy".
Thanks,
StevenStevenPollackFri, 12 Aug 2011 21:42:58 +0200https://ask.sagemath.org/question/8270/