-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleMagmaCode4.mgm
153 lines (123 loc) · 2.83 KB
/
ExampleMagmaCode4.mgm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Some miscellaneous tips
a := 7; b := 16; c := 29;
IsEven(a), IsEven(b), IsEven(c);
// false true false
IsOdd(a), IsOdd(b), IsOdd(c);
// true false true
// IsSquare returns one or two values:
// a true/false, based on whether the number is a square or not
// if the number IS a square, a second value is returned:
// the positive square root.
IsSquare(a), IsSquare(b), IsSquare(c);
// false true 4 false
IsSquare(7);
// false
IsSquare(16);
// true 4
// We can save the relevant parts of the function output
b := IsSquare(16);
b;
//true
b, d := IsSquare(16);
b;
// true
d;
// 4
// If we only want the second value, can use _ to discard the first value:
_, d := IsSquare(16);
// We won't get an error if a result is not defined until we try to check the value:
_, d := IsSquare(7);
d;
// >> d;
// ^
// User error: Identifier 'd' has not been declared or assigned
procedure issquare(a)
b, d := IsSquare(a);
if (b eq true) then
print "the number ", a, " is the square of ", d;
else
print "the number ", a, " is not a square.";
end if;
end procedure;
issquare(7);
// the number 7 is not a square.
issquare(16);
// the number 16 is the square of 4
Factorization(16);
// [ <2, 4> ]
Factorization(10513225);
// [ <5, 2>, <17, 1>, <29, 1>, <853, 1> ]
Divisors(10513225);
// [ 1, 5, 17, 25, 29, 85, 145, 425, 493, 725, 853, 2465, 4265, 12325,
// 14501, 21325, 24737, 72505, 123685, 362525, 420529, 618425,
// 2102645, 10513225 ]
D := Divisors(10513225);
DS := { d: d in D | IsSquare(d) };
DS;
// { 1, 25 }
R<x> := PolynomialRing(Rationals());
p1 := (x^2 +1);
p1 in R;
// true
p2 := x^6 +1;
p1;
// x^2 + 1
p2;
// x^6 + 1
p1*p2;
// x^8 + x^6 + x^2 + 1
p3 := p1*p2;
Degree(p3);
// 8
Coefficients(p3);
// [ 1, 0, 1, 0, 0, 0, 1, 0, 1 ]
p3;
// x^8 + x^6 + x^2 + 1
LeadingCoefficient(p3);
// 1
ConstantCoefficient(p3);
// 1
Terms(p3);
// [
// 1,
// x^2,
// x^6,
// x^8
// ]
LeadingTerm(p3);
// x^8
function MultiplyByLeadingTerm(p);
LT := LeadingTerm(p);
return p*LT;
end function;
MultiplyByLeadingTerm(p1);
//x^4 + x^2
MultiplyByLeadingTerm(p3);
// x^16 + x^14 + x^10 + x^8
p3;
//x^8 + x^6 + x^2 + 1
Derivative(p3);
// 8*x^7 + 6*x^5 + 2*x
Integral(p3);
// 1/9*x^9 + 1/7*x^7 + 1/3*x^3 + x
S<x,y> := PolynomialRing(Rationals(),2);
S;
// Polynomial ring of rank 2 over Rational Field
// Order: Lexicographical
// Variables: x, y
p := x^2 * y^3;
Derivative(p);
// >> Derivative(p);
^
// Runtime error in 'Derivative': Bad argument types
// Argument types given: RngMPolElt
// Need to take PARTIAL derivative, specify which variable:
Derivative(p,x);
// 2*x*y^3
Derivative(p,y);
// 3*x^2*y^2
// Note that dividing polynomials gives a RATIONAL FUNCTION,
// not a polynomial, uses a different algebraic structure.
x*y/(x^2 +y^2);
// $.1*$.2/($.1^2 + $.2^2)
// The $.1 and $.2 get used in place of x and y in this new structure.