Skip to content

Commit 9ac586b

Browse files
authored
swap_cols and swap_rows for Julia matrices (#1928)
Also remove a bounds check in `swap_rows!` for MatrixElem, to match `swap_cols!`. This follows our general principle that `!` methods don't perform such tests, it is up to the caller to ensure such preconditions are satisfied.
1 parent 739c5fc commit 9ac586b

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

src/Matrix.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6060,7 +6060,7 @@ end
60606060

60616061
###############################################################################
60626062
#
6063-
# Row swapping
6063+
# Row & column permutations
60646064
#
60656065
###############################################################################
60666066

@@ -6120,12 +6120,9 @@ julia> M # was modified
61206120
```
61216121
"""
61226122
function swap_rows!(a::MatrixElem{T}, i::Int, j::Int) where T <: NCRingElement
6123-
(1 <= i <= nrows(a) && 1 <= j <= nrows(a)) || throw(BoundsError())
61246123
if i != j
61256124
for k = 1:ncols(a)
6126-
x = a[i, k]
6127-
a[i, k] = a[j, k]
6128-
a[j, k] = x
6125+
a[i, k], a[j, k] = a[j, k], a[i, k]
61296126
end
61306127
end
61316128
return a
@@ -6153,9 +6150,7 @@ matrix (since matrices are assumed to be mutable in AbstractAlgebra.jl).
61536150
function swap_cols!(a::MatrixElem{T}, i::Int, j::Int) where T <: NCRingElement
61546151
if i != j
61556152
for k = 1:nrows(a)
6156-
x = a[k, i]
6157-
a[k, i] = a[k, j]
6158-
a[k, j] = x
6153+
a[k, i], a[k, j] = a[k, j], a[k, i]
61596154
end
61606155
end
61616156
return a

src/julia/Matrix.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,41 @@ function zeros(R::NCRing, r::Int...)
7979
end
8080
return A
8181
end
82+
83+
###############################################################################
84+
#
85+
# Row & column permutations
86+
#
87+
###############################################################################
88+
89+
function swap_rows(a::Matrix{T}, i::Int, j::Int) where T <: NCRingElement
90+
(1 <= i <= nrows(a) && 1 <= j <= nrows(a)) || throw(BoundsError())
91+
b = deepcopy(a)
92+
swap_rows!(b, i, j)
93+
return b
94+
end
95+
96+
function swap_rows!(a::Matrix{T}, i::Int, j::Int) where T <: NCRingElement
97+
if i != j
98+
for k = 1:ncols(a)
99+
a[i, k], a[j, k] = a[j, k], a[i, k]
100+
end
101+
end
102+
return a
103+
end
104+
105+
function swap_cols(a::Matrix{T}, i::Int, j::Int) where T <: NCRingElement
106+
(1 <= i <= ncols(a) && 1 <= j <= ncols(a)) || throw(BoundsError())
107+
b = deepcopy(a)
108+
swap_cols!(b, i, j)
109+
return b
110+
end
111+
112+
function swap_cols!(a::Matrix{T}, i::Int, j::Int) where T <: NCRingElement
113+
if i != j
114+
for k = 1:nrows(a)
115+
a[k, i], a[k, j] = a[k, i], a[k, j]
116+
end
117+
end
118+
return a
119+
end

test/Rings-conformance-tests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,24 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)
811811
@test a == A
812812
end
813813
end
814+
815+
@testset "Row & column permutations" begin
816+
a = matrix(R, [1 2 ; 3 4])
817+
b = swap_rows(a, 1, 2)
818+
@test b == matrix(R, [3 4 ; 1 2])
819+
@test a == matrix(R, [1 2 ; 3 4])
820+
821+
a = matrix(R, [1 2 ; 3 4])
822+
b = swap_cols(a, 1, 2)
823+
@test b == matrix(R, [2 1 ; 4 3])
824+
@test a == matrix(R, [1 2 ; 3 4])
825+
826+
# TODO: reverse_rows, reverse_cols
827+
# TODO: add_column, add_row
828+
# TODO: multiply_column, multiply_row
829+
# TODO: ! variants (such as `swap_cols!` etc.) of all of the above
830+
end
831+
814832
end
815833

816834
return nothing

0 commit comments

Comments
 (0)