-
Notifications
You must be signed in to change notification settings - Fork 63
/
Solve.jl
699 lines (589 loc) · 22.3 KB
/
Solve.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
module Solve
using AbstractAlgebra
import AbstractAlgebra: base_ring, nrows, ncols, matrix, rank, Generic, kernel
################################################################################
#
# "Lazy" transpose of a matrix
#
################################################################################
mutable struct LazyTransposeMatElem{T, MatT} <: MatElem{T} where {MatT <: MatElem{T}}
M::MatT
end
data(M::LazyTransposeMatElem) = M.M
# The entries of M and the result are SHARED, so e.g. a setindex! will modify
# 'both' matrices. But this is the point: we don't want to actually transpose
# the matrix.
lazy_transpose(M::MatElem{T}) where T = LazyTransposeMatElem{T, typeof(M)}(M)
lazy_transpose(M::LazyTransposeMatElem) = data(M)
# Change the order of rows and columns in nrows, ncols, getindex and setindex!
AbstractAlgebra.nrows(M::LazyTransposeMatElem) = ncols(data(M))
AbstractAlgebra.ncols(M::LazyTransposeMatElem) = nrows(data(M))
Base.getindex(M::LazyTransposeMatElem, r::Int, c::Int) = data(M)[c, r]
function Base.setindex!(M::LazyTransposeMatElem{T}, d::T, r::Int, c::Int) where T
setindex!(M.M, d, c, r)
return M
end
AbstractAlgebra.base_ring(M::LazyTransposeMatElem) = base_ring(data(M))
Base.zero(M::LazyTransposeMatElem) = lazy_transpose(zero(data(M)))
Base.zero(M::LazyTransposeMatElem, i::Int, j::Int) = lazy_transpose(zero(data(M), j, i))
Base.similar(M::LazyTransposeMatElem) = lazy_transpose(similar(data(M)))
Base.similar(M::LazyTransposeMatElem, i::Int, j::Int) = lazy_transpose(similar(data(M), j, i))
################################################################################
#
# Linear solving context object
#
################################################################################
mutable struct SolveCtx{T, MatT, TranspMatT}
A::MatT # matrix giving the linear system
red::MatT # reduced/canonical form of A (rref, hnf, lu)
red_transp::TranspMatT # reduced/canonical form of transpose(A)
trafo::MatT # transformation: trafo*A == red (not used for lu)
trafo_transp::TranspMatT # transformation: trafo_transp*transpose(A) == red_transp
# (not used for lu)
lu_perm::Generic.Perm # permutation used for the lu factorization of A
lu_perm_transp::Generic.Perm # permutation used for the lu factorization of transpose(A)
rank::Int # rank of A
pivots::Vector{Int} # pivot and non-pivot columns of red
pivots_transp::Vector{Int} # pivot and non-pivot columns of red_transp
function SolveCtx{T, MatT, TranspMatT}(A::MatT) where {T, MatT <: MatElem{T}, TranspMatT <: MatElem{T}}
z = new{T, MatT, TranspMatT}()
z.A = A
z.rank = -1 # not known yet
return z
end
function SolveCtx(A::MatElem{T}) where T
return SolveCtx{T, typeof(A), LazyTransposeMatElem{T, typeof(A)}}(A)
end
end
@doc raw"""
solve_init(A::MatElem)
Return a context object `C` that allows to efficiently solve linear systems
$Ax = b$ or $xA = b$ for different $b$.
"""
function solve_init(A::MatElem)
return SolveCtx(A)
end
matrix(C::SolveCtx) = C.A
function _init_reduce(C::SolveCtx{<:FieldElement})
if isdefined(C, :red) && isdefined(C, :trafo)
return nothing
end
r, R, U = _rref_with_transformation(matrix(C))
set_rank!(C, r)
C.red = R
C.trafo = U
return nothing
end
function _init_reduce(C::SolveCtx{<:RingElement})
if isdefined(C, :red) && isdefined(C, :trafo)
return nothing
end
R, U = hnf_with_transform(matrix(C))
C.red = R
C.trafo = U
return nothing
end
function _init_reduce_transpose(C::SolveCtx{<:FieldElement})
if isdefined(C, :red_transp) && isdefined(C, :trafo_transp)
return nothing
end
r, R, U = _rref_with_transformation(lazy_transpose(matrix(C)))
set_rank!(C, r)
C.red_transp = R
C.trafo_transp = U
return nothing
end
function _init_reduce_transpose(C::SolveCtx{<:RingElement})
if isdefined(C, :red_transp) && isdefined(C, :trafo_transp)
return nothing
end
R, U = hnf_with_transform(lazy_transpose(matrix(C)))
C.red_transp = R
C.trafo_transp = U
return nothing
end
function reduced_matrix(C::SolveCtx)
_init_reduce(C)
return C.red
end
function reduced_matrix_of_transpose(C::SolveCtx)
_init_reduce_transpose(C)
return C.red_transp
end
function lu_permutation(C::SolveCtx)
_init_reduce(C)
return C.lu_perm
end
function lu_permutation_of_transpose(C::SolveCtx)
_init_reduce_transpose(C)
return C.lu_perm_transp
end
function transformation_matrix(C::SolveCtx)
_init_reduce(C)
return C.trafo
end
function transformation_matrix_of_transpose(C::SolveCtx)
_init_reduce_transpose(C)
return C.trafo_transp
end
function set_rank!(C::SolveCtx, r::Int)
if C.rank >= 0
@assert C.rank == r
end
C.rank = r
return nothing
end
function AbstractAlgebra.rank(C::SolveCtx{<:FieldElement})
if C.rank < 0
_init_reduce(C)
end
return C.rank
end
AbstractAlgebra.nrows(C::SolveCtx) = nrows(matrix(C))
AbstractAlgebra.ncols(C::SolveCtx) = ncols(matrix(C))
AbstractAlgebra.base_ring(C::SolveCtx) = base_ring(matrix(C))
function pivot_and_non_pivot_cols(C::SolveCtx{<:FieldElement})
if !isdefined(C, :pivots)
R = reduced_matrix(C)
r = rank(C)
C.pivots = pivot_and_non_pivot_cols(R, r)
end
return C.pivots
end
function pivot_and_non_pivot_cols_of_transpose(C::SolveCtx{<:FieldElement})
if !isdefined(C, :pivots_transp)
R = reduced_matrix_of_transpose(C)
r = rank(C)
C.pivots_transp = pivot_and_non_pivot_cols(R, r)
end
return C.pivots_transp
end
################################################################################
#
# User facing functions for linear solving
#
################################################################################
@doc raw"""
solve(A::MatElem{T}, b::Vector{T}; side::Symbol = :left) where T
solve(A::MatElem{T}, b::MatElem{T}; side::Symbol = :left) where T
solve(C::SolveCtx{T}, b::Vector{T}; side::Symbol = :left) where T
solve(C::SolveCtx{T}, b::MatElem{T}; side::Symbol = :left) where T
Return $x$ of same type as $b$ solving the linear system $xA = b$, if `side == :left`
(default), or $Ax = b$, if `side == :right`.
If no solution exists, an error is raised.
If a context object `C` is supplied, then the above applies for `A = matrix(C)`.
See also [`can_solve_with_solution`](@ref).
"""
function solve(A::Union{MatElem{T}, SolveCtx{T}}, b::Union{Vector{T}, MatElem{T}}; side::Symbol = :left) where T
fl, x = can_solve_with_solution(A, b, side = side)
fl || throw(ArgumentError("Unable to solve linear system"))
return x
end
@doc raw"""
can_solve(A::MatElem{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve(A::MatElem{T}, b::MatElem{T}; side::Symbol = :left) where T
can_solve(C::SolveCtx{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve(C::SolveCtx{T}, b::MatElem{T}; side::Symbol = :left) where T
Return `true` if the linear system $xA = b$ or $Ax = b$ with `side == :left`
(default) or `side == :right`, respectively, has a solution and `false` otherwise.
If a context object `C` is supplied, then the above applies for `A = matrix(C)`.
See also [`can_solve_with_solution`](@ref).
"""
function can_solve(A::Union{MatElem{T}, SolveCtx{T}}, b::Union{Vector{T}, MatElem{T}}; side::Symbol = :left) where T
return _can_solve_internal(A, b, :only_check; side = side)[1]
end
@doc raw"""
can_solve_with_solution(A::MatElem{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve_with_solution(A::MatElem{T}, b::MatElem{T}; side::Symbol = :left) where T
can_solve_with_solution(C::SolveCtx{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve_with_solution(C::SolveCtx{T}, b::MatElem{T}; side::Symbol = :left) where T
Return `true` and $x$ of same type as $b$ solving the linear system $xA = b$, if
such a solution exists. Return `false` and an empty vector or matrix, if the
system has no solution.
If `side == :right`, the system $Ax = b$ is solved.
If a context object `C` is supplied, then the above applies for `A = matrix(C)`.
See also [`solve`](@ref).
"""
function can_solve_with_solution(A::Union{MatElem{T}, SolveCtx{T}}, b::Union{Vector{T}, MatElem{T}}; side::Symbol = :left) where T
return _can_solve_internal(A, b, :with_solution; side = side)[1:2]
end
@doc raw"""
can_solve_with_solution_and_kernel(A::MatElem{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve_with_solution_and_kernel(A::MatElem{T}, b::MatElem{T}; side::Symbol = :left) where T
can_solve_with_solution_and_kernel(C::SolveCtx{T}, b::Vector{T}; side::Symbol = :left) where T
can_solve_with_solution_and_kernel(C::SolveCtx{T}, b::MatElem{T}; side::Symbol = :left) where T
Return `true`, $x$ of same type as $b$ solving the linear system $xA = b$,
together with a matrix $K$ giving the kernel of $A$ (i.e. $KA = 0$), if such
a solution exists. Return `false`, an empty vector or matrix and an empty matrix,
if the system has no solution.
If `side == :right`, the system $Ax = b$ is solved.
If a context object `C` is supplied, then the above applies for `A = matrix(C)`.
See also [`solve`](@ref) and [`kernel`](@ref).
"""
function can_solve_with_solution_and_kernel(A::Union{MatElem{T}, SolveCtx{T}}, b::Union{Vector{T}, MatElem{T}}; side::Symbol = :left) where T
return _can_solve_internal(A, b, :with_kernel; side = side)
end
@doc raw"""
kernel(A::MatElem; side::Symbol = :left)
kernel(C::SolveCtx; side::Symbol = :left)
Return a matrix $K$ whose rows generate the left kernel of $A$, that
is, $KA$ is the zero matrix.
If `side == :right`, the columns of $K$ generate the right kernel of $A$, that
is, $AK$ is the zero matrix.
If the base ring is a principal ideal domain, the rows or columns respectively of $K$
are a basis of the respective kernel.
If a context object `C` is supplied, then the above applies for `A = matrix(C)`.
"""
function kernel(A::MatElem{<:FieldElement}; side::Symbol = :left)
check_option(side, [:right, :left], "side")
if side === :left
K = kernel(lazy_transpose(A), side = :right)
return lazy_transpose(K)
end
n, K = AbstractAlgebra.nullspace(A)
if ncols(K) > n
# For compatibility with `nullspace` methods in Nemo which add zero columns
K = sub(K, 1:nrows(K), 1:n)
end
return K
end
function kernel(A::MatElem{<:RingElement}; side::Symbol = :left)
check_option(side, [:right, :left], "side")
if side === :right
H, U = hnf_with_transform(lazy_transpose(A))
return _kernel_of_hnf(A, H, U)[2]
else
H, U = hnf_with_transform(A)
_, X = _kernel_of_hnf(lazy_transpose(A), H, U)
# X is of type LazyTransposeMatElem
return data(X)
end
end
function kernel(C::SolveCtx{<:FieldElement}; side::Symbol = :left)
check_option(side, [:right, :left], "side")
if side === :right
return _kernel_of_rref(reduced_matrix(C), rank(C), pivot_and_non_pivot_cols(C))[2]
else
nullity, X = _kernel_of_rref(reduced_matrix_of_transpose(C), rank(C), pivot_and_non_pivot_cols_of_transpose(C))
# X is of type LazyTransposeMatElem
return data(X)
end
end
function kernel(C::SolveCtx{<:RingElement}; side::Symbol = :left)
check_option(side, [:right, :left], "side")
if side === :right
return _kernel_of_hnf(matrix(C), reduced_matrix_of_transpose(C), transformation_matrix_of_transpose(C))[2]
else
nullity, X = _kernel_of_hnf(lazy_transpose(matrix(C)), reduced_matrix(C), transformation_matrix(C))
# X is of type LazyTransposeMatElem
return data(X)
end
end
################################################################################
#
# Internal functionality
#
################################################################################
###
# General concept:
# `_can_solve_internal` checks the sanity of the input and then calls
# `_can_solve_internal_no_check` . Only the latter function needs to be
# implemented for a given type of matrices. Specifically one needs to implement
# the signature(s)
# _can_solve_internal_no_check(A::MatrixType, b::MatrixType, task::Symbol, side::Symbol)
# _can_solve_internal_no_check(C::SolveCtx, b::MatrixType, task::Symbol, side::Symbol)
# Inside these functions one can assume that A (resp. C) and b have compatible
# dimensions and that `task` and `side` are set to a "legal" option.
# These functions should then (try to) solve Ax = b (side == :right) or xA = b
# (side == :left) possibly with kernel.
# They must always return a tuple (Bool, MatrixType, MatrixType).
# task may be:
# * :only_check -> It is only tested whether there is a solution, the second
# and third return value are only for type stability
# * :with_solution -> A solution is computed, the last return value is only
# for type stability
# * :with_kernel -> A solution and the kernel is computed
###
# A is supposed to be in rref of rank r
# Return a Vector of length ncols(A) with the first r entries the pivot columns
# of A and the following entries the non-pivot columns (in ascending order).
function pivot_and_non_pivot_cols(A::MatElem, r::Int)
p = zeros(Int, ncols(A))
j = 1
k = 1
for i = 1:r
while is_zero_entry(A, i, j)
p[r + k] = j
j += 1
k += 1
end
p[i] = j
j += 1
end
while k <= ncols(A) - r
p[r + k] = j
j += 1
k += 1
end
return p
end
# Transform a right hand side of type Vector into a MatElem and do sanity checks
function _can_solve_internal(A::Union{MatElem{T}, SolveCtx{T}}, b::Vector{T}, task::Symbol; side::Symbol = :left) where T
check_option(task, [:only_check, :with_solution, :with_kernel], "task")
check_option(side, [:right, :left], "side")
isright = side === :right
if isright
check_linear_system_dim_right(A, b)
B = matrix(base_ring(A), nrows(A), 1, b)
else # side == :left
check_linear_system_dim_left(A, b)
B = matrix(base_ring(A), 1, ncols(A), b)
end
fl, sol, K = _can_solve_internal_no_check(A, B, task, side = side)
if isright
x = eltype(b)[ sol[i, 1] for i in 1:nrows(sol) ]
else # side == :left
x = eltype(b)[ sol[1, i] for i in 1:ncols(sol) ]
end
return fl, x, K
end
# Do sanity checks and call _can_solve_internal_no_check
function _can_solve_internal(A::Union{MatElem{T}, SolveCtx{T}}, b::MatElem{T}, task::Symbol; side::Symbol = :left) where T
check_option(task, [:only_check, :with_solution, :with_kernel], "task")
check_option(side, [:right, :left], "side")
if side === :right
check_linear_system_dim_right(A, b)
else
check_linear_system_dim_left(A, b)
end
return _can_solve_internal_no_check(A, b, task, side = side)
end
# _can_solve_internal_no_check over FIELDS
function _can_solve_internal_no_check(A::MatElem{T}, b::MatElem{T}, task::Symbol; side::Symbol = :left) where T <: FieldElement
R = base_ring(A)
if side === :left
# For side == :left, we pretend that A and b are transposed
fl, sol, K = _can_solve_internal_no_check(lazy_transpose(A), lazy_transpose(b), task, side = :right)
return fl, data(sol), data(K)
end
mu = hcat(deepcopy(A), deepcopy(b))
rk = rref!(mu)
p = pivot_and_non_pivot_cols(mu, rk)
if any(i -> i > ncols(A), p[1:rk])
return false, zero(A, 0, 0), zero(A, 0, 0)
end
if task === :only_check
return true, zero(A, 0, 0), zero(A, 0, 0)
end
# Compute a solution
sol = zero(A, ncols(A), ncols(b))
for i = 1:rk
for j = 1:ncols(b)
sol[p[i], j] = mu[i, ncols(A) + j]
end
end
if task === :with_solution
return true, sol, zero(A, 0, 0)
end
# Build the kernel
nullity = ncols(A) - rk
X = zero(A, ncols(A), nullity)
for i = 1:nullity
for j = 1:rk
X[p[j], i] = -mu[j, p[rk + i]]
end
X[p[rk + i], i] = one(R)
end
return true, sol, X
end
# _can_solve_internal_no_check over RINGS
function _can_solve_internal_no_check(A::MatElem{T}, b::MatElem{T}, task::Symbol; side::Symbol = :left) where T <: RingElement
R = base_ring(A)
if side === :left
# For side == :left, we pretend that A and b are transposed
fl, sol, K = _can_solve_internal_no_check(lazy_transpose(A), lazy_transpose(b), task, side = :right)
return fl, data(sol), data(K)
end
H, S = hnf_with_transform(lazy_transpose(A))
fl, sol = _can_solve_with_hnf(b, H, S, task)
if !fl || task !== :with_kernel
return fl, sol, zero(A, 0, 0)
end
n, N = _kernel_of_hnf(A, H, S)
return true, sol, N
end
# _can_solve_internal_no_check over FIELDS with SOLVE CONTEXT
function _can_solve_internal_no_check(C::SolveCtx{T}, b::MatElem{T}, task::Symbol; side::Symbol = :left) where T <: FieldElement
if side === :right
fl, sol = _can_solve_with_rref(b, transformation_matrix(C), rank(C), pivot_and_non_pivot_cols(C), task)
else
fl, sol = _can_solve_with_rref(lazy_transpose(b), transformation_matrix_of_transpose(C), rank(C), pivot_and_non_pivot_cols_of_transpose(C), task)
sol = data(sol)
end
if !fl || task !== :with_kernel
return fl, sol, zero(b, 0, 0)
end
return true, sol, kernel(C, side = side)
end
# _can_solve_internal_no_check over RINGS with SOLVE CONTEXT
function _can_solve_internal_no_check(C::SolveCtx{T}, b::MatElem{T}, task::Symbol; side::Symbol = :left) where T <: RingElement
if side === :right
fl, sol = _can_solve_with_hnf(b, reduced_matrix_of_transpose(C), transformation_matrix_of_transpose(C), task)
else
fl, sol = _can_solve_with_hnf(lazy_transpose(b), reduced_matrix(C), transformation_matrix(C), task)
sol = data(sol)
end
if !fl || task !== :with_kernel
return fl, sol, zero(b, 0, 0)
end
return true, sol, kernel(C, side = side)
end
################################################################################
#
# Internals for solving of row reduced matrices
#
################################################################################
# Solve Ax = b with U*A in rref of rank r.
# pivots must be of length ncols(A) and contain the pivot columns of U*A in the
# first r entries.
# Takes same options for `task` as _can_solve_internal but only returns (flag, solution)
# and no kernel.
function _can_solve_with_rref(b::MatElem{T}, U::MatElem{T}, r::Int, pivots::Vector{Int}, task::Symbol) where T <: FieldElement
bU = U*b
if any(i -> !is_zero_row(bU, i), r + 1:nrows(bU))
return false, zero(b, 0, 0)
end
if task === :only_check
return true, zero(b, 0, 0)
end
# Compute a solution
sol = zero(b, length(pivots), ncols(b))
for i = 1:r
for j = 1:ncols(b)
sol[pivots[i], j] = bU[i, j]
end
end
return true, sol
end
# Compute a matrix N with RN == 0 where the columns of N give a basis for the kernel.
# R must be in rref of rank r and pivots must be of length ncols(R) with the pivot
# columns in the first r entries and the non-pivot columns in the remaining entries.
function _kernel_of_rref(R::MatElem{T}, r::Int, pivots::Vector{Int}) where T <: FieldElement
@assert length(pivots) == ncols(R)
nullity = ncols(R) - r
X = zero(R, ncols(R), nullity)
for i = 1:nullity
for j = 1:r
X[pivots[j], i] = -R[j, pivots[r + i]]
end
X[pivots[r + i], i] = one(base_ring(R))
end
return nullity, X
end
# Solve Ax = b where H = U*transpose(A) is in HNF.
# Takes same options for `task` as _can_solve_internal but only returns (flag, solution)
# and no kernel.
function _can_solve_with_hnf(b::MatElem{T}, H::MatElem{T}, U::MatElem{T}, task::Symbol) where T <: RingElement
sol = lazy_transpose(zero(b, nrows(H), ncols(b)))
l = min(nrows(H), ncols(H))
b = deepcopy(b)
for i = 1:ncols(b)
for j = 1:l
k = 1
while k <= ncols(H) && is_zero_entry(H, j, k)
k += 1
end
if k > ncols(H)
continue
end
q, r = divrem(b[k, i], H[j, k])
if !iszero(r)
return false, zero(b, 0, 0)
end
for h = k:ncols(H)
b[h, i] -= q*H[j, h]
end
sol[i, j] = q
end
end
if !is_zero(b)
return false, zero(b, 0, 0)
end
if task === :only_check
return true, zero(b, 0, 0)
end
return true, lazy_transpose(U)*lazy_transpose(sol)
end
# Compute a matrix N with AN == 0 where the columns of N give a basis for the kernel
# and H = U*transpose(A) is in HNF.
# The matrix A is only needed to get the return type right (MatElem vs LazyTransposeMatElem)
function _kernel_of_hnf(A::MatElem{T}, H::MatElem{T}, U::MatElem{T}) where T <: RingElement
r = nrows(H)
while r > 0 && is_zero_row(H, r)
r -= 1
end
nullity = nrows(H) - r
N = zero(A, nrows(H), nullity)
for i = 1:nrows(N)
for j = 1:ncols(N)
N[i, j] = U[r + j, i]
end
end
return nullity, N
end
# Copied from Hecke, to be replaced with echelon_form_with_transformation eventually
function _rref_with_transformation(M::MatElem{T}) where T <: FieldElement
n = hcat(deepcopy(M), identity_matrix(base_ring(M), nrows(M)))
rref!(n)
s = nrows(n)
while s > 0 && iszero(sub(n, s:s, 1:ncols(M)))
s -= 1
end
return s, sub(n, 1:nrows(M), 1:ncols(M)), sub(n, 1:nrows(M), ncols(M)+1:ncols(n))
end
################################################################################
#
# Checks
#
################################################################################
function check_option(x::Symbol, options::Vector{Symbol}, option_name::String, msg::String = "", throw_error::Bool = true)
if msg == ""
msg = "Unsupported argument $x for $option_name"
end
fl = (x in options)
if !fl && throw_error
throw(ArgumentError(msg))
end
return fl
end
# Checks whether A and b have the same number of rows
function check_linear_system_dim_right(A::Union{MatElem, SolveCtx}, b::MatElem, throw_error::Bool = true)
fl = nrows(A) == nrows(b)
if !fl && throw_error
error("Incompatible number of rows in linear system (use `side = :left` for a system with the indeterminate on the left)")
end
return fl
end
function check_linear_system_dim_right(A::Union{MatElem, SolveCtx}, b::Vector, throw_error::Bool = true)
fl = nrows(A) == length(b)
if !fl && throw_error
error("Incompatible number of rows in linear system (use `side = :left` for a system with the indeterminate on the left)")
end
return fl
end
# Checks whether A and b have the same number of columns
function check_linear_system_dim_left(A::Union{MatElem, SolveCtx}, b::MatElem, throw_error::Bool = true)
fl = ncols(A) == ncols(b)
if !fl && throw_error
error("Incompatible number of columns in linear system (use `side = :right` for a system with the indeterminate on the right)")
end
return fl
end
function check_linear_system_dim_left(A::Union{MatElem, SolveCtx}, b::Vector, throw_error::Bool = true)
fl = ncols(A) == length(b)
if !fl && throw_error
error("Incompatible number of columns in linear system (use `side = :right` for a system with the indeterminate on the right)")
end
return fl
end
end