/
matrix.jl
197 lines (159 loc) · 6.05 KB
/
matrix.jl
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Types of all Flint-backed matrices
const _FieldMatTypes = Union{QQMatrix, fpMatrix, FpMatrix, FqMatrix, fqPolyRepMatrix, FqPolyRepMatrix}
const _MatTypes = Union{_FieldMatTypes, ZZMatrix, zzModMatrix, ZZModMatrix}
################################################################################
#
# Support for view(A, :, i) and view(A, i, :)
#
################################################################################
# MatrixView{MatrixType, ElemType}
struct MatrixView{S, T} <: AbstractVector{T}
A::S
end
Base.length(V::MatrixView) = length(V.A)
Base.getindex(V::MatrixView, i::Int) = V.A[i]
Base.setindex!(V::MatrixView, z::ZZRingElem, i::Int) = (V.A[i] = z)
Base.setindex!(V::MatrixView, z, i::Int) = setindex!(V, ZZ(z), i)
Base.size(V::MatrixView) = (length(V.A), )
function Base.view(x::_MatTypes, r::Int, c::UnitRange{Int})
A = view(x, r:r, c)
return MatrixView{typeof(x), elem_type(base_ring(x))}(A)
end
function Base.view(x::_MatTypes, r::UnitRange{Int}, c::Int)
A = view(x, r, c:c)
return MatrixView{typeof(x), elem_type(base_ring(x))}(A)
end
################################################################################
#
# Generic kernel (calling nullspace in flint)
#
################################################################################
function kernel(A::_FieldMatTypes; side::Symbol = :left)
Solve.check_option(side, [:right, :left], "side")
if side === :left
K = kernel(transpose(A), side = :right)
return transpose(K)
end
return nullspace(A)[2]
end
################################################################################
#
# Solve context functionality
#
################################################################################
# Overwrite some solve context functionality so that it uses `transpose` and not
# `lazy_transpose`
function solve_init(A::_FieldMatTypes)
return Solve.SolveCtx{elem_type(base_ring(A)), typeof(A), typeof(A)}(A)
end
function Solve._init_reduce_transpose(C::Solve.SolveCtx{S, T}) where {S <: FieldElem, T <: _FieldMatTypes}
if isdefined(C, :red_transp) && isdefined(C, :trafo_transp)
return nothing
end
r, R, U = Solve._rref_with_transformation(transpose(matrix(C)))
Solve.set_rank!(C, r)
C.red_transp = R
C.trafo_transp = U
return nothing
end
function Solve._can_solve_internal_no_check(C::Solve.SolveCtx{S, T}, b::T, task::Symbol; side::Symbol = :left) where {S <: FieldElem, T <: _FieldMatTypes}
if side === :right
fl, sol = Solve._can_solve_with_rref(b, Solve.transformation_matrix(C), rank(C), Solve.pivot_and_non_pivot_cols(C), task)
else
fl, sol = Solve._can_solve_with_rref(transpose(b), Solve.transformation_matrix_of_transpose(C), rank(C), Solve.pivot_and_non_pivot_cols_of_transpose(C), task)
sol = transpose(sol)
end
if !fl || task !== :with_kernel
return fl, sol, zero(b, 0, 0)
end
return true, sol, kernel(C, side = side)
end
function Solve.kernel(C::Solve.SolveCtx{S, T}; side::Symbol = :left) where {S <: FieldElem, T <: _FieldMatTypes}
Solve.check_option(side, [:right, :left], "side")
if side === :right
return Solve._kernel_of_rref(Solve.reduced_matrix(C), rank(C), Solve.pivot_and_non_pivot_cols(C))[2]
else
nullity, X = Solve._kernel_of_rref(Solve.reduced_matrix_of_transpose(C), rank(C), Solve.pivot_and_non_pivot_cols_of_transpose(C))
return transpose(X)
end
end
################################################################################
#
# Eigenvalues and eigenspaces
#
################################################################################
@doc raw"""
eigenvalues(M::MatElem{T}) where T <: FieldElem
Return the eigenvalues of `M`.
"""
function eigenvalues(M::MatElem{T}) where T <: FieldElem
@assert is_square(M)
K = base_ring(M)
f = charpoly(M)
return roots(f)
end
@doc raw"""
eigenvalues_with_multiplicities(M::MatElem{T}) where T <: FieldElem
Return the eigenvalues of `M` together with their algebraic multiplicities as a
vector of tuples.
"""
function eigenvalues_with_multiplicities(M::MatElem{T}) where T <: FieldElem
@assert is_square(M)
K = base_ring(M)
Kx, x = polynomial_ring(K, "x", cached = false)
f = charpoly(Kx, M)
r = roots(f)
return [ (a, valuation(f, x - a)) for a in r ]
end
@doc raw"""
eigenvalues(L::Field, M::MatElem{T}) where T <: RingElem
Return the eigenvalues of `M` over the field `L`.
"""
function eigenvalues(L::Field, M::MatElem{T}) where T <: RingElem
@assert is_square(M)
M1 = change_base_ring(L, M)
return eigenvalues(M1)
end
@doc raw"""
eigenvalues_with_multiplicities(L::Field, M::MatElem{T}) where T <: RingElem
Return the eigenvalues of `M` over the field `L` together with their algebraic
multiplicities as a vector of tuples.
"""
function eigenvalues_with_multiplicities(L::Field, M::MatElem{T}) where T <: RingElem
@assert is_square(M)
M1 = change_base_ring(L, M)
return eigenvalues_with_multiplicities(M1)
end
@doc raw"""
eigenspace(M::MatElem{T}, lambda::T; side::Symbol = :left)
where T <: FieldElem -> Vector{MatElem{T}}
Return a basis of the eigenspace of $M$ with respect to the eigenvalue $\lambda$.
If side is `:right`, the right eigenspace is computed, i.e. vectors $v$ such that
$Mv = \lambda v$. If side is `:left`, the left eigenspace is computed, i.e. vectors
$v$ such that $vM = \lambda v$.
"""
function eigenspace(M::MatElem{T}, lambda::T; side::Symbol = :left) where T <: FieldElem
@assert is_square(M)
N = deepcopy(M)
for i = 1:ncols(N)
N[i, i] -= lambda
end
return kernel(N, side = side)
end
@doc raw"""
eigenspaces(M::MatElem{T}; side::Symbol = :left)
where T <: FieldElem -> Dict{T, MatElem{T}}
Return a dictionary containing the eigenvalues of $M$ as keys and bases for the
corresponding eigenspaces as values.
If side is `:right`, the right eigenspaces are computed, if it is `:left` then the
left eigenspaces are computed.
See also `eigenspace`.
"""
function eigenspaces(M::MatElem{T}; side::Symbol = :left) where T<:FieldElem
S = eigenvalues(M)
L = Dict{elem_type(base_ring(M)), typeof(M)}()
for k in S
push!(L, k => vcat(eigenspace(M, k, side = side)))
end
return L
end