Page 1 of 1
Computing with SageMath Directly from the Forum
Posted: Wed May 27, 2020 9:45 pm
by Eli
Type some Sage code below and press Compute (
Test examples). Choose a different language from the dropdown menu (say
Macaulay2), write some code in the editor, and click the "Compute" button to run it (
reload browser if you do not see the editor).
Re: Carry Computations with SageMath Directly from the Forum
Posted: Sun Jul 12, 2020 9:09 pm
by Eli
Try to integrate 1/(1+x^4) with Sage or Maxima, i.e.,
integrate(1/(1 + x^4), x);
The answer should be
\begin{equation}
\begin{split}
& \int\frac{1}{1 + x^4}{\rm d}x \\
& = 1/4\sqrt{2}\times{\rm tan}^{-1}\left(1/2\sqrt{2}\times(2x + \sqrt{2})\right) + 1/4\sqrt{2}\times {\rm tan}^{-1}\left(1/2\times \sqrt{2}\times(2x - \sqrt{2})\right)\\
& + 1/8\sqrt{2}\times{\rm log}(x^2 + \sqrt{2}\times x + 1) - 1/8\sqrt{2}\times{\rm log}(x^2 - \sqrt{2}\times x + 1)
\end{split}
\end{equation}
Try this in Macaulay2:
R = QQ[x,y,z]
curve = ideal( x^4-y^5, x^3-y^7 )
gb curve
dim curve
degree curve
curve1 = saturate(curve,ideal(x))
Re: Carry Computations with SageMath Directly from the Forum
Posted: Sat Aug 29, 2020 11:13 am
by Eli
Test these solutions in Sage (see reference manual
here):
x,y,z=var('x,y,z')
# Next we define the parameters
sigma=10
rho=28
beta=8/3
# The Lorenz equations
lorenz=[sigma*(y-x),x*(rho-z)-y,x*y-beta*z]
# Time and initial conditions
times=srange(0,50.05,0.005)
ics=[0,1,1]
sol=desolve_odeint(lorenz,ics,times,[x,y,z],rtol=1e-13,atol=1e-14)
vis=line(zip(sol[:,0],sol[:,1]))
vis.show()
The output is:
#from sage.calculus.desolvers import desolve_odeint -- optional
x,y=var('x,y')
f=[x*(1-y),-y*(1-x)]
sol=desolve_odeint(f,[0.5,2],srange(0,10,0.1),[x,y])
vis=line(zip(sol[:,0],sol[:,1]))
print(sol)
vis.show()
#y = function('y')(x)
#eq = diff(y,x) - (x - 2)/(y^2 + 1)*sin(x - 2) - sin(y) == 0
#desolve_laplace(eq,y,ics=[0,3])
y1,y2,y3=var('y1,y2,y3')
f1=77.27*(y2+y1*(1-8.375*1e-6*y1-y2))
f2=1/77.27*(y3-(1+y1)*y2)
f3=0.16*(y1-y3)
f=[f1,f2,f3]
ci=[0.2,0.4,0.7]
t=srange(0,10,0.01)
v=[y1,y2,y3]
sol=desolve_odeint(f,ci,t,v,rtol=1e-3,atol=1e-4,h0=0.1,hmax=1,hmin=1e-4,mxstep=1000,mxords=17)
print(sol)
Solve system of ODEs:
t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) + y - 1 == 0
de2 = diff(y,t) - x + 1 == 0
sol = desolve_system([de1, de2], [x,y])
#Add ics if you want
#sol = desolve_system([de1, de2], [x,y],ics=[0,0.5,1],ivar=t)
print(sol)
from sage.calculus.desolvers import desolve_system_rk4
x,y,t=var('x y t')
P=desolve_system_rk4([x*(1-y),-y*(1-x)],[x,y],ics=[0,0.5,2],ivar=t,end_points=20)
Q=[ [i,j] for i,j,k in P]
LP=list_plot(Q)
Q=[ [j,k] for i,j,k in P]
LP=list_plot(Q)
plot(LP)
var('t,x,y,X,Y')
f(t,x,y,X,Y)=[X, Y, -x/(x^2+y^2)^(3/2), -y/(x^2+y^2)^(3/2)]
ics = [0.8, 0, 0, 1.22474487139159]
t = 100*pi
sol = desolve_mintides(f, ics, 0, t, t, 1e-12, 1e-12) # optional -tides
sol # optional -tides # abs tol 1e-5
Re: Carry Computations with SageMath Directly from the Forum
Posted: Sat Aug 29, 2020 1:02 pm
by Eli
Plotting in Sage (see more from
here):
Code: Select all
f(x) = (x-3)*(x-5)*(x-7)+40
P = line([(2,0),(2,f(2))], color='black')
P += line([(8,0),(8,f(8))], color='black')
P += polygon([(2,0),(2,f(2))] + [(x, f(x)) for x in [2,2.1,..,8]] + [(8,0),(2,0)], rgbcolor=(0.8,0.8,0.8),aspect_ratio='automatic')
P += text("$\\int_{a}^b f(x) dx$", (5, 20), fontsize=16, color='black')
P += plot(f, (1, 8.5), thickness=3)
P # show the result
var("x y")
G = Graphics()
counter = 0
for col in colors.keys(): # Long time
G += implicit_plot(x^2 + y^2 == 1 + counter*.1, (x,-4,4),(y,-4,4), color=col)
counter += 1
G
var("x y")
contour_plot(y^2 + 1 - x^3 - x, (x,-pi,pi), (y,-pi,pi), fill=False, cmap='hsv', labels=True)
var("x y")
parametric_plot([cos(x) + 2 * cos(x/4), sin(x) - 2 * sin(x/4)], (x,0, 8*pi), fill=True)
def b(n): return lambda x: bessel_J(n, x)
plot([b(n) for n in [1..5]], 0, 20, fill='axis')
See the attached outputs.
Re: Carry Computations with SageMath Directly from the Forum
Posted: Sat Aug 29, 2020 2:34 pm
by Eli
Try to solve
\begin{equation}
\frac{{\rm d}y}{{\rm d}x} = \frac{x - 2}{y^2 + 1}\times {\rm sin}(x - 2) - {\rm sin}(y).
\end{equation}
Re: Carry Computations with SageMath Directly from the Forum
Posted: Sat Aug 29, 2020 3:28 pm
by Eli
We can label and decorate our plots:
x,y,z=var('x,y,z')
# Next we define the parameters
sigma=10
rho=28
beta=8/3
# The Lorenz equations
lorenz=[sigma*(y-x),x*(rho-z)-y,x*y-beta*z]
# Time and initial conditions
times=srange(0,50.05,0.005)
ics=[0,1,1]
sol=desolve_odeint(lorenz,ics,times,[x,y,z],rtol=1e-13,atol=1e-14)
vis=line(zip(sol[:,0],sol[:,1]), color='purple')
vis.axes_labels(['X axis', 'Y axis'])
vis.legend(False)
vis.show(title='Lorenz Equations', frame=True, legend_loc="lower right")
vis.show()

