Magma是一款由悉尼大学数学与统计学系计算代数学小组开发的功能强大的代数计算程序包,该软件专门解决代数系统中的数论、代数几何和代数组合学的计算问题。结合近几天的学习,主要从以下六个方面对Magma进行简单说明。
使用说明:每个完整的命令必须以分号(;)结束,当计算机准备好让你输入命令时会出现一个提示符(>)。注意字母区分大小写!!!不能识别中文字符!!!
一、数据类型和运算
1、数据类型及其表示形式
字符串的表示
> "\"Print this line in quotes\"";//输入
"Print this line in quotes"
> "The first line,\nthe second line, and then\ran\tindented line";
The first line,
the second line, and then
an indented line
字符串的其他详细操作可以参考:
http:\\magma.maths.usyd.edu.au\magma\handbook\text\31#221
数域
整数环:IntegerRing()
有限域:GF(P)
有理数域:RationalField()
实数域:RealField()
复数域:ComplexField()
整数环上的一元多项式表示
>P<x>:= PolynomialRing(IntegerRing());//定义P为整数环上的一元多项式环,变量为x
>P;
Univariate Polynomial Ring in x over Integer Ring
>(x^2+1)*(x^2+2);//多项式运算
x^4 + 3*x^2 + 2
>Factorization(x^8 - 1);//输出x^8-1的因式
[
<x - 1, 1>,
<x + 1, 1>,
<x^2 + 1, 1>,
<x^4 + 1, 1>
]
创建集合、数组的一般形式
> t := {1,1,2,3};//用花括号定义集合,元素用逗号隔开
> q := [1,1,2,3];//用中定义数组,元素用逗号隔开
> t, q;//输出集合t,数组q
{ 1, 2, 3 }
[ 1, 1, 2, 3 ]
>q[2];//访问数组元素
1
集合、数组的特殊形式:
D={i..j by k}表示:{i,i+k,i+2k,...,j}。k=1时,可以简写为{i..j}
框架:{ expression in x : x in D | condition }
>a:=[1..10 by 2];//例子,说明不能直接用这种形式创建集合或数组
>a;
[ 1 .. 9 by 2 ]
>a:=[1:i in [1..10]];//创建一个长为10全1数组
>a;
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>a:=[i:i in [1..10]];//创建数组,元素为1,2,..,10;
>a;
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
> t := { n^2 : n in [5..13 by 3]};//例子,说明创建集合还是数组取决于最外层的括号
>t;
{ 25, 64, 121 }
>t:={ n^2: n in [5..13 by 3] | IsPrime(n)};//IsPrime(n)判断素数函数,素数为TRUE,合数为FLASE
>t;
{ 25, 121 }
>x:={<a, b, c>: a,b,c in [1..10] | a^2 + b^2 eq c^2};//用<x1,x2,x3,...,xn>创建多变量元素集合
>x;
{ <6, 8, 10>, <4, 3, 5>, <3, 4, 5>, <8, 6, 10> }
2、赋值运算符
“ :=”是赋值,不是“=”
>x:=2;//令x=2
>x;//输出x
2
> x:= x+ 1;//令x=x+1
>x;
3
>y:=3;
>x:=x+y;//令x=x+y
>x;
6
> q:= [1,1,2,3];//用:=创建数组
>q[2]:=q[3]-q[2]+q[1];//用:=修改数组
>q;//输出数组
[ 1, 2, 2, 3 ]
显示所有变量值
>x:=2;
>y:=3;
>ShowValues();//输出所有的变量值
x:
2
y:
3
3、算术运算符
加、减、乘、除、指数
>x:=4;
>x+2;//加法,用“+”
6
>x-3;//减法,用“-”
1
>x*4;//乘法,用“*”
16
>x div 2,x/2;//除法,用“div”或“/”
2 2
>x^4;//指数幂,用“^”
256
4、关系运算符
= | ≠ | < | ≤ | > | ≥ |
eq | ne | lt | le | gt | ge |
>x:=1;
>y:=3;
>x eq y;//等于判断,是返回true,否返回false,下同
>x ne y;//不等于
>x lt y;//小于
>x le y;//小于等于
>x gt y;//大于
>x ge y;//大于等于,下面输出的是判断值,即true或false
false
true
true
true
false
false
5、逻辑运算符
非 | 且 | 或 | 异或 |
not | and | or | xor |
>x:=1;
>y:=3;
>not ( x eq y );//not后面是true则返回false;是false则返回true
>x eq y and x lt y;//and前后相同(都是true或都是false)才返回true,前后不同返回false
>x eq y or x gt y;//or前后都是false才返回false,其他情况都返回true
>x eq y xor x lt y;//xor前后相同(都是true或都是false)才返回false,前后不同返回true
true
false
false
true
二、矩阵
1、创建矩阵
使用Matrix(R, m, n, Q)function创建,主要包含三种结构:
Matrix(R, m, n, Q) : Rng, RngIntElt, RngIntElt, [ RngElt ] -> Mtrx
Matrix(R, m, n, Q) : Rng, RngIntElt, RngIntElt, [ <RngIntElt, RngIntElt, RngElt> ] -> Mtrx
Matrix(R, m, n, Q) : Rng, RngIntElt, RngIntElt, [ [ RngElt ] ] -> Mtrx
下面举例说明:
(a) Defining a matrix over Z:
> X := Matrix(IntegerRing(), 2, 2, [1,2, 3,4]);
//输入四个量,第一个Rng表示矩阵元素所在的环或域,
//第二个m是矩阵行数,第三个n是矩阵列数,
//第四个Q是把矩阵全部元素按行展开写成数组形式
> X;//输出矩阵
[1 2]
[3 4]
> Parent(X);//Parent函数返回矩阵属性
Full Matrix Algebra of degree 2 over Integer Ring
(b) Defining a matrix over GF(P):
> X := Matrix(GF(7), 2, 3, [1,-2,3,4,6,9]);
> X;
[1 5 3]
[4 6 2]
> Parent(X);
Full KMatrixSpace of 2 by 3 matrices over GF(7)
(c) Defining a sparse matrix over Q:
>X:= Matrix(RationalField(),5,10,[<1,2,23>,<3,7,11>,<5,10,-1>]);
//<1,2,23>表示第一行第二列的元素值为23
> X;
[ 0 23 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 11 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 -1]
> Parent(X);
Full KMatrixSpace of 5 by 10 matrices over Rational Field
(d) Defining a sparse matrix over GF(P):
> X := Matrix(GF(11), 5, 5, [<2*i-1, 2*j-1, i*j>: i, j in [1..3]]);
> X;
> Parent(X);
[ 1 0 2 0 3]
[ 0 0 0 0 0]
[ 2 0 4 0 6]
[ 0 0 0 0 0]
[ 3 0 6 0 9]
Full Matrix Algebra of degree 5 over GF(11)
(e) Defining a matrix over GF(p^n):
> X:= Matrix(GF(8),2,3,[1,-2,3,4,6,9]);//例子,说明不能直接用数值来创建扩域上的矩阵
> X;
[ 1 0 1]
[ 0 0 1]
> K<w>:=GF(8);//定义w为GF(8)的本原元
> X:=Matrix(K,2,3,[1,w,w^3+w+1,w^2,w^3,w^7]);//数组元素可以为0,1,关于w的多项式
> X;
[ 1 w 0]
[w^2 w^3 1]
> Parent(X);
Full KMatrixSpace of 2 by 3 matrices over GF(2^3)
简化形式:
创建一般矩阵
Matrix(R, Q) : Rng, [ [ RngElt ] ] -> Mtrx
Matrix(R, n, Q) : Rng, RngIntElt, [ RngElt ] -> Mtrx
实数域上创建矩阵
Matrix(m, n, Q) : RngIntElt, RngIntElt, [ RngElt ] -> Mtrx
Matrix(m, n, Q) : RngIntElt, RngIntElt, [ [ RngElt ] ] -> Mtrx
Matrix(n, Q) : RngIntElt, [ RngElt ] -> Mtrx
Matrix(Q) : [ [ RngElt ] ] -> Mtrx
Matrix(Q) : [ Mtrx ] -> Mtrx
代码示例1:
> X:=Matrix(2,[10,2,3,4]);//不写所在环或域,则根据元素特点识别为整数环或实数域等
> X;
[10 2]
[ 3 4]
>Parent(X);
Full Matrix Algebra of degree 2 over Integer Ring//元素全为整数,识别为整数环
> X:=Matrix([[1.5,2], [3,4]]);
> X;
[1.50000000000000000000000000000 2.00000000000000000000000000000]
[3.00000000000000000000000000000 4.00000000000000000000000000000]
> Parent(X);
Full Matrix Algebra of degree 2 over Real field of precision 30//含有浮点数,识别为整数环
代码示例2:
> X:=Matrix(GF(23),3, [1,-2,3, 4,100,-6]);//行可以省略不写,规定列数,所以数组长度必须为3的倍数
> X;
[ 1 21 3]
[ 4 8 17]
> X := Matrix(GF(23), [[1,-2,3],[4,100,-6]]);
//不写行列数,直接在数组中把矩阵每一行都以数组形式写出
//这要求数组的元素也要都写出数组,而且每个数组长度相等
> X;
[ 1 21 3]
[ 4 8 17]
> X := Matrix([[GF(23)|1,-2,3], [4,100,-6]]);把元素所在的环或域卸载数组里面,是上一种的变化形式
> X;
[ 1 21 3]
[ 4 8 17]
特殊矩阵的创建
全0矩阵: ZeroMatrix(R, m, n)
标量矩阵: ScalarMatrix(R, n, s)
单位矩阵: ScalarMatrix(R,n,1)
对角矩阵: DiagonalMatrix(R, n, Q)
下三角矩阵: LowerTriangularMatrix(Q)
上三角矩阵: UpperTriangularMatrix(R, Q)
对称矩阵: SymmetricMatrix(R, Q)
反对称矩阵: AntisymmetricMatrix(R,Q)
置换矩阵: PermutationMatrix(R,Q)
随机矩阵创建
随机矩阵: RandomMatrix(R, m, n)
随机单模矩阵: RandomUnimodularMatrix(n, M)
随机辛矩阵: RandomSymplecticMatrix(g, m)
创建向量
x:=Vector(R, n, Q)//R代表环或域,n是向量长度,Q是数组
其中R,n均可以省略,不写R会自动识别为整数环或者实数域等
2、矩阵操作
(1)单元素访问:
A[i, j]
(2)行访问:
第i行: A[i]
序列Q对应的行:A[Q]
连续行: A[i..j]
(3)矩阵修改:
元素:A[i, j] := x
行:A[i]:= v
代码演示:
> X:=Matrix(4, [1,2,3,4,5,4,3,2,1,2,3,4]);//创建四阶整数环矩阵
> X;//输出矩阵
[1 2 3 4]
[5 4 3 2]
[1 2 3 4]
> X[1];//输出矩阵第一行
(1 2 3 4)
> X[1,2];//输出矩阵第1行第2列元素
2
> X[1,2]:=23;//修改矩阵第1行第2列元素
> X;
[ 1 23 3 4]
[ 5 4 3 2]
[ 1 2 3 4]
> X[3]:=Vector([9,8,7,6]);//修改矩阵第3行,必须用向量赋值
> X[2]:=0;//修改矩阵第2行,全0向量可以用0代替
> X;
[ 1 23 3 4]
[ 0 0 0 0]
[ 9 8 7 6]
(4)子矩阵提取:
Submatrix(A,i,j,p,q)//提取A(i,j)和A(i+p-1,j+q-1)围成的矩阵
SubmatrixRange(A,i,j,r,s)//提取A(i,j)和A(r,s)围成的矩阵
Submatrix(A,I,J)//提取序列I对应的行和序列J对应的列所形成的矩阵
RowSubmatrix(A,i,k)//提取第i行和第(i+k-1)围成的矩阵
RowSubmatrix(A,I)//提取序列I对应行形成的矩阵
RowSubmatrixRange(A,i,j)//提取第i行和第j行围成的矩阵
ColumnSubmatrix(A,i,k)
ColumnSubmatrix(A,J)
ColumnSubmatrixRange(A,i,j)
(5)矩阵插入
InsertBlock(A,B,i,j) : Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx
InsertBlock(~A,B,i,j) : Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx
代码演示:
> A := Matrix(6,
> [ 9, 1, 7, -3, 2, -1,
> 3, -4, -5, 9, 2, 7,
> 7, 1, 0, 1, 8, 22,
> -3, 3, 3, 8, 8, 37,
> -9, 0, 7, -1, 2, 3,
> 7, 2, -2, 4, 3, 47 ]);
> A;
[ 9 1 7 -3 2 -1]
[ 3 -4 -5 9 2 7]
[ 7 1 0 1 8 22]
[-3 3 3 8 8 37]
[-9 0 7 -1 2 3]
[ 7 2 -2 4 3 47]
> Submatrix(A,2,2,3,3);
[-4 -5 9]
[ 1 0 1]
[ 3 3 8]
> SubmatrixRange(A,2,2,3,3);
[-4 -5]
[ 1 0]
> S:=$1;//$1上面最后输出的一个矩阵,同理$2表示倒数第2个输出的矩阵
> InsertBlock(~A,S,5,5);//把矩阵S插入在A的第5行第五列
> A;
[ 9 1 7 -3 2 -1]
[ 3 -4 -5 9 2 7]
[ 7 1 0 1 8 22]
[-3 3 3 8 8 37]
[-9 0 7 -1 -4 -5]
[ 7 2 -2 4 1 0]
> RowSubmatrix(A,5,2);
[-9 0 7 -1 -4 -5]
[ 7 2 -2 4 1 0]
> RowSubmatrixRange(A,2,3);
[ 3 -4 -5 9 2 7]
[ 7 1 0 1 8 22]
> RowSubmatrix(A,2,0);
Matrix with 0 rows and 6 columns
(5)行列操作
交换型
SwapRows(A, i, j):交换i,j行,赋值操作,不改变A
SwapRows(~A, i, j):交换i,j行,直接改变A
SwapColumns(A, i, j)
SwapColumns(~A, i, j)
逆序排列
ReverseRows(A)
ReverseRows(~A)
ReverseColumns(A)
ReverseColumns(~A)
以一个数c乘矩阵的第 i 行(列)所有元素
MultiplyRow(A,c,i)
MultiplyRow(~A,c,i)
MultiplyColumn(A,c,i)
MultiplyColumn(~A,c,i)
把矩阵的第 i 行(列)所有元素乘以一个数c后加到第 j 行(列)对应的元素
AddRow(A,c,i,j)
AddRow(~A,c,i,j)
AddColumn(A,c,i,j)
AddColumn(~A,c,i,j)
代码演示:
> A := Matrix(5, 6,
> [ 3, 1, 0, -4, 2, -12,
> 2, -4, -5, 5, 23, 6,
> 8, 0, 0, 1, 5, 12,
> -2, -6, 3, 8, 9, 17,
> 11, 12, -6, 4, 2, 27 ]);
> A;
[ 3 1 0 -4 2 -12]
[ 2 -4 -5 5 23 6]
[ 8 0 0 1 5 12]
[ -2 -6 3 8 9 17]
[ 11 12 -6 4 2 27]
> SwapColumns(~A, 1, 2);
> A;
[ 1 3 0 -4 2 -12]
[ -4 2 -5 5 23 6]
[ 0 8 0 1 5 12]
[ -6 -2 3 8 9 17]
[ 12 11 -6 4 2 27]
> AddRow(~A, 4, 1, 2);
> AddRow(~A, 6, 1, 4);
> AddRow(~A, -12, 1, 5);
> A;
[ 1 3 0 -4 2 -12]
[ 0 14 -5 -11 31 -42]
[ 0 8 0 1 5 12]
[ 0 16 3 -16 21 -55]
[ 0 -25 -6 52 -22 171]
详细介绍及其他操作可以参考:
http:\\magma.maths.usyd.edu.au\magma\handbook\text\269#2504
3、分块矩阵
水平合并: HorizontalJoin(X,Y)
竖直合并: VerticalJoin(X,Y)
主对角线合并: DiagonalJoin(X,Y)
分块矩阵构造: BlockMatrix(m,n,block)
Kronecker积: KroneckerProduct(A,B)
4、矩阵运算
转置: Transpose(A)
加法: A+B
减法: A-B
数乘: x*A或者A*x
乘法: A*B
-1*A: -A
逆: A^(-1)
幂: A^(n)
A + s.B: AddScaledMatrix(A, s, B)
A := A + s*B: AddScaledMatrix(~A, s, B)
5、矩阵属性
秩: rank(A)
迹: Trace(A)
Trace(A*B): TraceOfProduct(A,B)//结果和Trace(A*B)一样,但是效率更高
特征值: Eigenvalues(G)
实数或复数域上的特征值: NumericalEigenvalues(M)
特征多项式: CharacteristicPolynomial(G)
核: Kernel(A)或Nullspace(A)
核的基矩阵: KernelMatrix(A)或NullspaceMatrix(A)
6、行列式
方形矩阵的行列式: Determinant(A)
方子矩阵的行列式: Minor(M,I,J)//行数组I和列数组J构成的方子矩阵
所有r阶方子矩阵的行列式: Minors(M,r)
Mij的余子式: Minor(M,i,j)
Mij代数余子式: Cofactor(M,i,j)
矩阵的所有代数余子式: Cofactors(M)
7、矩阵属性测试
全0矩阵测试: IsZero(A)//很有用
单位矩阵测试: IsOne(A)
负单位阵检测: IsMinusOne(A)
标量矩阵测试: IsScalar(A)
对称矩阵测试: IsSymmetric(A)
上三角矩阵检测: IsUpperTriangular(A)
下三角矩阵检测: IsLowerTriangular(A)
奇异阵检测: IsSingular(A)//很有用
代码示例:
>G:=Matrix(3,[1,1,1,2, 3,2,1,4,11]);
>Determinant(G);
10
>Minor(G,[1,2],[2,3]);
-1
>Minors(G,2);
[ 25, 7, -1, 20, 10, 0, 5, 3, 1 ]
> IsSingular(G);
false
8、综合实例
在有限域GF(8)创建一个方形矩阵,并判断其是否可逆,输出逆
>K<w>:=GF(8);//设定本原元
>G:=Matrix(K,2,[1,w^2,w^3,w^4]);//创建矩阵
>if Determinant(G) eq 0 then//条件语句,进行判断,也可以用IsSingular(G)代替
>"Irreversible matrix";//不可逆就输出“Irreversible matrix”
>else
> G^(-1);//可逆就输出逆
>end if;//条件语句一定要以“end if”结束
[w^4 w^2]
[w^3 1]
创建一个系统生成矩阵G并求出校验矩阵H,判断是否满足GH'=0,HG'=0。
>I:=ScalarMatrix(GF(11),3,1);//创建3阶标量矩阵,取1则为单位矩阵
>A:=Matrix(GF(11),3,[1,1,1,1,2,3,1,4,9]);//创建一个范德蒙矩阵
>G:=HorizontalJoin(I,A);//水平合并I和A,即系统生成矩阵
>H:=HorizontalJoin(-(Transpose(A)),I);//创建典型校验矩阵
//下面是一系列输出语句
>"System generation matrix:";
>G;
>"System check matrix:";
>H;
>"All zero matrix detection:";
>IsZero(G*Transpose(H));//全0矩阵检测,下同
>IsZero(H*Transpose(G));
System generation matrix:
[ 1 0 0 1 1 1]
[ 0 1 0 1 2 3]
[ 0 0 1 1 4 9]
System check matrix:
[10 10 10 1 0 0]
[10 9 7 0 1 0]
[10 8 2 0 0 1]
All zero matrix detection:
true
true
使用GF(8)的本原元w创建由两个范德蒙矩阵构成的MDS码
>K<w>:=GF(8);
>k:=4;
>n:=8;
>X:=ZeroMatrix(K,k,n);//矩阵初始化
//下面是修改矩阵,创建4x8的范德蒙矩阵
>X[1]:=Vector(K,[1:i in [1..n]]);
>X[2]:=Vector(K,[w^j:j in [1..n]]);
>for i in [3..k] do
> X[i]:=Vector(K,[X[2,j]^(i-1) : j in [1..n]]);
>end for;
>X;
[ 1 1 1 1 1 1 1 1]
[ w w^2 w^3 w^4 w^5 w^6 w^7 w^8]
[ w^2 w^4 w^6 w^8 w^10 w^12 w^14 w]
[ w^3 w^6 w^9 w^12 1 w^3 w^6 w^9]
>C:=LinearCode(X);//把X作为生成矩阵创建线性码
>C;
[8, 4, 5] Linear Code over GF(2^4)
Generator matrix:
[ 1 0 0 0 w^6 w^3 w^5 w^4]
[ 0 1 0 0 1 w^4 1 w^7]
[ 0 0 1 0 w^4 w^4 w^7 w^8]
[ 0 0 0 1 w^12 w^14 w^13 w^6]
三、基本结构
1、顺序结构:按照解决问题的顺序写出相应的语句,执行顺序是自上而下,依次执行。
2、选择结构
以判断三条边是否能组成三角形为例
> a:=6; b:=8; c:=10;
> if ((a+b) gt c) and ((b+c) gt a) and ((c+a) gt b) then//选择判断语句,下面是执行语句
> s := (a+b+c)/2;
> "Area is", Sqrt(s*(s-a)*(s-b)*(s-c)), "square units.";
> else
> "Triangle is degenerate.";
> end if;//以end if结束
Area is 24.00000000000000000000000000 square units.
可以看出二分支结构的整体框架为:
>if +布尔类型表达式 +then
>真值执行语句1;
>真值执行语句...;
>else
>假值执行语句1;
>假值执行语句...;
>end if;
单分支形式:if... then+...;endif;
多分支的形式为:if ...then...;+elif ...then...;+else...;endif;
局部多分支:if ...then...;+elif ...then...;endif;
3、循环结构
for循环
//对1—100求和
>sum:=0;//和初始化
>for num in [1..100] do//循环起始语句,存储计数变量的取值集合
>sum +:=num;//循环执行语句
>end for;//以end for结束
>sum;
5050
while循环
>sum:=0;
>num:=1;
>while num le 100 do//循环起始语句,含有计数变量的临界值
>sum +:=num;//执行语句
>num:=num+1;//计数变量改变
>end while;//以end while结束
>sum;
5050
repeat 循环
>sum:=0;
>num:=1;
>repeat //循环起始语句,只有一个repeat
>sum +:=num;//执行语句
>num:=num+1;//计数变量改变
> until num gt 100;//循环结束判断语句,含有计数变量的临界值
>sum;
5050
打破循环语句
> p:=25;
> for x in [0 .. 100] do
> for y in [1 .. 100] do
> if x^2 + y^2 eq p then
> x, y;
> break;//break打破内层循环
> end if;
> end for;
> end for;
0 5
3 4
4 3
注:嵌套循环和其他的编程语言类似,不再赘述。此外,三种结构可以相互包含,结合使用。
循环实例,创建一个范德蒙矩阵并检验其是否存在奇异方子矩阵
>n:=5;//定义矩阵阶数
>P:=11;//定义有限域特征
>v:=Vector(GF(P),[i:i in [1..n]]);//创建全1向量v
> X:=ZeroMatrix(GF(P),n);//初始化为n阶全0矩阵
>for i in [1..n] do
>X[1,i]:=1;//循环语句,令矩阵第1行为全1
>end for;
>X[2]:=v;//令第2行为向量v
>for i in [3..n] do
> for j in [1..n] do
> X[i,j]:=X[2,j]^(i-1);//循环语句,按照范德蒙矩阵定义修改
> end for;
>end for;
>X;
[ 1 1 1 1 1]
[ 1 2 3 4 5]
[ 1 4 9 5 3]
[ 1 8 5 9 4]
[ 1 5 4 3 9]
//下面的一些语句可用于n阶矩阵X是否存在奇异子矩阵
>flag:=0;
>for r in [1..n] do
> if &*(Minors(X,r)) eq 0 then//判断所有的r阶子式中是否存在奇异矩阵
> "singular square submatrix exists";
> flag:=1;
> break;
> end if;
>end for;
>if flag eq 0 then
> "Singular square submatrix does not exist";
>end if;
singular square submatrix exists
四、函数
1、自定义函数
返回类型
> f := func< n, q |n^2+q^2>;//函数表达式类型,类似matlab的匿名函数
> y:=f(3,4);//调用函数
> y;
15
> f1 := func< a,n | &+[i: i in [a..n]] >;//多元函数表达式类型
> sum:=f1(1,100);//调用函数
> sum;
5050
> wff:= function(x,y)
> sum:=0;
> for i in[x..y] do
> sum+:=i;
> end for;
> return(sum);//函数返回值
> end function;
> sum:=wff(1,100);//调用函数
> sum;
5050
不返回类型
> fun2:=procedure(c,n)//使用procedure创建函数,没有返回值
> print c^n;
> end procedure;//函数结束语句
> fun2("#", 10);//调用函数
##########
> fun1 := procedure(~p,a,n)//“~p”表示p是实参
> for num in [a..n] do
> p+:=num;
> end for;
> end procedure;//函数结束语句
> sum:=0;
> fun1(~sum,1,100);//调用函数
> sum;
5050
2、Magma内嵌函数举例:阶乘、最大公约数、素数判断、累加、累乘、数组长度
> Factorial(18);//阶乘
6402373705728000
> GCD(15130, 3162);//最大公约数
34
> IsPrime(357);//素数判断
false
> p:=[i:i in [1..100]];
> a:=&+p;//对数组p求和
> a;
5050
> p:=[i:i in [1..18]];
> a:=&*p;//对数组p累乘
> a;
6402373705728000
> p:=[1,2,3];
> #p;//求数组p的长度
3
函数使用帮助
> ?Factorial;//查找Factorial函数的使用说明
4 matches:
1 I /magma/combinatorial-geometrical-incidence/enum-comb/\
combinatorics/Factorial
2 I /magma/ring-field-algebra/integer/combinatorics/Factorial
3 I /magma/ring-field-algebra/integer/combinatorics/IsFactorial
4 I /magma/ring-field-algebra/quantum_groups/gauss/GaussianFactorial
To view an entry, type ? followed by the number next to it
> ?2
===============================================================================
PATH: /magma/ring-field-algebra/integer/combinatorics/Factorial
KIND: Intrinsic
===============================================================================
Factorial(n) : RngIntElt -> RngIntElt
The factorial n! for positive small integer n.
===============================================================================
五、编码
1、一般线性码的构造
> K:=GF(5);//设定有限域
//-------------------------第1种方式
> C:=LinearCode<K,4|[1,1,1,2],[3,2,1,4]>;//创建线性码,需要输入有限域,码长,生成矩阵
> C;//输出码C,会展示[n,k,d]以及系统生成矩阵
[4, 2, 2] Linear Code over GF(5)
Generator matrix:
[1 0 4 0]
[0 1 2 2]
//-------------------------第2种方式
> G:=Matrix(K,2,4,[1,1,1,2,3,2,1,4]);//创建生成矩阵
> G;
[1 1 1 2]
[3 2 1 4]
> C:=LinearCode(G);//由生成矩阵创建线性码C
> C;
[4, 2, 2] Linear Code over GF(5)
Generator matrix:
[1 0 4 0]
[0 1 2 2]
补充一种创建矩阵的形式
> K:=GF(5);//设定矩阵环或域
> M:=KMatrixSpace(K,2,4);//设定生成矩阵的有限域、行数、列数
> G:=M![1,1,1,2,3,2,1,4];//创建矩阵
> G;
[1 1 1 2]
[3 2 1 4]
生成RS码
> K<w>:=GF(8);//设定本原元
> C:=ReedSolomonCode(K,5);//设置参数K,d,生成RS码满足[q-1,k,q-k]即[q-1,q-d,d]
> C;
[7, 3, 5] "BCH code (d = 5, b = 1)" Linear Code over GF(2^3)
//GF(q)(q≠2)上,码长N=q-1的本原BCH码称为RS码
Generator matrix:
[ 1 0 0 w^3 w 1 w^3]
[ 0 1 0 w^6 w^6 1 w^2]
[ 0 0 1 w^5 w^4 1 w^4]
//-----------------------------------
> C:=ReedSolomonCode(7,5);//设定参数n,d,要求q=n+1为素数,生成RS码
> C;
[7, 3, 5] "BCH code (d = 5, b = 1)" Linear Code over GF(2^3)
Generator matrix:
[ 1 0 0 $.1^3 $.1 1 $.1^3]
[ 0 1 0 $.1^6 $.1^6 1 $.1^2]
[ 0 0 1 $.1^5 $.1^4 1 $.1^4]
//-----------------------------------生成广义RS码
> q:=2^3;
> K<w>:= GF(q);
> A := [w ^ i : i in [0 .. q - 2]];//设定广义RS码变量取值,码长
> V := [K ! 1 : i in [0 .. q - 2]];//设定系数
> k := 3;
> C := GRSCode(A, V, k);//输入三个参数
> C;
[7, 3, 5] "GRS code" Linear Code over GF(2^3)
Generator matrix:
[ 1 0 0 w^3 w 1 w^3]
[ 0 1 0 w^6 w^6 1 w^2]
[ 0 0 1 w^5 w^4 1 w^4]
生成MDS码
> K<w>:=GF(8);//设定有限域本原元
> C:=MDSCode(K,3);//MDSCode函数默认生成[q+1,3,q-1]MDS码
> C;
[9, 3, 7] Cyclic Linear Code over GF(2^3)
Generator matrix:
[ 1 0 0 1 w^3 w^4 w^6 w^4 w^3]
[ 0 1 0 w^3 w^2 w w w^2 w^3]
[ 0 0 1 w^3 w^4 w^6 w^4 w^3 1]
生成循环码
> P<x>:=PolynomialRing(GF(2));//设定多项式环
> F:=Factorization(x^7-1);//Factorization函数进行多项式因式分解
> F;
[
<x + 1, 1>,
<x^3 + x + 1, 1>,
<x^3 + x^2 + 1, 1>
]
> C:=CyclicCode(7,F[2][1]);//创建循环码
> C;
[7, 4, 3] Cyclic Linear Code over GF(2)
Generator matrix:
[1 0 0 0 1 1 0]
[0 1 0 0 0 1 1]
[0 0 1 0 1 1 1]
[0 0 0 1 1 0 1]
生成汉明码
> H:=HammingCode(GF(2),3); //设定参数,生成汉明码
> H;
[7,4,3] "Hamming code (r=3)" Linear Code over GF(2)
Generator matrix:
[1 0 0 0 1 1 0]
[0 1 0 0 0 1 1]
[0 0 1 0 1 1 1]
[0 0 0 1 1 0 1]
创建其他类型的线性码可以参考:
http:\\magma.maths.usyd.edu.au\magma\handbook\text\1910
2、码的属性
码长: Length(C)
码的维数: Dimension(C)或NumberOfGenerators(C)
码字总数: #C
码的信息率: InformationRate(C)
码的生成矩阵: GeneratorMatrix(C)或BasisMatrix(C)
对偶码: Dual(C)
奇偶校验矩阵: ParityCheckMatrix(C)
码和对偶的交集: Hull(C)
3、码字的构建及运算
码字的构建: C![a1, ...,an]或[a1, ..., ak]*G
零码字: C!0
随机码字: Random(C)
加法: u+v
减法: u-v
逆码字: -u
数乘: a*u
乘法: u*v
//下面几个函数都很有用
码字距离: Distance(u, v)
码字权重: Weight(u)
码字内积: InnerProduct(u, v):
最小权重: MinimumWeight(C)
最小距离: MinimumDistance(C)
4、码字的逻辑运算符
等于 | 不等于 | 全0 |
u eq v | u ne v | IsZero(u) |
5、码判断
循环码: IsCyclic(C)
自对偶码: IsSelfDual(C)
自正交码: IsSelfOrthogonal(C)
MDS码: IsMDS(C)
等距离码: IsEquidistant(C)
完全码: IsPerfect(C)
拟完备码: IsNearlyPerfect(C)
偶数码: IsEven(C)
双偶码: IsDoublyEven(C)
射影码: IsProjective(C)
等效判断: IsEquivalent(C1,C2)
6、综合实例
求所有的码字权重和距离
> M := KMatrixSpace(FiniteField(5), 2, 4);
> G := M ! [1,1,1,2, 3,2,1,4];//创建生成矩阵
> C := LinearCode(G);//创建线性码
> {Distance(v,w):v,w in C|v ne w};//求任意两个码字汉明距离
{ 2, 3, 4 }
> {Weight(v):v in C|v ne 0};//求任意非0码字汉明重量
{ 2, 3, 4 }
判断生成的码以及共轭码是否MDS
>q:=3^2;
>k:=3;//设定MDS参数
>K<w>:= GF(q^2);//设定本原元
>C:=MDSCode(K,k);//生成MDS码
>G:=GeneratorMatrix(C);//求生成矩阵
>G2:=Matrix(K,q^2+1,[G[i,j]^q:j in [1..q^2+1],i in [1..3]]);//创建共轭生成矩阵
>C2:=LinearCode(G2);//生成线性码
>IsMDS(C);//判断码C是否MDS码
true
>IsMDS(C2);//判断共轭码C2是否MDS码
true
判断码是否LCD码,自对偶码和自正交码
> M := KMatrixSpace(FiniteField(5), 2, 4);
> G := M ! [1,1,1,2, 3,2,1,4];//创建生成矩阵
> C := LinearCode(G);//创建线性码
//--------------------第1种方法根据码C判断
> if #Hull(C) eq 1 then//求C和其对偶交集,判断交集码字数量是否为1
> "LCD true";
> else
> "LCD false";
> end if;
LCD true
> if IsSelfOrthogonal(C) then//调用IsSelfOrthogonal(C)自正交码判断函数
> "SelfOrthogonal code true";
> else
> "SelfOrthogonal code false";
> end if;
SelfOrthogonal code false
> if IsSelfDual(C) then///调用 IsSelfDual(C)自对偶码判断函数
> "SelfDual code true";
> else
> "SelfDual code false";
> end if;
SelfDual code false
//--------------------第2种方法,根据生成矩阵判断
> if IsSingular(G*Transpose(G)) then//LCD,判断生成矩阵和转置的乘积是否奇异
> "LCD flase";
> else
> "LCD true";
> end if;
LCD true
> if IsZero(G*Transpose(G)) then//自正交码,判断生成矩阵和转置的乘积是否全0
> "SelfOrthogonal code true";
> else
> "SelfOrthogonal code false";
> end if;
SelfOrthogonal code false
已知[n,k,d]码C的系统生成矩阵[I,P],判断是否存在α,使得[I,αP]可以生成相同参数的LCD
> q:=9;
> k:=3;
> n:=6;
> K<w>:=GF(q);
> G:=Matrix(K,k,n,[[1:i in[1..n]],[w^i:i in[0..n-1]],[w^(2*i):i in[0..(2*n-2) by 2]]]);
> C:=LinearCode(G);//创建线性码
> G:=GeneratorMatrix(C);//获取系统生成矩阵
> P:=ColumnSubmatrix(G,[(k+1)..n]);//提取系统生成矩阵的P部分
> a:=Eigenvalues(P*Transpose(P));//获取特征值
> a:=[i:i in a];//将特征值集合改为数组
> N:=#a;//求特征值数量
> flag:=0;//计数变量初始化,记录不符合要求的α数量
> for i in[1..N] do//二重循环用于寻找所有不符合要求的α
> if a[i][1] ne 0 then
> for j in [w^t: t in[0..q-2]] do
> if -1/(j^2) eq a[i][1] then
> flag+:=1;
> end if;
> end for;
> end if;
> end for;
> (q-1)-flag;//域q非零元素数目减去不符合要求元素的数目
2
7、码字操作
> PunctureCode(C,S): 把码C中每个码字与集合S对应的码位删除,形成新的码
> PadCode(C,n): 在每个码字后面加n个0
> CodeComplement(C,C1):Given a subcode C1 of C,return a code C2 such that C=C1+C2.
示例:
> K<w>:=GF(4);
> G := Matrix(K,2,4,[1,1,1,1,w^3,w^2,1,w]);//创建生成矩阵
> C := LinearCode(G);//创建线性码
> C;
[4, 2, 2] Linear Code over GF(2^2)
Generator matrix:
[ 1 0 1 w^2]
[ 0 1 0 w]
> C1:=PunctureCode(C,1);//删除码字第1位,相当于删除生成矩阵第1列
> C1;
[3, 2, 2] Cyclic Linear Code over GF(2^2)
Generator matrix:
[ 1 0 w]
[ 0 1 w^2]
C2:=PadCode(C,2);//码字末尾加2个0,相当于生成矩阵最后加两列0
C2;
[6, 2, 2] Linear Code over GF(2^2)
Generator matrix:
[ 1 0 1 w^2 0 0]
[ 0 1 0 w 0 0]
其他操作参考:
http:\\magma.maths.usyd.edu.au\magma\handbook\text\1911#21633
六、输出,读入和写入
1、输出示例
> 2+4;//表达式,直接输出结果
6
>print 2+4;//print输出
6
> "Number of permutations of five objects is", Factorial(5);
Number of permutations of five objects is 120
> "\"Print this line in quotes\"";//输出字符串
"Print this line in quotes"
2、读入
load "filename"//读取文件名
Read(name)
ReadBinary(name)
3、写入
IOType(I)
Open(name, mode)
很多操作在Windows平台上不能使用,不再叙述,详细介绍可以参考:
http:\\magma.maths.usyd.edu.au\magma\handbook\input_and_output
附:
Magma官网:
http://magma.maths.usyd.edu.au/
Magma在线手册:
http:\\magma.maths.usyd.edu.au\magma\handbook"
First Steps in Magma:
http://magma.maths.usyd.edu.au/magma/pdf/first.pdf
Solving Problems with Magma: