Sage 常用指令

SageMath 在线文档:https://doc.sagemath.org/html/en/reference/index.html

文章目录

  • 数论
  • 群环域
  • 多项式
  • 矩阵
  • 编码学

数论

数字

Integer(3) #3
Rational(2/6) #1/3
5*RealField(53).0 #5.00000000000000
2*ComplexField(53).0+1 #1.00000000000000 + 2.00000000000000*I
1+2j.conjugate() #1.00000000000000 - 2.00000000000000*I

进制

ZZ([1,1,1,1], base=2) #15
ZZ([1,1,1,1], base=10) #1111

ZZ(15).str(base=2) #'1111'
ZZ(15).str(base=16) #'f'
0x12f.digits(base=16,digits='零壹贰叁四五六七八九ABCDEF',padto=5) #['F', '贰', '壹', '零', '零']

次方根

QQ(4/9).nth_root(2) #2/3
RR(3).nth_root(2) #1.73205080756888

二项系数 / 阶乘

10.binomial(2) #45
10.binomial(3) #120
factorial(4) #24
factorial(3.99) #23.6415020249042

gamma(4.99) #23.6415020249042
gamma(-0.99999999999) #-9.99999917263864e10
gamma(-1) #Infinity

素数

is_prime(8) #False
is_prime_power(8) #True
prime_range(10,15) #[11, 13]
next_prime(10^15) #1000000000000037
6.coprime_integers(20) #[1, 5, 7, 11, 13, 17, 19]

prime_factors(12) #[2, 3]
factor(12) #2^2 * 3

幂次

prime_powers(1000,1050) #[1009, 1013, 1019, 1021, 1024, 1031, 1033, 1039, 1049]
is_prime_power(1024) #True
is_power_of_two(1024) #True
1024.is_perfect_power() #True

GCD / LCM

GCD(6,9) #3
LCM(4,6) #12
sage.rings.integer.GCD_list([6,9,15]) #3

逆元

4.inverse_mod(15) #4
4.inverse_mod(17) #13
4.inverse_mod(ZZ.ideal(17)) #13

雅克比符号

3.jacobi(5) #-1
3.jacobi(9) #0
3.jacobi(11) #1

欧拉函数

euler_phi(8) #4
euler_phi(10) #4
sage: euler_phi(13) #12

本原根

primitive_root(13) #2
2^6%13 #12

primitive_root(257) #3
3^128%257 #256

CRT

crt([1,2,3,4],[11,3,5,7]) #683
a = Zmod(11*3*5*7)(683)
a%11, a%3, a%5, a%7 #(1, 2, 3, 4)

群环域

整数环

Z = IntegerRing() #Integer Ring
Z(13) #13
ZZ(13) #13

数域

QQ #Rational Field
RR #Real Field with 53 bits of precision
CC #Complex Field with 53 bits of precision
QQbar #Algebraic Field

理想

ZZ.ideal(6) #Principal ideal (6) of Integer Ring
6*ZZ #Principal ideal (6) of Integer Ring
2*QQ #Principal ideal (1) of Rational Field
sqrt(2)*RR #Principal ideal (1.00000000000000) of Real Field with 53 bits of precision

商环

quotient(ZZ,7*ZZ) #Ring of integers modulo 7
Z7 = ZZ.quo(7*ZZ) #Ring of integers modulo 7
Z7.is_field() #True

RF7 = ZZ.residue_field(7) #Residue field of Integers modulo 7
RF7 == Z7 #False
RF7 == GF(7) #False

有限域

GF(7) #Finite Field of size 7
GF(7) == Z7 #False
GF(8) #Finite Field in z3 of size 2^3
GF(8, 'a')([1,1,1]) #a^2 + a + 1

分式域

ZZ.fraction_field() #Rational Field
ZZ.fraction_field() == QQ #True
QQ.fraction_field() == QQ #True

生成元

ZZ.gens() #(1,)
ZZ.order() #+Infinity
GF(8).gens() #(z3,)
GF(8).order() #8

本原多项式 / 本原元

F = GF(16,'a',modulus='primitive') #Finite Field in a of size 2^3
f = F.modulus() #x^4 + x + 1
f.is_primitive() #True
f.is_prime() #True
f.roots() #[]

F.gen() #a
pe = F.primitive_element() #a
pe^4+pe+2 #1

特征