Re: Carry Computations with SageMath Directly from the Forum
Posted: Wed Sep 16, 2020 11:28 pm
by Eli
Try the following Algebra problems in Sage:
Example 1: Transpose of a Matrix
A = matrix(QQ, 5, range(25))
A.T
Example 2:
LU Factorization of a Matrix
A = matrix(QQ, [[ 2, 1, 1],
[ 4,-6, 0],
[-2, 7, 2]])
(P,L,U) = A.LU()
#table([[A, '=', P, '$\\cdot$', L, '$\\cdot$', U]])
print(P, L, U, sep='\n')
A = matrix(QQ, [[ 2, 1, 1],
[ 4,-6, 0],
[-2, 7, 2]])
(P,L,U) = A.LU(pivot='nonzero')
#table([[A, '=', P, '$\\cdot$', L, '$\\cdot$', U]])
print(P, L, U, sep='\n')
A = matrix(QQ, [[ 1, -4, 1, 0, -2, 1, 3, 3, 2],
[-1, 4, 0, -4, 0, -4, 5, -7, -7],
[ 0, 0, 1, -4, -1, -3, 6, -5, -6],
[-2, 8, -1, -4, 2, -4, 1, -8, -7],
[ 1, -4, 2, -4, -3, 2, 5, 6, 4]])
P, L, U = A.LU()
U
A.rref()
A.pivots()
D = matrix(QQ, [[ 1, 0, 2, 0, -2, -1],
[ 3, -2, 3, -1, 0, 6],
[-4, 2, -3, 1, -1, -8],
[-2, 2, -3, 2, 1, 0],
[ 0, -1, -1, 0, 2, 5],
[-1, 2, -4, -1, 5, -3]])
P, L, U = D.LU(pivot='nonzero')
P
L
U
D == L*U
Example 3: Modified Gram-Schmidt Algorithm -- QR decomposition
A = matrix(QQbar, [[-2, 0, -4, -1, -1],
[-2, 1, -6, -3, -1],
[1, 1, 7, 4, 5],
[3, 0, 8, 3, 3],
[-1, 1, -6, -6, 5]])
Q, R = A.QR()
Q
R
Q.conjugate_transpose()*Q
Q*R == A
See more examples
here.
Re: Carry Computations with SageMath Directly from the Forum
Posted: Thu Sep 17, 2020 12:00 am
by Eli
Knots Using Sage:
B = BraidGroup(4)
K = Knot(B([1,1,1,2,-1,2,-3,2,-3]))
plot(K)
K.alexander_polynomial()
K.jones_polynomial()
K.determinant()
K.signature()
Re: Computing with SageMath Directly from the Forum
Posted: Thu Apr 15, 2021 6:21 pm
by Eli
Interactive Fourier Series with Sage (see
source):
## Interactive Fourier Series, via: http://www.walkingrandomly.com/?p=1879
def ftermSquare(n):
return(1/n*sin(n*x*pi/3))
def ftermSawtooth(n):
return(1/n*sin(n*x*pi/3))
def ftermParabola(n):
return((-1)^n/n^2 * cos(n*x))
def fseriesSquare(n):
return(4/pi*sum(ftermSquare(i) for i in range (1,2*n,2)))
def fseriesSawtooth(n):
return(1/2-1/pi*sum(ftermSawtooth(i) for i in range (1,n)))
def fseriesParabola(n):
return(pi^2/3 + 4*sum(ftermParabola(i) for i in range(1,n)))
@interact
def plotFourier(n=slider(1, 30,1,3,'Number of terms')
,Function=['Square Wave','Saw Tooth','Periodic Parabola']):
if Function=='Saw Tooth':
show(plot(fseriesSawtooth(n),x,-6,6,figsize=(7,3)))
if Function=='Square Wave':
show(plot(fseriesSquare(n),x,-6,6,figsize=(7,3)))
if Function=='Periodic Parabola':
show(plot(fseriesParabola(n),x,-6,6,figsize=(7,3)))