Skip to content

Commit

Permalink
Clean InjProjMat up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt committed Apr 4, 2024
1 parent e177eb8 commit 9533878
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
10 changes: 4 additions & 6 deletions src/generic/DirectSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ end

function AbstractAlgebra.canonical_injection(A::DirectSumModule, i::Int)
B = summands(A)[i]
return hom(B, A, InjProjMat{elem_type(base_ring(A))}(base_ring(A), dim(B), dim(A), sum(dim(x) for x = summands(A)[1:i-1]; init = 0)+1))
return hom(B, A, [direct_sum_injection(i, A, x) for x = gens(B)])
return hom(B, A, inj_proj_mat(base_ring(A), dim(B), dim(A), sum(dim(x) for x = summands(A)[1:i-1]; init = 0)+1))

Check warning on line 157 in src/generic/DirectSum.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/DirectSum.jl#L157

Added line #L157 was not covered by tests
end

AbstractAlgebra._number_of_direct_product_factors(A::DirectSumModule) = length(summands(A))
Expand All @@ -174,8 +173,7 @@ end

function AbstractAlgebra.canonical_projection(A::DirectSumModule, i::Int)
B = summands(A)[i]
return hom(B, A, InjProjMat{elem_type(base_ring(A))}(base_ring(A), dim(A), dim(B), sum(dim(x) for x = summands(A)[1:i-1]; init = 0)+1))
return hom(A, summands(A)[i], [direct_sum_projection(A, i, x) for x = gens(A)])
return hom(B, A, inj_proj_mat(base_ring(A), dim(A), dim(B), sum(dim(x) for x = summands(A)[1:i-1]; init = 0)+1))

Check warning on line 176 in src/generic/DirectSum.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/DirectSum.jl#L175-L176

Added lines #L175 - L176 were not covered by tests
end

function direct_sum(m::Vector{<:AbstractAlgebra.FPModule{T}}) where T <: RingElement
Expand Down Expand Up @@ -210,8 +208,8 @@ function direct_sum(m::Vector{<:AbstractAlgebra.FPModule{T}}) where T <: RingEle
start = 0
for i = 1:length(m)
igens = ngens(m[i])
inj[i] = ModuleHomomorphism(m[i], M, InjProjMat{T}(R, igens, n, start+1))
pro[i] = ModuleHomomorphism(M, m[i], InjProjMat{T}(R, n, igens, start+1))
inj[i] = ModuleHomomorphism(m[i], M, inj_proj_mat(R, igens, n, start+1))
pro[i] = ModuleHomomorphism(M, m[i], inj_proj_mat(R, n, igens, start+1))
# Override image_fns with fast versions that don't do matrix-vector mul
inj[i].image_fn = x -> direct_sum_injection(i, M, x)
pro[i].image_fn = x -> direct_sum_projection(M, i, x)
Expand Down
27 changes: 15 additions & 12 deletions src/generic/GenericTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1320,21 +1320,24 @@ struct FreeModuleElem{T <: Union{RingElement, NCRingElem}} <: AbstractAlgebra.FP
end

"""
an n x m matrix over R, all zero but
if n>m: s is a row where an identity matrix is inserted
if n<m: s is a col where the identity is inserted
so this is a typical matrix to represent an projection from
a direct sum into a summand
or an injection into the sum
an n x m matrix over R, all zero but
if n>m: s is a row starting from which an identity matrix is inserted
if n<m: s is a column starting from which an identity matrix is inserted
Example: n = 2, m = 5, s = 3 gives the matrix
[0 0 1 0 0]
[0 0 0 1 0]
So this is a typical matrix to represent an projection from
a direct sum into a summand or an injection into the sum.
"""
struct InjProjMat{T <: RingElement} <: AbstractAlgebra.MatElem{T}
R
n::Int #Rows
m::Int #Cols
s::Int
R
n::Int #Rows
m::Int #Cols
s::Int
end

###############################################################################
#
# ModuleHomomorphism
Expand Down
57 changes: 32 additions & 25 deletions src/generic/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,65 +252,75 @@ Base.size(V::MatSpaceVecView) = (length(V.entries), )
#
###############################################################################

function inj_proj_mat(R::NCRing, r::Int, c::Int, s::Int)
@assert r >= 0 && c >= 0 && s >= 0
# Check whether there is space for a full identity matrix
if r <= c
@assert s + r - 1 <= c
else
@assert s + c - 1 <= r
end
return InjProjMat{elem_type(R)}(R, r, c, s)
end

AbstractAlgebra.nrows(K::InjProjMat) = K.n
AbstractAlgebra.ncols(K::InjProjMat) = K.m
AbstractAlgebra.base_ring(K::InjProjMat) = K.R
AbstractAlgebra.base_ring(K::InjProjMat{T}) where T = K.R::parent_type(T)

Check warning on line 268 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L268

Added line #L268 was not covered by tests