ZZ.characteristic() #0
QQ.characteristic() #0
GF(7).characteristic() #7
GF(8).characteristic() #2

单位根

ZZ.zeta(2) #-1
GF(7).zeta(2) #6
GF(7).zeta(3) #2
GF(8).zeta(7) #z3

ZZ.zeta() #-1
GF(7).zeta() #3
GF(8).zeta() #Z3

单位(可逆元集合)

ZZ(1).is_unit() #True
GF(9)(0).is_unit() #False
Zmod(6)(4).is_unit() #False
Zmod(6)(5).is_unit() #True

CRT Basis

E = crt_basis([2,3,7]) #[21, -14, -6]

E[0]%2, E[1]%2, E[2]%2
E[0]%3, E[1]%3, E[2]%3
E[0]%7, E[1]%7, E[2]%7
#(1, 0, 0)
#(0, 1, 0)
#(0, 0, 1)

多项式

多项式环

PR1 =PolynomialRing(GF(7),['X']) #Univariate Polynomial Ring in X over Finite Field of size 7
PR2.<x,y> =PolynomialRing(GF(8)) #Multivariate Polynomial Ring in x, y over Finite Field in z3 of size 2^3

x^2 + x*y + GF(8).gen() in PR2 #True
(x^2+x*y+GF(8).gen()) * (x^3+2*y) #x^5 + x^4*y + z3*x^3

PR3.<z> = QQ[] #Univariate Polynomial Ring in z over Rational Field
PR4.<x> = Zmod(15)[] #Univariate Polynomial Ring in x over Ring of integers modulo 15

PR4.modulus() #15
PR4.fraction_field()

多项式商环

R.<x> = QQ[] #Univariate Polynomial Ring in x over Rational Field
S = R.quotient(x**3-3*x+1, 'alpha') #Univariate Quotient Polynomial Ring in alpha over Rational Field with modulus x^3 - 3*x + 1

S.degree() #3
S.gen() #alpha
S.modulus() #x^3 - 3*x + 1
S.modulus().is_irreducible() #True
S.is_field() #True

分式域

R.<t> = GF(5)[] #Univariate Polynomial Ring in t over Finite Field of size 5
R.fraction_field() #Fraction Field of Univariate Polynomial Ring in t over Finite Field of size 5

特征

R.<z> = PolynomialRing(ZZ)
S.<a> = R.quo(z - 19)
S.characteristic() #0

R.<x> = PolynomialRing(GF(9,'a'))
S = R.quotient(x^3 + 1)
S.characteristic() #3

多项式

P = PolynomialRing(GF(3),'y')
for p in P.polynomials( of_degree = 2 ): print(p) 
#

P.random_element(10)
#2*y^10 + y^9 + y^6 + y^5 + 2*y^3 + 2*y^2 + y + 2

不可约多项式

R = GF(5^3, 'a')['x'] #Univariate Polynomial Ring in x over Finite Field in a of size 5^3
f = R.irreducible_element(2) #x^2 + 3*a^2 + 2*a + 1
f.degree() #2
f.is_irreducible() #True

拉格朗日插值

R = PolynomialRing(QQ, 'x')
f = R.lagrange_polynomial([(0,1),(2,2),(3,-2),(-4,9)])
#-23/84*x^3 - 11/84*x^2 + 13/7*x + 1

分园多项式

S = PolynomialRing(FiniteField(7), 'x')
S.cyclotomic_polynomial(12) #x^4 + 6*x^2 + 1
S.cyclotomic_polynomial(1) #x + 6

矩阵

向量

v = vector(GF(7),[1,2,3,4]) #(1, 2, 3, 4)
v+v #(2, 4, 6, 1)
v*v #2

线性空间

M = MatrixSpace(ZZ, 2) #Full MatrixSpace of 2 by 2 dense matrices over Integer Ring

M2 = Mat(GF(8), 2, 3) #Full MatrixSpace of 2 by 3 dense matrices over Finite Field in z3 of size 2^3

维度

M2.dims() #(2, 3)
M2.dimension() #6
M2.ncols() #3
M2.nrows() #2

M.is_finite() #False
M2.is_finite() #True

基底

list(M.basis()) #标准正交基
#[
#[1 0]  [0 1]  [0 0]  [0 0]
#[0 0], [0 0], [1 0], [0 1]
#]

