ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 12 Sep 2020 05:50:48 -0500Triple integrals in a specific region of spacehttps://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.
In Mathematica:
reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
![mathematica-polyhedral-region-integrate-polynomial](/upfiles/16000472797455741.png)
Is there a way to easily perform this operation even in Sage?Teo7Sat, 12 Sep 2020 05:50:48 -0500https://ask.sagemath.org/question/53419/Error in integralhttps://ask.sagemath.org/question/53172/error-in-integral/I've got this error on the second integral. I put the code on wxMaxima and returned sucessfull. On wxMaxima, raise a question "z is positive, negative or zero?", also on sage, but on sage I can't answer that. How can I correct this?
```x,y,z = var("x y z")```
```function = x*e^(-y)```
``` integral(function,y,0,ln(x)).integral(x,0,2*z) ```gabrielromao5Tue, 25 Aug 2020 08:50:58 -0500https://ask.sagemath.org/question/53172/Stack overflow during symbolic manipulationshttps://ask.sagemath.org/question/52986/stack-overflow-during-symbolic-manipulations/I'm trying to evaluate this cursed integral (finite for $ 0 < a < 1 $):
$$\int_0^1 \frac{x}{\log{\left(ax+(1-x)^2\right)}}{\rm d}x$$
using Sagemath 9.0 and 9.1:
sage: a = var('a')
sage: assume(a>0)
sage: assume(a<1)
sage: f = integrate(x*log(1/(a*x+(1-x)^2)), x, 0, 1)
leads to
RuntimeError: ECL says: C-STACK overflow at size 1048576. Stack can probably be resized. Proceed with caution.
Sympy gives a result even without the assumptions (which is odd by itself):
sage: f = integrate(x*log(1/(a*x+(1-x)*(1-x))), x, 0, 1, algorithm='sympy')
$$\frac{1}{4} \{\left(a^{2} + \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 2\right)} \log\left(\frac{a^{2} + \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 4}{2 {\left(a - 2\right)}} + 1\right) - \frac{1}{4} {\left(a^{2} + \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 2\right)} \log\left(\frac{a^{2} + \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 4}{2 {\left(a - 2\right)}}\right) + \frac{1}{4} \{\left(a^{2} - \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 2\right)} \log\left(\frac{a^{2} - \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 4}{2 {\left(a - 2\right)}} + 1\right) - \frac{1}{4} {\left(a^{2} - \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 2\right)} \log\left(\frac{a^{2} - \sqrt{{\left(a - 4\right)} a} {\left(a - 2\right)} - 4 a + 4}{2 {\left(a - 2\right)}}\right) - \frac{1}{2} a + \frac{1}{2} \log\left(\frac{1}{a}\right) + \frac{3}{2}$$
But then problems continue:
sage: f.simplify_full()
RuntimeError: ECL says: C-STACK overflow at size 1048576. Stack can probably be resized. Proceed with caution.
But:
sage: f.expand().simplify_full()
$$
-\frac{1}{2} {\left(2 \pi - {\left(\pi - \arctan\left(\frac{\sqrt{-a + 4}}{\sqrt{a}}\right) + \arctan\left(\frac{\sqrt{a} \sqrt{-a + 4}}{a - 2}\right)\right)} a - 2 \arctan\left(\frac{\sqrt{-a + 4}}{\sqrt{a}}\right) + 2 \arctan\left(\frac{\sqrt{a} \sqrt{-a + 4}}{a - 2}\right)\right)} \sqrt{a} \sqrt{-a + 4} + \frac{1}{4} {\left(a^{2} - 4 a\right)} \log\left(a\right) - \frac{1}{2} a + \frac{3}{2}
$$
If I want to find the limit when $a\to 0$:
sage: f.limit(a=0)
RuntimeError: ECL says: C-STACK overflow at size 1048576. Stack can probably be resized. Proceed with caution.
and once again
sage: f.limit(a=0, algorithm="sympy")
$$-i \pi + \frac{3}{2}$$
Which is only true for $a<0$.
I previously found [this strange bug](https://ask.sagemath.org/question/48058/stack-overflow-in-boolean-test/) a while ago (fixed upstream, still present in Sagemath 9.1), this makes me think the assumption `assume(a>0)` is the root of it all, unfortunately it cannot be skipped to evaluate the integral properly.
Do you know when the updated version of Maxima will be included with Sagemath?
Do you think all these issues are linked to this old ticket or should it be investigated independently?
Florentin JaffredoThu, 13 Aug 2020 11:13:05 -0500https://ask.sagemath.org/question/52986/How to define an integral to not be evaluatedhttps://ask.sagemath.org/question/52359/how-to-define-an-integral-to-not-be-evaluated/I'm trying to write the following script:
var('alpha beta x')
num = x**(alpha-1) * (1-x)**(beta-1)
denom = integral(x**(alpha-1) * (1-x)**(beta-1), x)
gammadist = num / denom
diff(gammadist, x)
So it is mich easier if the integral on `denom` does not get evaluated. However, I cannot find a way to do that on Sage and Sage always tries to evaluate it (without success). Is there a way to tell Sage to keep it as an integral?
ThankstomchoMon, 06 Jul 2020 12:22:40 -0500https://ask.sagemath.org/question/52359/Possible bug needs confirmation: Fricas can't handle "ln"https://ask.sagemath.org/question/49221/possible-bug-needs-confirmation-fricas-cant-handle-ln/ Hello, Sage Community!
Based on [this question](https://ask.sagemath.org/question/49215/confusion-about-integrating-a-string/) by user @Nasser, and follow up comments, there seems to be a bug in SageMath. The following statement is syntactically correct:
integrate('ln(x)', x, algorithm='fricas')
but it returns `integral(ln(x), x)`, which is indeed correct, but not expected. The equivalent statement
integrate(ln(x), x, algorithm='fricas')
returns the expected answer, 'x*log(x) - x'.
The problem seems to be that `ln` is not defined in Fricas, but `log` is. When Fricas receives an unknown function---let's say `func`---, it returns the abstract integral
t
++
| func(%A)d%A
++
which is converted by Sage to
integral(func(x), x)
And that is what happens with `ln`: Sage uses the Symbolic Ring `SR` to convert the string `'ln(x)'` to a symbolic math function, but then, it doesn't convert it to `log(x)`, and passes `ln(x)` to Fricas, which is a unknown function for it. So, an abstract integral is returned.
As @Nasser points out:
> But I am using sagemath? If a user has to know what each other CAS system that sagemath uses prefer or not prefer in terms of the input, what is the point then of using Sagemath? One can just use the other CAS system. I mean, I am using ln(t) which Sagemath knows, right? A user should not care if the system that sagemath ends up calling to do the integration knows or not know about ln(t). Sagemath should have then converted ln(t) automatically internally to log(t) in this case. At least this is what I would have expected. But thanks for the answer. I changed my calls to use "log(t)" and issue resolved for me.
I have to agree with this statement. SageMath should handle the adequate conversion for the CAS it passes its input.
In order to see more detail about this process, and a possible point where things go wrong, please see [the answer to this question](https://ask.sagemath.org/question/49215/confusion-about-integrating-a-string/).
Thanks in advance!dsejasFri, 27 Dec 2019 08:43:14 -0600https://ask.sagemath.org/question/49221/Differential of a integral?https://ask.sagemath.org/question/48486/differential-of-a-integral/
So I'm trying to calculate the frenet thrihedrom in the "clasic" way, (doing an arc length and his integrals)
but sage don't let me do a diff of a integral. Here ismy code:
var('t, n')
assume(n > 0)
c=vector((t,t**2,t**3))
l=sqrt(1+4*t*t+9*t**4)
L= l.integral(t, 0, n)
C=vector((t/L,(t/L)**2,(t/L)**3))
T=C.diff(t)
I get this error:
AttributeError: 'sage.rings.rational.Rational' object has no attribute 'diff'
Any idea of how to do a Diff of a integral? serk12Thu, 24 Oct 2019 12:17:51 -0500https://ask.sagemath.org/question/48486/Basic integral errorhttps://ask.sagemath.org/question/45750/basic-integral-error/In Sage 8.6 (Mac OS):
sage: integral(ln(1 + sqrt(x))/sqrt(x), x, 1, 4)
4*log(3) + 2*log(4/3) - 2
sage: n(_)
2.96981329957600
while
sage: numerical_integral(ln(1 + sqrt(x))/sqrt(x), 1, 4)
(1.8190850097688764, 2.0195900615958844e-14).
By hand, the correct result is `6*ln(3) - 4*ln(2) - 2`.Vincent 87Sun, 10 Mar 2019 23:37:06 -0500https://ask.sagemath.org/question/45750/How can I detect failure of analytical integration?https://ask.sagemath.org/question/45729/how-can-i-detect-failure-of-analytical-integration/I want to check whether an analytical integration worked, and to try numerical integration in case it failed. I'd like to know when this happened. I'm very inexperienced with sagemath, but I know my way around python.
I did this:
g(x) = integrate(f(x), x)
w0 = SR.wild(0)
w1 = SR.wild(1)
s=integrate(w0,w1,hold=True)
if(len(g.find(s)) : print('failed')
else: print('worked')
This worked for my **one** test case, but I'd like to know if this is correct and somewhat general, and also, it looked a bit convoluted to me. There probably is a better solution that I could not find.
Apologies if the above is too gross to contemplate!rbwFri, 08 Mar 2019 13:46:43 -0600https://ask.sagemath.org/question/45729/Indefinite integral is incorrecthttps://ask.sagemath.org/question/44077/indefinite-integral-is-incorrect/`indefinite_integral(sqrt(1+cos(x)**2), x).full_simplify()` gives `1/6*sin(x)^3`, which is incorrect.
proy87Fri, 26 Oct 2018 07:08:37 -0500https://ask.sagemath.org/question/44077/Integrate after Pullbackhttps://ask.sagemath.org/question/41246/integrate-after-pullback/ How to integrate a differential for obtained by pullback?
Let say I have calculated the pullback of some differential form, the result is (in latex):
$\rho^2\sin(\theta)\,\mathrm{d}\rho\wedge\mathrm{d}\theta\wedge\mathrm{d}\phi$
The triple integral is:
sage: integral(integral(integral(rho^2*sin(theta),rho,0,1),theta,0,pi/7),phi,0,pi/5)
But I want this process to be automatic, i.e. not manually write the integrand in the triple integral by myself. I want to be able to extract the integrand from the pullback and put it in the triple integral. I that possible?
Daniel
danielvolinskiFri, 23 Feb 2018 13:29:37 -0600https://ask.sagemath.org/question/41246/How to do numerical integral where the answer still have variables in it?https://ask.sagemath.org/question/41225/how-to-do-numerical-integral-where-the-answer-still-have-variables-in-it/I typed in
"numerical_integral(lambda d:exp(-(h-1)^2-(d-2)^2), 0,Infinity)"
and got
"unable to simplify to float approximation"
Are there anyway to compute the integral numerically and still leave a variable in the answer? Thanks,SarahWed, 21 Feb 2018 15:13:07 -0600https://ask.sagemath.org/question/41225/Integrate f(x,y) over a diskhttps://ask.sagemath.org/question/40272/integrate-fxy-over-a-disk/I have a homogeneous polynomial $F(x,y)$, and I'd like to integrate it over a disk $B_1(0)$, or approximate by integrating over a regular polygon. I just need the numerical value or an approximation of $$\int_{B_1(0)}F(x,y)dxdy$$
I looked up everywhere but I can't seem to find a way to do it. I used
integral ( integral ( F , x , -1 , 1 ), y , -1 , 1 )
for integrating over a rectangle, but I couldn't even find a way to integrate over regular polygons, and such an approximation would suffice.
Is there a way to integrate directly over disks ?
ThanksKoljaThu, 21 Dec 2017 13:30:14 -0600https://ask.sagemath.org/question/40272/integral containing products of bessel functionshttps://ask.sagemath.org/question/37836/integral-containing-products-of-bessel-functions/I am trying to find a closed form expression of an integral containing a product of bessel functions. Sage returns my command instead of a solution. Any help is appreciated. My code is below. I am solving this on a free CoCalc server.
phi, l, R, r_bar= var('phi l R r_bar')
phi = bessel_J(0, l*R) - (bessel_J(0,l*r_bar)*bessel_Y(0,l*R))/(bessel_Y(0,l*r_bar))
f = R*phi*phi
integrate(f, R)RieszRepresentWed, 07 Jun 2017 20:55:07 -0500https://ask.sagemath.org/question/37836/How can I Integrate the dirac_delta and heaviside functions in sage?https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/Is it possible to integrate the dirac_delta and the heaviside function in sage. I can't seem to do it. For the dirac_delta I've tried the following code:
reset()
var('x,a')
integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity)
from which, after evaluating, I get:
integrate(x^2*dirac_delta(-a + x), x, -Infinity, +Infinity)
i.e. the integration is not performed. I am expecting a^2.
For the heaviside fucntion I've tried:
reset()
var('x,x1,x2')
k(x,x1,x2)=heaviside(x-x1)*heaviside(x2-x)
integrate(k(x,x1,x2),x,-infinity,infinity)
from which, after evaluating, I get:
integrate(heaviside(-x + x2)*heaviside(x - x1), x, -Infinity, +Infinity)
I am expecting x2-x1. Am I doing something wrong or is it just not possible to do these integrations in sage?rtrwalkerThu, 31 Mar 2011 13:56:34 -0500https://ask.sagemath.org/question/8041/Integrate 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...
jrojasquTue, 28 Mar 2017 18:28:56 -0500https://ask.sagemath.org/question/37114/integral of piecewise function: errorhttps://ask.sagemath.org/question/36536/integral-of-piecewise-function-error/I get a weird error when trying to take an integral of a very simple piecewise function:
blah = piecewise([((0, 0.01), 0.0001), ([0.01, 0.02], 0.0002), ((0.02, 0.03), 0.0003)])
blah.integral(x, 0.01, 0.025)
gives:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-132-73d5f18533d2> in <module>()
1 blah = piecewise([((Integer(0), RealNumber('0.01')), RealNumber('0.0001')), ([RealNumber('0.01'), RealNumber('0.02')], RealNumber('0.0002')), ((RealNumber('0.02'), RealNumber('0.03')), RealNumber('0.0003')), ([RealNumber('0.03'), RealNumber('0.04')], RealNumber('0.0004'))])
----> 2 blah.integral(x, RealNumber('0.01'), RealNumber('0.025'))
/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/function_factory.pyc in new_f(ex, *args, **kwds)
400 new_args = list(ex._unpack_operands())
401 new_args.extend(args)
--> 402 return f(ex, *new_args, **kwds)
403 return new_f
/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in integral(cls, self, parameters, variable, x, a, b, definite)
793 """
794 if a != None and b != None:
--> 795 F = self.integral(x)
796 return F(b) - F(a)
797
/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/function_factory.pyc in new_f(ex, *args, **kwds)
400 new_args = list(ex._unpack_operands())
401 new_args.extend(args)
--> 402 return f(ex, *new_args, **kwds)
403 return new_f
/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in integral(cls, self, parameters, variable, x, a, b, definite)
828 else:
829 try:
--> 830 assume(start < x)
831 except ValueError: # Assumption is redundant
832 pass
/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/assumptions.pyc in assume(*args)
513 else:
514 try:
--> 515 x.assume()
516 except KeyError:
517 raise TypeError("assume not defined for objects of type '%s'"%type(x))
AttributeError: 'numpy.bool_' object has no attribute 'assume'
Is this a bug or did I make a mistake?
UPDATE: I uploaded an example to SMC: https://cloud.sagemath.com/projects/34b4b62a-2621-47c8-9bda-cde3a855f995/files/numpy_bool_traceback/stanFri, 10 Feb 2017 07:06:56 -0600https://ask.sagemath.org/question/36536/Integration and differentiation symbolshttps://ask.sagemath.org/question/36441/integration-and-differentiation-symbols/I have seen this in the examples but it doesn't appear to be very easy unless I am totally missing something. It is important that the proper symbology be used in presentation form to show the integration symbol and or differentiation symbol. But can't seem to find any way of doing it . This should be straight forward like show(integral()) and it puts the integral symbol on the screen especially in showing symbolic calculations.
How do you get the symbols on the screen? NT4MAXIMUSDThu, 02 Feb 2017 07:15:53 -0600https://ask.sagemath.org/question/36441/Two ways of integrating x↦xⁿsin(x) give contradictory results. Bug?https://ask.sagemath.org/question/36185/two-ways-of-integrating-x-xnsinx-give-contradictory-results-bug/**First way:**
var('x,n')
integral(x^n*sin(x),x)
gives just
integrate(x^n*sin(x), x)
not very informative, let us try to add an assumption to get nicer results.
**Second way:**
assume(n,'integer')
integral(x^n*sin(x),x)
gives
1/4*(((-1)^n - 1)*gamma(n + 1, I*x) - ((-1)^n - 1)*gamma(n + 1, -I*x))*(-1)^(-1/2*n)
Uhm, looks better, but... wait, isn't `(-1)^n-1` equal to `0` for even values of `n` ? That would make the whole thing equal to `0` for even `n`.
I = integral(x^n*sin(x),x)
for k in range(10):
print I.subs(n==2*k)
prints only `0`s. Weird, non-zero functions should not have zero integrals.
**Third way :**
Let us try to do the integration with particular values of `n`.
for n in range(5):
print integral(x^n*sin(x),x)
prints
-cos(x)
-x*cos(x) + sin(x)
-(x^2 - 2)*cos(x) + 2*x*sin(x)
-(x^3 - 6*x)*cos(x) + 3*(x^2 - 2)*sin(x)
Looks better, but is clearly different from the previous answer.
**Question:**
I am working on the cloud, with SageMath 7.4 kernel. Is this a bug or did I misunderstood the meaning of the `'integer'`assumption ?
If this is a bug, how should I report it, is posting this question here enough ?
P.S. I did read the [wiki page about reporting bugs](http://doc.sagemath.org/html/en/developer/trac.html#reporting-bugs), but, gosh, is it really necessary to have a google account in order to report a bug ? Both sage-devel and sage-support are on Google Groups. lbWed, 04 Jan 2017 14:44:16 -0600https://ask.sagemath.org/question/36185/why I can not get a numerical answer by show(integral(x^x,x,1,2))https://ask.sagemath.org/question/35629/why-i-can-not-get-a-numerical-answer-by-showintegralxxx12/ show(integral(x^x,x,1,2))j5464654Thu, 17 Nov 2016 20:34:19 -0600https://ask.sagemath.org/question/35629/ValueError: Computation failed since Maxima requested additional constraintshttps://ask.sagemath.org/question/35572/valueerror-computation-failed-since-maxima-requested-additional-constraints/ I am trying integrate an exponential decay function.
reset()
var('t,v')
tmax=0.01
fx=((1.66533453693773e-16 +
1.99493199737333e-17*i)*e^((2.57571741713036e-13 -
1.81898940354586e-12*i)*t) + (1.00000000000000 -
1.99493199737333e-17*i)*e^(-(30.0000000000003 - 12784.0000000000*i)*t))
forget()
assume(v>0)
integrate(fx*exp(-i*v*t),(t,0,tmax))
the error shows
Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(-%i*v/(0.257571741713036e-12-0.181898940354586e-11*%i)
>0)', see `assume?` for more details)
Is -%i*v/(0.257571741713036e-12-0.181898940354586e-11*%i)
+12784.0*%i/(0.257571741713036e-12-0.181898940354586e-11*%i)
-30.00000000000026/(0.257571741713036e-12-0.181898940354586e-11*%i)-1
equal to -1?
no assumption working. please help.mkrMon, 14 Nov 2016 13:01:46 -0600https://ask.sagemath.org/question/35572/What is the problem with that integral ?https://ask.sagemath.org/question/35436/what-is-the-problem-with-that-integral/I try to integrate this:
sage: integrate(integrate(integrate(2*cos(z)*sin(atan((2*cos(y)-0.5+x)/(2*sin(y)))),y,0,pi/2),x,0,1),z,0,pi/2);
There is a problem :
"ECL says: In function ZEROP, the value of the only argument is
((RAT SIMP) -0.5 1.0)
which is not of the expected type NUMBER"
Could you help me please ?
Wolfram does it so I think it is not an error of the function maybe a limit ?
> It works with python:
>
> def f1(y,x,z): ... return 2*np.cos(z)*np.sin(np.arctan((2*np.cos(y)-0.5+x)/(2*np.sin(y))))
> ...
> >>> tplquad(f1,0,np.pi/2, lambda z: 0, lambda z:1, lambda z, x: 0, lambda z,
> x: np.pi/2) (1.9792263101075036,
> 2.1973826204252407e-14)dx6665Fri, 04 Nov 2016 14:24:13 -0500https://ask.sagemath.org/question/35436/what is the problem with that integral ?https://ask.sagemath.org/question/35437/what-is-the-problem-with-that-integral/ I try to integrate this:
sage: integrate(integrate(integrate(2*cos(z)*sin(atan((2*cos(y)-0.5+x)/(2*sin(y)))),y,0,pi/2),x,0,1),z,0,pi/2);
There is a problem :
"ECL says: In function ZEROP, the value of the only argument is
((RAT SIMP) -0.5 1.0)
which is not of the expected type NUMBER"
Could you help me please ?
dx6665Fri, 04 Nov 2016 14:25:12 -0500https://ask.sagemath.org/question/35437/integrate and simplifyhttps://ask.sagemath.org/question/33286/integrate-and-simplify/ Hello,
Here is a list of command lines that used to work fine one year ago, but today, it's not :
> fksin=piecewise([[(-2,0),f1(t)*sin(k*w*t)],[(0,1),f2(t)*sin(k*w*t)],[(1,2),f3(t)*sin(k*w*t)]])
>var('t k')
>assume(k,'integer')
>f1(t)=0
>f2(t)=t
>f3(t)=2-t
>T=4
>w=2*pi/T
>f=piecewise([[(-2,0),f1],[(0,1),f2],[(1,2),f3]])
>fkcos=piecewise([[(-2,0),f1(t)*cos(k*w*t)],[(0,1),f2(t)*cos(k*w*t)],[(1,2),f3(t)*cos(k*w*t)]])
>bkf=(2/T)*integral(fksin,t,-T/2,T/2)
>bkf.simplify_trig()
4*sin(1/2*pi*k)/(pi^2*k^2)
But today, the answer is :
**Error in lines 1-1
Traceback (most recent call last):
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/misc/functional.py", line 664, in integral
return x.integral(*args, **kwds)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 837, in integral
F = self.integral(x)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 877, in integral
area += fun.integral(x, start, end)
File "sage/symbolic/expression.pyx", line 11352, in sage.symbolic.expression.Expression.integral (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:60288)
return integral(self, *args, **kwds)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 761, in integrate
return definite_integral(expression, v, a, b, hold=hold)
File "sage/symbolic/function.pyx", line 988, in sage.symbolic.function.BuiltinFunction.__call__ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:11343)
res = super(BuiltinFunction, self).__call__(
File "sage/symbolic/function.pyx", line 503, in sage.symbolic.function.Function.__call__ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:7110)
res = g_function_evalv(self._serial, vec, hold)
File "sage/symbolic/function.pyx", line 1059, in sage.symbolic.function.BuiltinFunction._evalf_or_eval_ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:12072)
return self._eval0_(*args)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 176, in _eval_
return integrator(*args)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/integration/external.py", line 24, in maxima_integrator
result = maxima.sr_integral(expression, v, a, b)
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 776, in sr_integral
return max_to_sr(maxima_eval(([max_integrate],[sr_to_max(SR(a)) for a in args])))
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 1630, in max_to_sr
args=[max_to_sr(a) for a in max_args]
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 1630, in max_to_sr
args=[max_to_sr(a) for a in max_args]
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 1630, in max_to_sr
args=[max_to_sr(a) for a in max_args]
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 1630, in max_to_sr
args=[max_to_sr(a) for a in max_args]
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 1631, in max_to_sr
return op(*args)
File "sage/symbolic/function.pyx", line 841, in sage.symbolic.function.GinacFunction.__call__ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:9780)
res = super(GinacFunction, self).__call__(*args, **kwds)
File "sage/symbolic/function.pyx", line 979, in sage.symbolic.function.BuiltinFunction.__call__ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/function.cpp:11148)
res = method()
File "sage/symbolic/expression.pyx", line 7169, in sage.symbolic.expression.Expression.cos (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:39288)
g_hold_wrapper(g_cos, self._gobj, hold))
RuntimeError: stub**
pvquereMon, 02 May 2016 03:41:16 -0500https://ask.sagemath.org/question/33286/Integrating with constant integrandhttps://ask.sagemath.org/question/30185/integrating-with-constant-integrand/How can you specify an integral with a constant integrand? For example, I know that
f = x
f.integrate(x,0,1)
works fine, but
f = 1
f.integrate(x,0,1)
doesn't, since the Integer class has no integrate() method. (Should it?) I can get around this with something like
f = x-x+1
f.integrate(x,0,1)
but that seems awfully kludgey.Jeremy MartinWed, 21 Oct 2015 13:20:59 -0500https://ask.sagemath.org/question/30185/symbolic and numeric double integration methodhttps://ask.sagemath.org/question/30329/symbolic-and-numeric-double-integration-method/ fidbc corrected some of my previous code for double integration. However I am only able to good a good numerical double integral. Is their a sage method that can do both symbolic ie variables as bounds and numeric integration if not what is the symbolic integration method. Using the current integrate method I have I get back the wrong results in bracketed around the word integrate.
Here is my double integration code corrected by fidbc for numerical integration does anyone know a better integration method or methods for symbolic double integration.
from sage.plot.plot3d.shapes import Box
x,y = var('x,y')
@interact
def interplay(function= input_box(sin(x*y)),lower_x_bound= input_box(0),upper_x_bound = input_box(1),lower_y_bound=input_box(0),upper_y_bound=input_box(1),dydx = checkbox(default = False)):
if dydx==False :
prev = integral(function,x,lower_x_bound,upper_x_bound)
result = n(integral(prev,y,lower_y_bound,upper_y_bound))
q ="$\int_%s^{%s} \int_%s^%s %s \,dx\,dy = %s $" % (lower_y_bound, upper_y_bound,lower_x_bound,upper_x_bound,function,result)
else:
result = n(integral(integral(function,y,lower_y_bound,upper_y_bound),x,lower_x_bound,upper_x_bound))
q = "$\int_%s^{%s} \int_%s^%s %s \,dy\,dx = %s $" % (lower_x_bound,upper_x_bound,lower_y_bound,upper_y_bound,function,result)
html("%s" %q)collabmathWed, 28 Oct 2015 21:48:00 -0500https://ask.sagemath.org/question/30329/Double Integration not workinghttps://ask.sagemath.org/question/30300/double-integration-not-working/I am making a double integral interactive. However instead of giving my result for sin(x*y) from x = 0 to x = 1 and y= 0 to y = 1 it gives integrate(-cos(2*y)/y + 1/y,y,0,1).
from sage.plot.plot3d.shapes import Box
x,y = var('x,y')
html("<h1>Double Integrator<h1>")
#permutations = ["dx dy","dy dx"]
@interact
def interplay(function= input_box(sin(x*y)),lower_x_bound= input_box(0),upper_x_bound = input_box(1),lower_y_bound=input_box(0),upper_y_bound=input_box(1),dydx = checkbox(default = False)):
try:
if dydx==False :
prev = integral(function,x,lower_x_bound,upper_x_bound)
result = integral(prev,y,lower_y_bound,upper_y_bound)
q ="$\int_%s^{%s} \int_%s^%s %s \,dx\,dy = %s $" % (lower_y_bound, upper_y_bound,lower_x_bound,upper_x_bound,function,result)
# puts expressions inside latex
else:
result = integral(integral(function,y,lower_y_bound,upper_y_bound),y,lower_x_bound,upper_x_bound)
#actually calculates integral
q = "$\int_%s^{%s} \int_%s^%s %s \,dy\,dx = %s $" % (lower_x_bound,upper_x_bound,lower_y_bound,upper_y_bound,function,result)
#if type(lower_x_bound)!=float or type(upper_x_bound)!=float or type(lower_y_bound)!= float or type(upper_y_bound)!= float :
html("%s" %q)
except:
print(" "+"Please enter better bounds")
#show(result)
html("<p> Created by Hans Gundlach </p>")
collabmathTue, 27 Oct 2015 16:01:32 -0500https://ask.sagemath.org/question/30300/Is this a known bug with integral()https://ask.sagemath.org/question/30075/is-this-a-known-bug-with-integral/I've tried to compute the following integral wth integral() in a SageMathCloud worksheet: $\displaystyle \int_{-\pi/6}^{\pi/6}\frac{\cos x}{1+\sin x}dx$.
The output was an error message (saying the integral is divergent), just like the one I got in SageMathCell (see link):
https://sagecell.sagemath.org/?z=eJzzVLBVyMwrSU0vSszRSM4v1qjQ1Ncw1C7OzAOyNHUUKnQUdAsy9c10FECkJi9XcUZ-uYanJgDa5Q_i&lang=sage
So I tried with integrate() and with numerical_integral() as well. I was never able to obtain the value of this integral, which turns out to be $\ln(3)$ after an obvious substitution.
Is this a bug?
Note that replacing 1 by 1.1 yields this:
https://sagecell.sagemath.org/?z=eJzzVLBVyMwrSU0vSszRSM4v1qjQ1Ncw1DPULs7MA7I1dRQqdBR0CzL1zXQUQKQmL1dxRn65hqcmAPXGEEE=&lang=sage
while we get that when replacing 1 by 2:
https://sagecell.sagemath.org/?z=eJzzVLBVyMwrSU0vSszRSM4v1qjQ1Ncw0i7OzAOyNHUUKnQUdAsy9c10FECkJi9XcUZ-uYanJgDbCA_j&lang=sageJulienSat, 17 Oct 2015 10:01:02 -0500https://ask.sagemath.org/question/30075/Wrong error messagehttps://ask.sagemath.org/question/28851/wrong-error-message/The best things in Sage are 'mathematica_free'. No, no, I never would say this!
(exp(2*x)/cosh(exp(x))).integral(x,algorithm='mathematica_free')
I*(log(I*e^(-e^x) + 1) - log(-I*e^(-e^x) + 1))*e^x + I*polylog(2, I*e^(-e^x)) - I*polylog(2, -I*e^(-e^x))
BUT:
(exp(3*x)/cosh(exp(x))).integral(x,algorithm='mathematica_free')
AttributeError: 'NoneType' object has no attribute 'groups'
I think Sage wanted to say: "Mathematica could not find a formula for your integral."
Peter LuschnyMon, 17 Aug 2015 05:16:08 -0500https://ask.sagemath.org/question/28851/Simplify result of this definite integralhttps://ask.sagemath.org/question/10503/simplify-result-of-this-definite-integral/It is well known that for $n\in\mathbb{N}$ and $n>0$ (an maybe even for more than these restrictions): $$I_n = \int_0^\infty\frac{x^n}{e^x-1}dx = \zeta(n+1)n!$$ which, analytically can be shown easily by expanding $1/(1-e^{-x})$ into a geometric series, which leads to trivial integrals, and by using $\zeta(n+1)=\sum_{l=1}^\infty l^{-(n+1)}$. So, eg.: $$I_1 = \pi^2/6$$ $$I_2 = 2\zeta(3)$$ a.s.o...
Now, if I try even the simplest case with sage, I get this 'nifty' little results
sage: integrate(x/(exp(x)-1),x,0,oo)
-1/6*pi^2 + limit(-1/2*x^2 + x*log(-e^x + 1) + polylog(2, e^x), x,
+Infinity, minus)
**Is there any trick to simplify this** down to the final result, or is this about as far as I can get with sage alone?
PS.: it is probably needless to say that (once again ... :( ...)
In[1]:= Integrate[x/(Exp[x] - 1), {x, 0, Infinity}]
Out[1]:= Pi^2/6
MarkWed, 04 Sep 2013 10:06:49 -0500https://ask.sagemath.org/question/10503/improper integral error?https://ask.sagemath.org/question/26152/improper-integral-error/
Is there an alternative way to calculate this improper integral?
integral(log(x)/(x^2 - 1),x,0,infinity)
the integral is convergent to pi^2/4 but Sage says divergent.rafarobWed, 11 Mar 2015 08:33:19 -0500https://ask.sagemath.org/question/26152/