|
3 | 3 | exec julia --project="$(realpath $(dirname $(dirname $0)))" --color=yes --startup-file=no -e "include(popfirst!(ARGS))" \
|
4 | 4 | "${BASH_SOURCE[0]}" "$@"
|
5 | 5 | =#
|
6 |
| - |
7 |
| -include(joinpath(dirname(@__FILE__), "utils.jl")) |
8 |
| -include(joinpath(dirname(@__FILE__), "rref.jl")) |
9 | 6 |
|
10 |
| -using LinearAlgebra: I |
| 7 | +""" |
| 8 | + struct FinitePolynomial <: FiniteField |
| 9 | + |
| 10 | +Has parameters `p`, which is an abstract polynomial, and `n` which is the modulus of the field under which the molynomial is defined. |
| 11 | +
|
| 12 | +--- |
| 13 | +
|
| 14 | + FinitePolynomial(p::AbstractPolynomial, n::Integer) |
| 15 | +
|
| 16 | +A constructor method for `FinitePolynomial`. Takes in a polynomial `p` and a number `n`, and constructs a polynomial under modulo n. |
| 17 | +""" |
| 18 | +struct FinitePolynomial <: FiniteField |
| 19 | + p::AbstractPolynomial |
| 20 | + n::Integer |
| 21 | + |
| 22 | + function FinitePolynomial(p::AbstractPolynomial, n::Integer) |
| 23 | + p = Polynomial(mod.(p.coeffs, n)) |
| 24 | + new(p, n) |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +""" |
| 29 | + mod(p::Polynomial, n::Integer) -> Polynomial |
| 30 | + |
| 31 | +Uses the FinitePolynomial constructor to return a polynomial `p` under modulus `n`. |
| 32 | +
|
| 33 | +Parameters: |
| 34 | + - p::Polynomial: The input polynomial. |
| 35 | + - n::Integer: The modulus of the field. |
| 36 | + |
| 37 | +Returns |
| 38 | + - Polynomial: A polynomial modulo n. |
| 39 | +""" |
| 40 | +Base.mod(p::Polynomial, n::Integer) = FinitePolynomial(p, n).p |
| 41 | + |
| 42 | +""" |
| 43 | + Polynomial(A::Union{Tuple, AbstractArray}, n::Integer) -> Polynomial |
| 44 | + |
| 45 | +Constructs a polynomial under modulo `n`. |
| 46 | +
|
| 47 | +Parameters: |
| 48 | + - A::Union{Tuple, AbstractArrau}: The polynomial coefficients. |
| 49 | + - n::Integer: The modulus of the field. |
| 50 | + |
| 51 | +Returns |
| 52 | + - Polynomial: A polynomial modulo n. |
| 53 | +""" |
| 54 | +Polynomial(A::Union{Tuple, AbstractArray}, n::Integer) = mod(Polynomial(A), n) |
| 55 | + |
| 56 | +""" |
| 57 | + list_polys(n::Integer, m::Integer) -> Array |
| 58 | + |
| 59 | +Lists all polynomials of degree less than to `n` under modulo `m`. |
| 60 | +
|
| 61 | +Parameters: |
| 62 | + - n::Integer: Highest degree of polynomial. |
| 63 | + - m::Integer: The modulus of the field. |
| 64 | + |
| 65 | +Returns: |
| 66 | + - Array: An array of polynomials of degree less than n, under modulo m. |
| 67 | +""" |
| 68 | +function list_polys(n::Integer, m::Integer) |
| 69 | + return collect(Polynomial(collect(t)) for t in Iterators.product([0:(m-1) for i in 1:n]...)) |
| 70 | +end |
| 71 | + |
| 72 | +""" |
| 73 | + multiplication_table(degree::Integer, modulo::Integer) -> Matrix |
| 74 | + |
| 75 | +Returns a table (matrix) of the multiplication of all combinations of polynomials for degree less than `degree`, under modulo `modulo`. |
| 76 | +
|
| 77 | +Parameters: |
| 78 | + - degree::Integer: Highest degree of polynomial. |
| 79 | + - modulo::Integer: The modulus of the field. |
| 80 | + |
| 81 | +Returns: |
| 82 | + - Matrix: A multiplication table of all polynomials with degree less than n, under modulus. |
| 83 | +""" |
| 84 | +function multiplication_table(degree::Integer, modulo::Integer) |
| 85 | + polys = list_polys(degree, modulo) |
| 86 | + number_of_polys = length(polys) |
| 87 | + poly_matrix = Matrix{Polynomial}(undef, number_of_polys, number_of_polys) |
| 88 | + |
| 89 | + for i in 1:number_of_polys, j in 1:number_of_polys |
| 90 | + poly_matrix[i,j] = mod(polys[i]*polys[j], modulo) |
| 91 | + end |
| 92 | + |
| 93 | + return poly_matrix |
| 94 | +end |
| 95 | + |
| 96 | +""" |
| 97 | + list_span(u̲::Vector, v̲::Vector, modulo::Integer) -> Array |
| 98 | +
|
| 99 | +Given two vectors `u̲` and `v̲`, prints all linear combinations of those vectors, modulo `modulo`. NB: this function can take in another vector, but is not yet generalised to more than three. |
| 100 | +
|
| 101 | +Parameters: |
| 102 | + - u̲::Vector: One vector. |
| 103 | + - v̲::Vector: Another vector. |
| 104 | + - modulo::Integer: The modulus of the field. |
| 105 | + |
| 106 | +Returns: |
| 107 | + - Array: All vectors in the span of u̲ and v̲, under modulo. |
| 108 | +""" |
| 109 | +function list_span(u̲::Vector, v̲::Vector, modulo::Integer)::Array{Array{Int, 1}} |
| 110 | + span = Vector[] |
| 111 | + |
| 112 | + for λ in 0:modulo-1, γ in 0:modulo-1 |
| 113 | + w̲ = mod.(λ*u̲ + γ*v̲, modulo) |
| 114 | + if w̲ ∉ span |
| 115 | + push!(span, w̲) |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + return span |
| 120 | +end |
| 121 | + |
| 122 | +function list_span(u̲::Vector, v̲::Vector, t̲::Vector, modulo::Integer)::Array{Array{Int, 1}} |
| 123 | + span = Vector[] |
| 124 | + |
| 125 | + for λ in 0:modulo-1, γ in 0:modulo-1, α in 0:modulo-1 |
| 126 | + w̲ = mod.(λ*u̲ + γ*v̲ + α*t̲, modulo) |
| 127 | + if w̲ ∉ span |
| 128 | + push!(span, w̲) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + return span |
| 133 | +end |
| 134 | + |
| 135 | +""" |
| 136 | + islinear(C::Vector, modulo::Integer; verbose::Bool=false) -> Bool |
| 137 | + |
| 138 | +Determines whether a code `C` is a linear code (i.e., if it is closed under addition, scalar multiplication, and has the zero vector in it). |
| 139 | +
|
| 140 | +Parameters: |
| 141 | + - C::Vector: A code, typically consisting of multiple vectors or strings. |
| 142 | + - modulo::Integer: The modulus of the field under which you are working. |
| 143 | + - verbose::Bool (kwarg): print the point at which C fails, if it does. |
| 144 | + |
| 145 | +Returns: |
| 146 | + - Bool: Whether or not the code `C` is linear (true or false). |
| 147 | +""" |
| 148 | +function islinear(C::Vector, modulo::Integer; verbose::Bool=false) |
| 149 | + allequal_length(C) || return false # not all codes are of the same length |
| 150 | + block_length = length(C[1]) |
| 151 | + 𝟎 = fill(0, block_length) |
| 152 | + |
| 153 | + if 𝟎 ∉ C |
| 154 | + if verbose |
| 155 | + println("The zero vector 0̲ is not in C.\n") |
| 156 | + end |
| 157 | + return false # the zero vector is not in the code |
| 158 | + end |
| 159 | + |
| 160 | + for c̲ ∈ C |
| 161 | + for λ in 0:modulo-1 |
| 162 | + if mod.(λ*c̲, modulo) ∉ C |
| 163 | + if verbose |
| 164 | + println(λ, " ⋅ ", c̲, " = ", mod.(λ*c̲, modulo), " ∉ C\n") |
| 165 | + end |
| 166 | + return false # this code isn't closed under scalar multiplication |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + for c̲ ∈ C, c̲′ ∈ C |
| 171 | + if c̲ ≠ c̲′ |
| 172 | + if mod.(c̲ + c̲′, modulo) ∉ C |
| 173 | + if verbose |
| 174 | + println(c̲, " + ", c̲′, " = ", mod.(c̲ + c̲′, modulo), " ∉ C\n") |
| 175 | + end |
| 176 | + return false # this code isn't closed under addition |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + if verbose |
| 183 | + println() |
| 184 | + end |
| 185 | + |
| 186 | + return true |
| 187 | +end |
| 188 | + |
| 189 | +""" |
| 190 | + isirreducible(f::AbstractPolynomial, modulo::Integer) -> Bool |
| 191 | + |
| 192 | +Checks if a polynomial is irreducible. |
| 193 | + |
| 194 | +Parameters: |
| 195 | + - f::Polynomial: The polynomial you need to check. |
| 196 | + - modulo::Integer: The modulus under which you are working. |
| 197 | + |
| 198 | +Returns: |
| 199 | + - Bool: Whether or not the polynomial is irreducible (true or false). |
| 200 | +""" |
| 201 | +function isirreducible(f::AbstractPolynomial, modulo::Integer) |
| 202 | + deg = length(f.coeffs) - 1 |
| 203 | + f = mod(f, deg) |
| 204 | + polys = list_polys(deg, modulo) |
| 205 | + |
| 206 | + for a in polys, b in polys |
| 207 | + isequal(f, mod(a*b, modulo)) && return false |
| 208 | + end |
| 209 | + |
| 210 | + return true |
| 211 | +end |
11 | 212 |
|
12 | 213 | """
|
13 | 214 | normal_form!(M::AbstractArray, n::Integer) -> Matrix{Integer}
|
|
0 commit comments