M2.ngens() #6
M2.gen(3) #第4个生成元
#[0 0 0]
#[1 0 0]

矩阵

Z = M.zero() #零矩阵
#[0 0]
#[0 0]

I = M.identity_matrix() #单位阵
I = M.one()
#[1 0]
#[0 1]

D = M.diagonal_matrix([1, 2]) #对角阵
[1 0]
[0 2]

m=M2.random_element() #随机矩阵
#[z3^2 + z3        z3         1]
#[     z3^2         1         0]

m=M2.matrix([[1,GF(8).gen(),1],[2,-1,1]]) #按行向量赋值
#[ 1 z3  1]
#[ 0  1  1]

m[0,0] #1
m[0][1] #z3

m.T #转置
#[ 1  0]
#[z3  1]
#[ 1  1]

矩阵运算

m1=M.matrix([[1,2],[3,4]])
m2=M.matrix([[1,1],[0,2]])

m1+m2 #加法
#[2 3]
#[3 6]

m1*m2 #乘法
#[ 1  5]
#[ 3 11]

阵列运算

m1*2+1
#[3 4]
#[6 9]

m1%2
#[1 0]
#[1 0]

秩 / 行列式 / 逆矩阵

m1.rank() #秩
#2

m1.det() #行列式
#-2

m1.inverse() #逆元
~m1
1/m1
#[  -2    1]
#[ 3/2 -1/2]

行空间 / 列空间

M2 #Full MatrixSpace of 2 by 3 dense matrices over Finite Field in z3 of size 2^3
M2.column_space() #Vector space of dimension 2 over Finite Field in z3 of size 2^3
M2.row_space() #Vector space of dimension 3 over Finite Field in z3 of size 2^3

Base Ring

R.<x,y> = ZZ[] #Multivariate Polynomial Ring in x, y over Integer Ring
m = matrix(R,[1,2*x,3*y,4*x*y]) #[   1   2*x   3*y  4*x*y]
m.base_ring() #Multivariate Polynomial Ring in x, y over Integer Ring

m.mod(2) #[    1    0   3*ybar    0]
parent(m.mod(3)) #Full MatrixSpace of 1 by 4 dense matrices over Quotient of Multivariate Polynomial Ring in x, y over Integer Ring by the ideal (3)

初等行变换

M=Mat(ZZ,5).diagonal_matrix([1,2,3,4,5])

M.add_multiple_of_row(2,0,-3)
#[ 1  0  0  0  0]
#[ 0  2  0  0  0]
#[-3  0  3  0  0]
#[ 0  0  0  4  0]
#[ 0  0  0  0  5]

M.rescale_row(0,7)
#[ 7  0  0  0  0]
#[ 0  2  0  0  0]
#[-3  0  3  0  0]
#[ 0  0  0  4  0]
#[ 0  0  0  0  5]

M.swap_rows(0,2)
#[-3  0  3  0  0]
#[ 0  2  0  0  0]
#[ 7  0  0  0  0]
#[ 0  0  0  4  0]
#[ 0  0  0  0  5]

乘以置换阵

#置换群
G = PermutationGroup(['(1,2,3)(4,5)', '(1,2,3,4,5)']) #Permutation Group with generators [(1,2,3)(4,5), (1,2,3,4,5)]
sigma, tau = G.gens() #(1,2,3)(4,5), (1,2,3,4,5)

M=Mat(ZZ,5).diagonal_matrix([1,2,3,4,5])
M.permute_rows(sigma)
#[0 2 0 0 0]
#[0 0 3 0 0]
#[1 0 0 0 0]
#[0 0 0 0 5]
#[0 0 0 4 0]

迹 / 范数

a = matrix(3,3,range(9))
#[0 1 2]
#[3 4 5]
#[6 7 8]

a.trace() #对角线的和,12

a.norm(p=1) #最大的每列累加值,15.0
sage: a.norm(p=2) #最大奇异值,14.226707390822694
a.norm(p=Infinity) #最大的每行累加值,21.0

特征值 / 特征向量

A = matrix(ZZ, 3, range(9))
sorted(A.eigenvalues(), reverse=True)
#[13.34846922834954?, 0, -1.348469228349535?]

#左特征向量,vA = lambda*v
el = A.eigenvectors_left()
#[(0, [(1, -2, 1)], 1),
# (-1.348469228349535?, [(1, 0.3101020514433644?, -0.3797958971132713?)], 1),
# (13.34846922834954?, [(1, 1.289897948556636?, 1.579795897113272?)], 1)]

