Try CoCalc Published Files Email HelpDocumentationSign In
Path: sage-9.4.ipynb
License: none
Image: ubuntu2004-dev
Project: Software 20.04
Edit | Raw | Embed | Download

# Hello Sage 9.4

In [3]:
version()

Out[3]:
'SageMath version 9.4, Release Date: 2021-08-22'
In [2]:
M = matrix([[sin(x), cos(x)], [-cos(x), sin(x)]])
M_sympy = M._sympy_()
M_sympy

Out[2]:
$\displaystyle \left[\begin{matrix}\sin{\left(x \right)} & \cos{\left(x \right)}\\- \cos{\left(x \right)} & \sin{\left(x \right)}\end{matrix}\right]$
In [4]:
M_sympy.subs(x, pi/4)

Out[4]:
$\displaystyle \left[\begin{matrix}\frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2}\\- \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2}\end{matrix}\right]$
In [5]:
in_small_oblong(x, y) = x^2 + 3 * y^2 <= 42
SmallOblongUniverse = ConditionSet(QQ^2, in_small_oblong)
SmallOblongUniverse

Out[5]:
{ (x, y) ∈ Vector space of dimension 2 over Rational Field : x^2 + 3*y^2 <= 42 }
In [6]:
parity_check(x, y) = abs(sin(pi/2*(x + y))) < 1/1000
EvenUniverse = ConditionSet(ZZ^2, parity_check); EvenUniverse

Out[6]:
{ (x, y) ∈ Ambient free module of rank 2 over the principal ideal domain Integer Ring : abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }
In [7]:
SmallOblongUniverse & EvenUniverse

Out[7]:
{ (x, y) ∈ Free module of degree 2 and rank 2 over Integer Ring Echelon basis matrix: [1 0] [0 1] : x^2 + 3*y^2 <= 42, abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }
In [8]:
symbolic_expression(lambda x,y: x^2+y^2) == (SR.var("x")^2 + SR.var("y")^2).function(SR.var("x"), SR.var("y"))

Out[8]:
(x, y) |--> x^2 + y^2 == x^2 + y^2
In [9]:
predicate(x, y, z) = sqrt(x^2 + y^2 + z^2) < 12  # preparser syntax, creates globals

In [10]:
ConditionSet(ZZ^3, predicate)

Out[10]:
{ (x, y, z) ∈ Ambient free module of rank 3 over the principal ideal domain Integer Ring : sqrt(x^2 + y^2 + z^2) < 12 }
In [11]:
M = Manifold(2, 'M')
X.<x,y> = M.chart()
M.identity_map().display()

Out[11]:
Id_M: M → M (x, y) ↦ (x, y)
In [12]:
v = M.vector_field(-y, x, name='v')
v.display()

Out[12]:
v = -y ∂/∂x + x ∂/∂y
In [13]:
f = M.scalar_field(x^2 + y^2, name='f')
(v*diff(f)).display()

Out[13]:
v⊗df = -2*x*y ∂/∂x⊗dx - 2*y^2 ∂/∂x⊗dy + 2*x^2 ∂/∂y⊗dx + 2*x*y ∂/∂y⊗dy
In [14]:
sage: M = Manifold(2, 'M')
sage: X.<x,y> = M.chart(coord_restrictions=lambda x,y: [x^2+y^2<1, x+y>0])
sage: X.plot(max_range=1, number_values=41)

Out[14]:
In [0]:


In [0]:


In [0]:


In [15]:
from veerer import *
T = VeeringTriangulation([(0,1,2), (-1,-2,-3)], [RED, RED, BLUE])
print(T.is_core())
F = T.flat_structure_middle()
print(F)
F.plot(vertical_train_track=True)

Out[15]:
True FlatVeeringTriangulation(Triangulation("(0,1,2)(~2,~0,~1)"), [(1, 2), (-2, -1), (1, -1), (-1, 1), (2, 1), (-1, -2)])
In [16]:
F.plot(horizontal_train_track=True)

Out[16]:
In [17]:
import flatsurf.geometry.similarity_surface_generators as sfg
T = sfg.translation_surfaces.regular_octagon()
T

Out[17]:
TranslationSurface built from 1 polygon
In [18]:
T.stratum()

Out[18]:
H_2(2)
In [19]:
import flatsurf
flatsurf.__version__

Out[19]:
'0.4.1'
In [20]:
from surface_dynamics.all import *
o = Origami('(1,2)', '(1,3)')
o

Out[20]:
(1,2)(3) (1,3)(2)
In [21]:
o.lyapunov_exponents_approx()

