完整教學請參考 Sage4HS GitHub repo
https://github.com/jephianlin/outreach/tree/master/Sage4HS
(修課同學請點進連結,並先完成課前準備)
Sage4HS
課程檔案
+
-
*
/
**
或 ^
(後者在純 Python 環境不適用)sqrt
//
%
a = [1, 2, 3]
k
個元表 a[k]
list
expand
factor
simplify
p = (x+1).polynomial(QQ)
p.subs(10)
或 p(10)
sqrt(2).minpoly()
def function_name(var1, ..., var2=0, ...):
do something
return something
ZZ
QQ
RR
CC
shift + enter
factorial
factor
N
type
0
到 n-1
的列表 range(n)
sum
a = [1,2]; a.append(3)
for x in range(101):
if is_prime(x):
print(x^2)
[x^2 for x in range(1,101) if is_prime(x)]
Permutations([1,2,3], 2)
Combinations([1,2,3], 2)
p = expand((1+x)^5); p.coefficient(x^2)
binomial(n,k)
random
套件import random
a
中隨機挑出一個元素 random.choice(a)
a
到 b
之間(包含兩端點)取出一個數 random.randint(a,b)
a = {'one':1, 'two':2}
one
的值 a['one']
a.keys()
a.values()
x
w = var('w')
f(x) = x^2
或 f(x,y) = x^2 + y^2
x
代入數值 f(2)
x
和 y
代數數值 f(3,4)
lambda
運算式f = lambda k: k^2
sin
, cos
, tan
, cot
, sec
, csc
pi
I
f(x) = x^2
f.plot(xmin=-3, xmax=3,
ymin=-1, ymax=10,
color='red',
legend_label='$x^2$',
linestyle='--'
)
xmin
、xmax
ymin
、ymax
color
legend_label
linestyle
v = vector([a,b])
v.plot(start=(c,d))
k * v
v1 * v2
class fraction:
def __init__(self,a,b): ## 注意:是兩個底線_
self.numerator = a
self.denominator = b
q = fraction(a,b)
isinstance(q, fraction)
__repr__
__contains__
__add__
__sub__
__mul__
__div__
__eq__
__ne__
dir
vars
v
的範數 v.norm()
v
與向量 w
內積 v.dot_product(w)
v
與向量 w
外積 v.cross_product(w)
A = matrix([
[0, 1, 2],
[1, 2, 3],
[2, 3, 4]
])
##注意 Sage 裡列和行都是從 0 開始數
A[i,j]
A[list of rows, list of columns]
A[i,:]
A[:,j]
import numpy as np
A = np.array([
[0, 1, 2],
[1, 2, 3],
[2, 3, 4]
])
v = np.array([1,1,1])
np.dot(A, v)
np.random.rand(a,b)
np.ones_like(A)
np.sum(A)
%%timeit
A = [1,2,3]
中隨機抽一個元素 np.random.choice(A)
import matplotlib.pyplot as plt
plt.hist
np.array([1,2,3], dtype=float)
[a,b]
區間等間隔分布 k
個點 np.linspace(a,b,k)
np.set_printoptions(precision=k)
plt.scatter
plt.axis('equal')
plt.legend()
x = var('x')
oo
f(x) = x^2
或 f = x^2
x=a
代入函數 f.subs(x=a)
limit
f.derivative
f.integral
A = matrix(2, [1,2,3,4])
A.transpose()
A.inverse()
A.determinant()
A.eigenmatrix_right()
ker = A.right_kernel()
ker.basis()
identity_matrix(n)
import numpy as np
import scipy.linalg as LA
A = np.random.randn(3,3)
LA.eig(A)
LA.eigh(A)
V = [0,1,2,3,4,5]
E = [(0,1), (1,2), (1,4), (3,4), (4,5)]
g = Graph([V, E])
n
點路徑圖 graphs.PathGraph(n)
n
點圈圖 graphs.CycleGraph(n)
n
點完全圖 graphs.CompleteGraph(n)
n
點星圖 graphs.StarGraph(n)
d
維超立方體 graphs.CubeGraph(d)
graphs.PetersenGraph()
n
點,每邊以獨立機率 p
出現) graphs.RandomGNP(n,p)
n
點隨機樹 graphs.RandomTree(n)
g.vertices()
g.edges(labels=False)
g.order()
g.size()
g.degree_sequence()
g.is_connected()
g.girth()
g.adjacency_matrix()
g.laplacian_matrix()
g.add_vertex(v)
g.add_edge(v,u)
g1.disjoint_union(g2)
g.show(figsize=[2,2],
vertex_labels=False,
vertex_size=10,
vertex_colors={'red':[1,3], 'blue':[2,4], 'orange':[0]},
edge_style='--')
figsize
vertex_labels
vertex_size
vertex_colors
edge_style
graphs.CycleGraph(4)
pos = {0:(0,0), 1:(1,0), 2:(0,1), 3:(1,1)}
g.set_pos(pos)
g.show(figsize=[2,2])
pos = g.get_pos()
g.set_pos(pos)
Graph([V, E], pos=pos)