eval, [evec], mult = el[1]
eval*evec - evec*A #(0, 0, 0)

#右特征向量,Av = lambda*v
er = A.eigenvectors_right()
#[(0, [(1, -2, 1)], 1),
# (-1.348469228349535?, [(1, 0.1303061543300932?, -0.7393876913398137?)], 1),
# (13.34846922834954?, [(1, 3.069693845669907?, 5.139387691339814?)], 1)]

eval, [evec], mult = er[2]
eval*evec - A*evec #(0, 0, 0)

共轭 / 厄米

A = matrix(QQbar, [[     -3,  5 - 3*I, 7 - 4*I],
                   [7 + 3*I, -1 + 6*I, 3 + 5*I],
                   [3 + 3*I, -3 + 6*I, 5 +   I]])

A.C
#[      -3  5 + 3*I  7 + 4*I]
#[ 7 - 3*I -1 - 6*I  3 - 5*I]
#[ 3 - 3*I -3 - 6*I  5 - 1*I]

A.H
#[      -3  7 - 3*I  3 - 3*I]
#[ 5 + 3*I -1 - 6*I -3 - 6*I]
#[ 7 + 4*I  3 - 5*I  5 - 1*I]

厄米标准型(整环上的简化行阶梯矩阵,每列枢轴最大)

K.<x> = FunctionField(GF(7))
M = K.maximal_order()
A = matrix(M, 2, 3, [x, 1, 2*x, x, 1+x, 2])
A.hermite_form()
[      x       1     2*x]
[      0       x 5*x + 2]

LU 分解

A = matrix(QQ, [[1, -1,  0,  2,  4,  7, -1],
                [2, -1,  0,  6,  4,  8, -2],
                [2,  0,  1,  4,  2,  6,  0],
                [1,  0, -1,  8, -1, -1, -3],
                [1,  1,  2, -2, -1,  1,  3]])

P, L, U = A.LU(pivot='partial')

P #置换阵
#[0 0 0 0 1]
#[1 0 0 0 0]
#[0 0 0 1 0]
#[0 0 1 0 0]
#[0 1 0 0 0]

L #下三角阵
#[   1    0    0    0    0]
#[ 1/2    1    0    0    0]
#[ 1/2  1/3    1    0    0]
#[   1  2/3  1/5    1    0]
#[ 1/2 -1/3 -2/5    0    1]

U #上三角阵
#[    2    -1     0     6     4     8    -2]
#[    0   3/2     2    -5    -3    -3     4]
#[    0     0  -5/3  20/3    -2    -4 -10/3]
#[    0     0     0     0   2/5   4/5     0]
#[    0     0     0     0   1/5   2/5     0]

P*L*U == A #True

QR 分解

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 #酉矩阵(Q.H*Q=I)
#[ -0.4588314677411235?  -0.1260506983326509?   0.3812120831224489?   -0.394573711338418?     -0.6874400625964?]
#[ -0.4588314677411235?   0.4726901187474409? -0.05198346588033394?   0.7172941251646595?     -0.2209628772631?]
#[  0.2294157338705618?   0.6617661662464172?   0.6619227988762521?  -0.1808720937375480?      0.1964114464561?]
#[  0.6882472016116853?   0.1890760474989764?  -0.2044682991293135?   0.0966302966543065?     -0.6628886317894?]
#[ -0.2294157338705618?   0.5357154679137663?   -0.609939332995919?   -0.536422031427112?      0.0245514308070?]

R #上三角矩阵(对角线非负)
#[  4.358898943540674? -0.4588314677411235?   13.07669683062202?   6.194224814505168?   2.982404540317303?]
#[                   0   1.670171752907625?  0.5987408170800917?  -1.292019657909672?   6.207996892883057?]
#[                   0                    0   5.444401659866974?   5.468660610611130? -0.6827161852283857?]
#[                   0                    0                    0   1.027626039419836?  -3.619300149686620?]
#[                   0                    0                    0
     0 0.02455143080701182?]

Q*R==A #True

编码学

奇偶校验码