function AbstractAlgebra.matrix(K::InjProjMat)
R = base_ring(K)
if K.n >= K.m
return [zero_matrix(R, K.s-1, ncols(K)) ; identity_matrix(R, ncols(K)) ; zero_matrix(R, K.n - K.s - K.m + 1, ncols(K))]
if nrows(K) >= ncols(K)
return [zero_matrix(R, K.s-1, ncols(K)) ; identity_matrix(R, ncols(K)) ; zero_matrix(R, nrows(K) - K.s - ncols(K) + 1, ncols(K))]

Check warning on line 273 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L270-L273

Added lines #L270 - L273 were not covered by tests
else
return [zero_matrix(R, nrows(K), K.s-1) identity_matrix(R, nrows(K)) zero_matrix(R, nrows(K), K.m-K.s-K.n + 1)]
return [zero_matrix(R, nrows(K), K.s-1) identity_matrix(R, nrows(K)) zero_matrix(R, nrows(K), ncols(K)-K.s-nrows(K) + 1)]

Check warning on line 275 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L275

Added line #L275 was not covered by tests
end
end

function Base.getindex(K::InjProjMat{T}, i::Int, j::Int) where T
(1 <= i <= nrows(K) && 1 <= j <= ncols(K)) || error(BoundsError(K, (i,j)))
K.n >= K.m && i - K.s + 1 == j && return one(K.R)::T
K.n <= K.m && i == j - K.s + 1 && return one(K.R)::T
return zero(K.R)::T
(1 <= i <= nrows(K) && 1 <= j <= ncols(K)) || error(BoundsError(K, (i, j)))
nrows(K) >= ncols(K) && i - K.s + 1 == j && return one(base_ring(K))::T
nrows(K) <= ncols(K) && i == j - K.s + 1 && return one(base_ring(K))::T
return zero(base_ring(K))::T

Check warning on line 283 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L279-L283

Added lines #L279 - L283 were not covered by tests
end

function *(b::InjProjMat{T}, c::MatElem{T}) where {T <: NCRingElement}
@assert ncols(b) == nrows(c)
R = base_ring(b)
@assert base_ring(c) == R
if b.n >= b.m
@assert base_ring(c) === R
if nrows(b) >= ncols(b)
z = zero_matrix(R, nrows(b), ncols(c))
z[b.s:b.s+nrows(c)-1, :] = c
return z

Check warning on line 293 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L286-L293

Added lines #L286 - L293 were not covered by tests
# return [zero_matrix(R, b.s-1, ncols(c)) ; c ; zero_matrix(R, b.n - b.m - b.s + 1, ncols(c))]
else
return c[b.s:b.s+b.m-1, :]
return c[b.s:b.s+ncols(b)-1, :]

Check warning on line 295 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L295

Added line #L295 was not covered by tests
end
end

function *(b::MatElem{T}, c::InjProjMat{T}) where {T <: NCRingElement}
@assert ncols(b) == nrows(c)
R = base_ring(b)
@assert base_ring(c) == R
if c.n >= c.m
@assert base_ring(c) === R
if nrows(c) >= ncols(c)

Check warning on line 303 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L299-L303

Added lines #L299 - L303 were not covered by tests
#c = [0 I 0]^t
return b[:, c.s:c.s+c.m-1]
return b[:, c.s:c.s+ncols(c)-1]

Check warning on line 305 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L305

Added line #L305 was not covered by tests
else
z = zero_matrix(R, nrows(b), ncols(c))
z[:, c.s:c.s+c.n-1] = b
z[:, c.s:c.s+nrows(c)-1] = b
return z

Check warning on line 309 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L307-L309

Added lines #L307 - L309 were not covered by tests
return [zero_matrix(R, nrows(b), c.s-1) b zero_matrix(R, nrows(b), c.m - c.n - c.s + 1)]
end
end

function +(b::MatElem{T}, c::InjProjMat{T}) where {T <: NCRingElement}
@assert size(b) == size(c)
R = base_ring(b)
@assert base_ring(c) === R
a = deepcopy(b)
if c.n >= c.m
for i=1:c.m
if nrows(c) >= ncols(c)
for i in 1:ncols(c)
add_one!(a, c.s+i-1, i)
end

Check warning on line 321 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L313-L321

Added lines #L313 - L321 were not covered by tests
else
for i=1:c.n
for i in 1:nrows(c)
add_one!(a, i, c.s+i-1)
end

Check warning on line 325 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L323-L325

Added lines #L323 - L325 were not covered by tests
end
Expand All @@ -319,9 +329,6 @@ end
+(c::InjProjMat{T}, b::MatElem{T}) where {T <: NCRingElement} = b+c

Check warning on line 329 in src/generic/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/Matrix.jl#L329

Added line #L329 was not covered by tests

function add_one!(a::MatElem, i::Int, j::Int)
a[i,j] += 1
a[i, j] += 1
return a
end




0 comments on commit 9533878

Please sign in to comment.