Skip to content

Commit da64552

Browse files
authored
Merge pull request #1558 from isaacsas/return_pivot_cols
optionally return col ordering in nullspace
2 parents 8ee9a74 + 0c3656b commit da64552

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/structural_transformation/bareiss.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,22 @@ function bareiss!(M::AbstractMatrix{T}, swap_strategy=bareiss_colswap;
207207
return (n, pivot, column_permuted)
208208
end
209209

210-
function nullspace(A)
210+
function nullspace(A; col_order=nothing)
211211
column_pivots = collect(1:size(A, 2))
212212
B = copy(A)
213213
(rank, d, column_permuted) = bareiss!(B; column_pivots)
214214
reduce_echelon!(B, rank, d)
215+
216+
# The first rank entries in col_order are columns that give a basis
217+
# for the column space. The remainder give the free variables.
218+
if col_order !== nothing
219+
resize!(col_order, size(A,2))
220+
col_order .= 1:size(A,2)
221+
for (i,cp) in enumerate(column_pivots)
222+
@swap(col_order[i],col_order[cp])
223+
end
224+
end
225+
215226
N = ModelingToolkit.reduced_echelon_nullspace(rank, B)
216227
apply_inv_pivot_rows!(N, column_pivots)
217228
end

test/linalg.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ N = ModelingToolkit.nullspace(A)
1818
@test size(N, 2) == 3
1919
@test rank(N) == 3
2020
@test iszero(A * N)
21+
22+
A = [0 1 2 0 1 0;
23+
0 0 0 0 0 1;
24+
0 0 0 0 0 1;
25+
1 0 1 2 0 1;
26+
0 0 0 2 1 0]
27+
col_order = Int[]
28+
N = ModelingToolkit.nullspace(A; col_order)
29+
colspan = A[:,col_order[1:4]] # rank is 4
30+
@test iszero(ModelingToolkit.nullspace(colspan))
31+
@test !iszero(ModelingToolkit.nullspace(A[:,col_order[1:5]]))
32+
@test !iszero(ModelingToolkit.nullspace(A[:,[col_order[1:4]...,col_order[6]]]))

0 commit comments

Comments
 (0)