C = codes.ParityCheckCode(GF(5), 7) #[8, 7] parity-check code over GF(5)
E = codes.encoders.ParityCheckCodeGeneratorMatrixEncoder(C) #Generator matrix-based encoder for [8, 7] parity-check code over GF(5)
E.generator_matrix()
#[1 0 0 0 0 0 0 4]
#[0 1 0 0 0 0 0 4]
#[0 0 1 0 0 0 0 4]
#[0 0 0 1 0 0 0 4]
#[0 0 0 0 1 0 0 4]
#[0 0 0 0 0 1 0 4]
#[0 0 0 0 0 0 1 4]

汉明码

C = codes.HammingCode(GF(7), 3) #[57, 54] Hamming Code over GF(7)
C.parity_check_matrix() #3 x 57 dense matrix over Finite Field of size 7 (use the '.str()' method to see the entries)
C.generator_matrix() #54 x 57 dense matrix over Finite Field of size 7 (use the '.str()' method to see the entries)

循环码

from sage.coding.cyclic_code import find_generator_polynomial
F = GF(16, 'a')
a = F.gen()
C = codes.CyclicCode(length=15, field=F, D=[1,2], primitive_root = a^2 + 1) #[15, 13] Cyclic Code over GF(16)
g = find_generator_polynomial(C) #x^2 + (a^2 + a + 1)*x + a^3 + a
C.defining_set() #[1, 2]
C.primitive_root() #a^2 + 1

F.<x> = GF(2)[]
g = x ** 3 + x + 1
C = codes.CyclicCode(generator_pol = g, length = 7) #[7, 4] Cyclic Code over GF(2)
C.generator_polynomial() #x^3 + x + 1
C.check_polynomial() #x^4 + x^2 + x + 1
C.bch_bound() #(3, (1, 1))

E = codes.encoders.CyclicCodePolynomialEncoder(C)
m = x ** 2 + 1
c = E.encode(m) #(1, 1, 1, 0, 0, 1, 0)

Reed-Solomon 码

F = GF(11)
Fx.<x> = F[]
n, k = 10 , 5
C = codes.GeneralizedReedSolomonCode(F.list()[:n], k) #[10, 5, 6] Reed-Solomon Code over GF(11)

E = codes.encoders.GRSEvaluationPolynomialEncoder(C) #Evaluation polynomial-style encoder for [10, 5, 6] Reed-Solomon Code over GF(11)
p = x^2 + 3*x + 10
c = E.encode(p) #(10, 3, 9, 6, 5, 6, 9, 3, 10, 8)

E = codes.encoders.GRSEvaluationVectorEncoder(C) #Evaluation vector-style encoder for [10, 5, 6] Reed-Solomon Code over GF(11)
p = vector(F,[10,3,1,0,0])
c = E.encode(p) #(10, 3, 9, 6, 5, 6, 9, 3, 10, 8)

F = GF(59)
n, k = 40, 12
C = codes.GeneralizedReedSolomonCode(F.list()[1:n+1], k) #[40, 12, 29] Reed-Solomon Code over GF(59)
D = codes.decoders.GRSKeyEquationSyndromeDecoder(C) #Key equation decoder for [40, 12, 29] Reed-Solomon Code over GF(59)
c = C.random_element()
Chan = channels.StaticErrorRateChannel(C.ambient_space(), D.decoding_radius()) #Static error rate channel creating 14 errors, of input and output space Vector space of dimension 40 over Finite Field of size 59
y = Chan(c)
c == D.decode_to_code(y) #True

BCH 码

C = codes.BCHCode(GF(2), 15, 3) #[15, 11] BCH Code over GF(2) with designed distance 3
RS = C.bch_to_grs() #[15, 13, 3] Reed-Solomon Code over GF(16)
D = codes.decoders.BCHUnderlyingGRSDecoder(C) #Decoder through the underlying GRS code of [15, 11] BCH Code over GF(2) with designed distance 3
c = (1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0)
D.decode_to_code(c) #(0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0)

Goppa 码

F = GF(2^3)
R.<x> = F[]
g = x^2 + x + 1
L = [a for a in F.list() if g(a) != 0]
C = codes.GoppaCode(g, L) #[8, 2] Goppa code over GF(2)
E = codes.encoders.GoppaCodeEncoder(C) #Encoder for [8, 2] Goppa code over GF(2)
word = vector(GF(2), (0, 1))
c = E.encode(word)

你可能感兴趣的:(python,C/C++,数学,抽象代数,Sage)