-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFriCAS-Numbers.input
163 lines (106 loc) · 3.41 KB
/
FriCAS-Numbers.input
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
154
155
156
157
158
159
160
161
162
163
-- ---
-- jupyter:
-- jupytext:
-- formats: ipynb,input:light
-- text_representation:
-- extension: .input
-- format_name: light
-- format_version: '1.5'
-- jupytext_version: 1.10.0
-- kernelspec:
-- display_name: FriCAS
-- language: spad
-- name: jfricas
-- ---
-- This notebook is licenced under
-- [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/).
--
-- # FriCAS Tutorial (Numbers)
--
-- ## Ralf Hemmecke <[[email protected]](mailto:[email protected])>
--
-- Sources at [Github](http://github.com/fricas/fricas-notebooks/).
)read init.input )quiet
-- ## Finite rings
-- FriCAS allows to work in finite fields.
P := PrimeField 7;
a: P := 1
(Integer has CharacteristicZero, P has CharacteristicNonZero)
-- Of course, all the field operations make sense.
a2 := a+a; a3 := a2 + a
a2 + a3 + 2
10*a2 - 5
1/a2
-- There is a simple way to work in arbitrary Galois fields.
F := FiniteField(2,3);
definingPolynomial()$F
basis()$F
-- A finite field is a cyclic group, so it has a primitive element.
f: F := primitiveElement()
[f^i for i in 0..7]
-- All the field axioms hold.
1/f
-- Also finding solutions of polynomials over finite fields works.
solve(f*y^2 + 1$F, y)
-- Similarly, one can construct and work with matrices over finite fields.
M := SquareMatrix(2, F);
m: M := matrix [[1, f],[1+f, f]]
m * transpose(m) + m
(M has Field, M has Ring, M has CommutativeRing)
-- We can have finite rings with zero divisors.
W := IntegerMod(6);
w2: W := 2
w3: W := 3
w2*w3
w2^2+w3
(W has Ring, W has noZeroDivisors)
-- The "false" that is returned by the following operation is to be
-- interpreted as: "It is not know whether `IntegerMod(7)` has no zero
-- divisors".
IntegerMod(7) has noZeroDivisors
-- By general principle, FriCAS can construct power series over finite fields.
-- ## Integers localized at a prime
-- Sometimes it might be useful to work with rational numbers
-- with the condition that no denominator is divisible by a
-- certain prime. Such structures are implemented in FriCAS
-- by the domain `IntegerLocalizedAtPrime(p)`.
-- This domain is a ring.
Z3 ==> IntegerLocalizedAtPrime 3
-- Elements are created by retracting rational numbers.
x := retract(7/4)$Z3
y := retract(1/2)$Z3
-- Of course, it must be an error, if you try to consider a rational
-- number that has a denominator that is divisible by the prime as
-- an element of the localized ring.
retractIfCan(2/27)$Z3
retract(2/27)$Z3
-- The sum $x+y$ gives $\frac{9}{4}=3^2\cdot\frac{1}{4}$.
z := x+y
-- Clearly, `Z3` is not a field, but a commutative ring
-- without zero divisors.
(Z3 has Field, Z3 has CommutativeRing, Z3 has noZeroDivisors)
-- Internally, the number is stored as a pair consisting of
-- a power of the prime together with a unit of the ring.
exponent z
unit z
-- Since `Z3` is not a field, ordinary division cannot be done
-- in `Z3`, so FriCAS extends the domain by applying the
-- `Fraction` constructor (see result type).
x/y
y/z
-- `Z3` is even a Euclidean domain, so the Euclidean algorithm
-- can be applied.
Z3 has EuclideanDomain
euclideanSize z
t := retract(27/5)$Z3
gcd(x,y), gcd(t,z)
-- In a Euclidean domain, we can do division with remainder.
divide(x,y)
divide(y,z)
divide(t,z)
-- If we know in advance that the division has a zero remainder,
-- we can use the `exquo` function and retract the result
-- into an element of `Z3`.
1 exquo x
(t exquo z)::Z3
unitNormal z