Out[21]:
[0.333479541721790]
In [22]:
q = QuadraticStratum(1, 1, 1, 1)
q.orientation_cover()

Out[22]:
H_5(2^4)
In [23]:
%display latex

In [24]:
@interact
def f(x=slider(-1,10,0.1)):
print(x)

Out[24]:
In [25]:
#import matplotlib.pyplot
#matplotlib.pyplot.style.available

In [26]:
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
plot(sin(x^2), (x, 0, 2*pi))

Out[26]:

## Bär–Faddeev–LeVerrier algorithm for the Pfaffian of skew-symmetric matrices

In [27]:
A = matrix([(0, 0, 1, 0, -1, -2, -1, 0, 2, 1),
(0, 0, 1, -3/2, 0, -1, 1/2, 3, 3/2, -1/2),
(-1, -1, 0, 2, 0, 5/2, 1, 0, -2, 1),
(0, 3/2, -2, 0, 5/2, -1, 2, 0, -1, -3/2),
(1, 0, 0, -5/2, 0, 0, -1, 1/2, 1, -1),
(2, 1, -5/2, 1, 0, 0, 2, 1, 2, 1),
(1, -1/2, -1, -2, 1, -2, 0, 0, -3, -1),
(0, -3, 0, 0, -1/2, -1, 0, 0, 1/2, 1/2),
(-2, -3/2, 2, 1, -1, -2, 3, -1/2, 0, 1),
(-1, 1/2, -1, 3/2, 1, -1, 1, -1/2, -1, 0)])
%time A.pfaffian(algorithm='bfl')

Out[27]:
CPU times: user 853 µs, sys: 0 ns, total: 853 µs Wall time: 8.53 ms
$\frac{817}{16}$

## Polyhedral geometry

In [28]:
fcube = polytopes.hypercube(4)
tfcube = fcube.face_truncation(fcube.faces(0)[0])
tfcube.facets()[-1]

Out[28]:
$\verb|A|\phantom{\verb!x!}\verb|3-dimensional|\phantom{\verb!x!}\verb|face|\phantom{\verb!x!}\verb|of|\phantom{\verb!x!}\verb|a|\phantom{\verb!x!}\verb|Polyhedron|\phantom{\verb!x!}\verb|in|\phantom{\verb!x!}\verb|QQ^4|\phantom{\verb!x!}\verb|defined|\phantom{\verb!x!}\verb|as|\phantom{\verb!x!}\verb|the|\phantom{\verb!x!}\verb|convex|\phantom{\verb!x!}\verb|hull|\phantom{\verb!x!}\verb|of|\phantom{\verb!x!}\verb|8|\phantom{\verb!x!}\verb|vertices|$
In [29]:
sp = tfcube.schlegel_projection(tfcube.facets()[-1])
sp.plot()  # The proper Schlegel diagram is shown

Out[29]:
In [0]:
sp = tfcube.schlegel_projection(tfcube.facets()[4],1/2)
sp.plot()


## *-insertion algorithm from ​https://arxiv.org/abs/1911.08732:

In [0]:
from sage.combinat.rsk import RuleStar
p,q = RuleStar().forward_rule([1,1,2,2,4,4], [1,3,2,4,2,4])
ascii_art(p, q)

In [0]:
line1,line2 = RuleStar().backward_rule(p, q)
line1,line2


## Spheres added to the manifold catalog

In [0]:
S3 = manifolds.Sphere(3)
S3

In [0]:
S3.atlas()

In [0]:
S3.embedding().display()

In [0]:
g = S3.induced_metric()
g.display()

In [0]:
S3.stereographic_coordinates()


## 3d plots

In [0]:
line = parametric_plot3d([x*cos(x),x*sin(x),x], (x,0,2*pi), thickness=10)
surface = dodecahedron(mesh=True, thickness=10)
show(line + surface)

In [0]:
x, y = var('x, y')

def build_frame(t):
"""Build a single frame of animation at time t."""
e = parametric_plot3d([sin(2*x - t), sin(x + t), x],
(x, 0, 2*pi), color='red')
b = parametric_plot3d([cos(x + t), -sin(x - t), x],
(x, 0, 2*pi), color='green')
return e + b

frames = [build_frame(t) for t in (0, pi/32, pi/16, .., 2*pi)]
animate(frames, delay=5).interactive(projection='orthographic')

In [0]:
@interact
def func(k = slider(0, 10, .1)):
print("k: %s" % k)
s = (1 - sqrt(k))^2
print("s: %s" % s)

In [0]: