Skip to content

Commit

Permalink
Improve type stability
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt committed Jan 17, 2024
1 parent fa5f5d0 commit 2e4f705
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import AbstractAlgebra: base_ring, nrows, ncols
#
################################################################################

mutable struct LazyTransposeMatElem{T} <: MatElem{T}
M::MatElem{T}
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) = LazyTransposeMatElem(M)
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!
Expand Down
12 changes: 6 additions & 6 deletions test/Solve-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ end
M = matrix(QQ, [1 2 3 4 5; 0 0 8 9 10; 0 0 0 14 15])
MT = AbstractAlgebra.Solve.lazy_transpose(M)

@test AbstractAlgebra.Solve.data(MT) === M
@test AbstractAlgebra.Solve.lazy_transpose(MT) === M
@test @inferred AbstractAlgebra.Solve.data(MT) === M
@test @inferred AbstractAlgebra.Solve.lazy_transpose(MT) === M
@test transpose(M) == MT

@test nrows(MT) == 5
Expand All @@ -71,16 +71,16 @@ end

@test base_ring(MT) == QQ

@test zero(MT) == AbstractAlgebra.Solve.lazy_transpose(zero_matrix(QQ, 3, 5))
@test zero(MT, 2, 3) == AbstractAlgebra.Solve.lazy_transpose(zero_matrix(QQ, 3, 2))
@test @inferred zero(MT) == AbstractAlgebra.Solve.lazy_transpose(zero_matrix(QQ, 3, 5))
@test @inferred zero(MT, 2, 3) == AbstractAlgebra.Solve.lazy_transpose(zero_matrix(QQ, 3, 2))

S = similar(MT)
S = @inferred similar(MT)
@test S isa AbstractAlgebra.Solve.LazyTransposeMatElem
@test nrows(S) == 5
@test ncols(S) == 3
@test base_ring(S) == QQ

S = similar(MT, 2, 3)
S = @inferred similar(MT, 2, 3)
@test S isa AbstractAlgebra.Solve.LazyTransposeMatElem
@test nrows(S) == 2
@test ncols(S) == 3
Expand Down

0 comments on commit 2e4f705

Please sign in to comment.