From 32776dc5a5f3a1336d348f3556f07553e1430a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 10:38:25 +0200 Subject: [PATCH 1/9] Add `can_solve` for `SMat` --- src/Sparse/Solve.jl | 30 ++++++++++++++++++++++++++++++ test/Sparse/Solve.jl | 2 ++ 2 files changed, 32 insertions(+) diff --git a/src/Sparse/Solve.jl b/src/Sparse/Solve.jl index 889499ea9d..8a7d315a39 100644 --- a/src/Sparse/Solve.jl +++ b/src/Sparse/Solve.jl @@ -346,6 +346,14 @@ function solve(a::SMat{T}, b::SRow{T}) where T <: FieldElem return sol end +function can_solve(a::SMat{T}, b::SRow{T}) where T <: FieldElem + c = sparse_matrix(base_ring(b)) + push!(c, b) + + # b is a row, so this is always from the left + return can_solve(a, c, side = :left) +end + function can_solve_with_solution(a::SMat{T}, b::SRow{T}) where T <: FieldElem c = sparse_matrix(base_ring(b)) push!(c, b) @@ -383,6 +391,28 @@ function find_pivot(A::SMat{T}) where T <: RingElement return p end +function can_solve(A::SMat{T}, B::SMat{T}; side::Symbol = :right) where T <: FieldElement + @assert side == :right || side == :left "Unsupported argument :$side for side: Must be :left or :right." + K = base_ring(A) + if side == :right + # sparse matrices might have omitted zero rows, so checking compatibility of + # the dimensions does not really make sense (?) + #nrows(A) != nrows(B) && error("Incompatible matrices") + mu = hcat(A, B) + ncolsA = ncols(A) + ncolsB = ncols(B) + else # side == :left + #ncols(A) != ncols(B) && error("Incompatible matrices") + mu = hcat(transpose(A), transpose(B)) + ncolsA = nrows(A) # They are transposed + ncolsB = nrows(B) + end + + rk, mu = rref(mu, truncate = true) + p = find_pivot(mu) + return !any(let ncolsA = ncolsA; i -> i > ncolsA; end, p) +end + function can_solve_with_solution(A::SMat{T}, B::SMat{T}; side::Symbol = :right) where T <: FieldElement @assert side == :right || side == :left "Unsupported argument :$side for side: Must be :left or :right." K = base_ring(A) diff --git a/test/Sparse/Solve.jl b/test/Sparse/Solve.jl index d429451d07..aaa3fbcd38 100644 --- a/test/Sparse/Solve.jl +++ b/test/Sparse/Solve.jl @@ -30,8 +30,10 @@ N = matrix(FlintQQ, rand([0,0,0,0,0,0,0,0,0,0,1], r, 2)) Ns = sparse_matrix(N) fl, sol = can_solve_with_solution(Ms, Ns, side = :right) + @test fl == can_solve(Ms, Ns, side = :right) @test nnz(sol) == sum(length(sol.rows[i].values) for i = 1:nrows(sol); init = 0) fl2, sol2 = can_solve_with_solution(M, N, side = :right) + @test fl2 == can_solve(M, N, side = :right) @test fl == fl2 if fl @test M*matrix(sol) == N From 9711ad95f9b1f071ffee7dfb91c7005e88456c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 10:42:14 +0200 Subject: [PATCH 2/9] Move `Matrix(::SMat)` to `Sparse/Matrix.jl` and adapt `Array(::SMat)` --- docs/src/sparse/intro.md | 3 ++- src/NumField/NfRel/NfRelNS.jl | 10 ---------- src/Sparse.jl | 2 +- src/Sparse/Matrix.jl | 27 +++++++++++++++++++++------ 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/docs/src/sparse/intro.md b/docs/src/sparse/intro.md index 5de81737f1..d2010f61c3 100644 --- a/docs/src/sparse/intro.md +++ b/docs/src/sparse/intro.md @@ -207,6 +207,7 @@ Other: sparse(::SMat) ZZMatrix(::SMat{ZZRingElem}) ZZMatrix(::SMat{T}) where {T <: Integer} -Array(::SMat{T}) where {T} +Matrix(::SMat) +Array(::SMat) ``` diff --git a/src/NumField/NfRel/NfRelNS.jl b/src/NumField/NfRel/NfRelNS.jl index e607638761..909eb99190 100644 --- a/src/NumField/NfRel/NfRelNS.jl +++ b/src/NumField/NfRel/NfRelNS.jl @@ -496,16 +496,6 @@ function minpoly_dense(a::NfRelNSElem) end end -function Base.Matrix(a::SMat) - A = zero_matrix(base_ring(a), nrows(a), ncols(a)) - for i = 1:nrows(a) - for (k, c) = a[i] - A[i, k] = c - end - end - return A -end - function minpoly_sparse(a::NfRelNSElem) K = parent(a) n = degree(K) diff --git a/src/Sparse.jl b/src/Sparse.jl index d7a786ca54..f0db9f798f 100644 --- a/src/Sparse.jl +++ b/src/Sparse.jl @@ -17,7 +17,7 @@ One (+one with trafo) is probably enough =# -import Base.push!, Base.max, Nemo.nbits, Base.Array, +import Base.push!, Base.max, Nemo.nbits, Base.Array, Base.Matrix, Base.hcat, Base.vcat, Base.max, Base.min diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index 65a7d09216..2c7c1383f7 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -1396,19 +1396,34 @@ function SparseArrays.sparse(A::SMat{T}) where T return SparseArrays.sparse(I, J, V) end +@doc raw""" + Matrix(A::SMat{T}) -> Matrix{T} + +The same matrix, but as a julia matrix. +""" +function Matrix(a::SMat) + A = zero_matrix(base_ring(a), nrows(a), ncols(a)) + for i = 1:nrows(a) + for (k, c) = a[i] + A[i, k] = c + end + end + return A +end + @doc raw""" Array(A::SMat{T}) -> Matrix{T} The same matrix, but as a two-dimensional julia array. """ -function Array(A::SMat{T}) where T - R = zero_matrix(base_ring(A), A.r, A.c) - for i=1:nrows(A) - for j=1:length(A.rows[i].pos) - R[i, A.rows[i].pos[j]] = A.rows[i].values[j] +function Array(a::SMat) + A = zero_matrix(base_ring(a), nrows(a), ncols(a)) + for i = 1:nrows(a) + for (k, c) = a[i] + A[i, k] = c end end - return R + return A end #TODO: write a kronnecker-row-product, this is THE From 4bf63f276db9d6d5dc2883a8cc5a7b03fc18c151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 11:02:40 +0200 Subject: [PATCH 3/9] Fix `Matrix(::SMat)` and `Array(::SMat)` to match their docstrings --- src/Sparse/Matrix.jl | 24 ++++++++++++------------ test/Sparse/Matrix.jl | 2 ++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index 2c7c1383f7..c583dde9d6 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -1401,14 +1401,14 @@ end The same matrix, but as a julia matrix. """ -function Matrix(a::SMat) - A = zero_matrix(base_ring(a), nrows(a), ncols(a)) - for i = 1:nrows(a) - for (k, c) = a[i] - A[i, k] = c +function Matrix(A::SMat) + M = elem_type(base_ring(A))[zero(base_ring(A)) for _ in 1:nrows(A), _ in 1:ncols(A)] + for i in 1:nrows(A) + for (k, c) in A[i] + M[i, k] = c end end - return A + return M end @doc raw""" @@ -1416,14 +1416,14 @@ end The same matrix, but as a two-dimensional julia array. """ -function Array(a::SMat) - A = zero_matrix(base_ring(a), nrows(a), ncols(a)) - for i = 1:nrows(a) - for (k, c) = a[i] - A[i, k] = c +function Array(A::SMat) + M = elem_type(base_ring(A))[zero(base_ring(A)) for _ in 1:nrows(A), _ in 1:ncols(A)] + for i in 1:nrows(A) + for (k, c) in A[i] + M[i, k] = c end end - return A + return M end #TODO: write a kronnecker-row-product, this is THE diff --git a/test/Sparse/Matrix.jl b/test/Sparse/Matrix.jl index cba8d16190..6d7cd2ed9f 100644 --- a/test/Sparse/Matrix.jl +++ b/test/Sparse/Matrix.jl @@ -286,6 +286,8 @@ using Hecke.SparseArrays # Conversion to julia types D = sparse_matrix(FlintZZ, [1 5 3; 0 -10 0; 0 1 0]) + @test Matrix(D) == ZZRingElem[1 5 3; 0 -10 0; 0 1 0] + @test Array(D) == ZZRingElem[1 5 3; 0 -10 0; 0 1 0] E = SparseArrays.sparse(D) @test Matrix(E) == ZZRingElem[1 5 3; 0 -10 0; 0 1 0] @test Array(E) == ZZRingElem[1 5 3; 0 -10 0; 0 1 0] From bb9addd3a4d7dcb5b93a6e34c91d75d9fb01ef8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 11:11:21 +0200 Subject: [PATCH 4/9] Add `Vector(::SRow)` --- docs/src/sparse/intro.md | 6 ++++++ src/Sparse/Row.jl | 21 +++++++++++++++++++++ test/Sparse/Row.jl | 5 ++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/src/sparse/intro.md b/docs/src/sparse/intro.md index d2010f61c3..c218c0ed9d 100644 --- a/docs/src/sparse/intro.md +++ b/docs/src/sparse/intro.md @@ -75,6 +75,12 @@ mod_sym!(::SRow{ZZRingElem}, ::Integer) maximum(::typeof(abs), ::SRow{ZZRingElem}) ``` +### Conversion to/from julia and AbstractAlgebra types + +```@docs +Vector(r::SRow, n::Int) +``` + ## Sparse matrices Let $R$ be a commutative ring. Sparse matrices with base ring $R$ are modelled by diff --git a/src/Sparse/Row.jl b/src/Sparse/Row.jl index 859e73298a..d3176c55a5 100644 --- a/src/Sparse/Row.jl +++ b/src/Sparse/Row.jl @@ -1,3 +1,5 @@ +import Base.Vector + export sparse_row, dot, scale_row!, add_scaled_row, permute_row ################################################################################ @@ -676,3 +678,22 @@ Returns the smallest entry of $A$. function minimum(A::SRow) return minimum(A.values) end + +################################################################################ +# +# Conversion to julia types +# +################################################################################ + +@doc raw""" + Vector(a::SMat{T}, n::Int) -> Vector{T} + +The first `n` entries of `a`, as a julia vector. +""" +function Vector(r::SRow, n::Int) + A = elem_type(base_ring(r))[zero(base_ring(r)) for _ in 1:n] + for i in intersect(r.pos, 1:n) + A[i] = r[i] + end + return A +end \ No newline at end of file diff --git a/test/Sparse/Row.jl b/test/Sparse/Row.jl index cf3ccbe6fc..39c9b7d3bb 100644 --- a/test/Sparse/Row.jl +++ b/test/Sparse/Row.jl @@ -154,5 +154,8 @@ C = sparse_row(FlintZZ, [1, 2, 4, 5], ZZRingElem[-10, 100, 1, 1]) @test minimum(C) == ZZRingElem(-10) - + # Conversion + A = sparse_row(FlintZZ, [1, 3, 4, 5], ZZRingElem[-5, 2, -10, 1]) + @test Vector(A, 3) == ZZRingElem[-5, 0, 2] + @test Vector(A, 6) == ZZRingElem[-5, 0, 2, -10, 1, 0] end From 4003bb98a148c6caaf342998b138fdb5aeec4cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 11:14:39 +0200 Subject: [PATCH 5/9] Move SRow <-> MatElem conversion from Oscar --- docs/src/sparse/intro.md | 2 ++ src/Sparse/Row.jl | 33 ++++++++++++++++++++++++++++++--- test/Sparse/Row.jl | 3 +++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/src/sparse/intro.md b/docs/src/sparse/intro.md index c218c0ed9d..c5922ba2e0 100644 --- a/docs/src/sparse/intro.md +++ b/docs/src/sparse/intro.md @@ -79,6 +79,8 @@ maximum(::typeof(abs), ::SRow{ZZRingElem}) ```@docs Vector(r::SRow, n::Int) +sparse_row(A::MatElem) +dense_row(r::SRow, n::Int) ``` ## Sparse matrices diff --git a/src/Sparse/Row.jl b/src/Sparse/Row.jl index d3176c55a5..fc6a587563 100644 --- a/src/Sparse/Row.jl +++ b/src/Sparse/Row.jl @@ -1,6 +1,6 @@ import Base.Vector -export sparse_row, dot, scale_row!, add_scaled_row, permute_row +export sparse_row, dot, scale_row!, add_scaled_row, permute_row, dense_row ################################################################################ # @@ -681,7 +681,7 @@ end ################################################################################ # -# Conversion to julia types +# Conversion from/to julia and AbstractAlgebra types # ################################################################################ @@ -696,4 +696,31 @@ function Vector(r::SRow, n::Int) A[i] = r[i] end return A -end \ No newline at end of file +end + +@doc raw""" + sparse_row(A::MatElem) + +Convert `A` to a sparse row. +`nrows(A) == 1` must hold. +""" +function sparse_row(A::MatElem) + @assert nrows(A) == 1 + if ncols(A) == 0 + return sparse_row(base_ring(A)) + end + return Hecke.sparse_matrix(A)[1] +end + +@doc raw""" + dense_row(r::SRow, n::Int) + +Convert `r[1:n]` to a dense row, that is an AbstractAlgebra matrix. +""" +function dense_row(r::SRow, n::Int) + A = zero_matrix(base_ring(r), 1, n) + for i in intersect(r.pos, 1:n) + A[1,i] = r[i] + end + return A +end diff --git a/test/Sparse/Row.jl b/test/Sparse/Row.jl index 39c9b7d3bb..24f95b203e 100644 --- a/test/Sparse/Row.jl +++ b/test/Sparse/Row.jl @@ -158,4 +158,7 @@ A = sparse_row(FlintZZ, [1, 3, 4, 5], ZZRingElem[-5, 2, -10, 1]) @test Vector(A, 3) == ZZRingElem[-5, 0, 2] @test Vector(A, 6) == ZZRingElem[-5, 0, 2, -10, 1, 0] + @test dense_row(A, 3) == matrix(FlintZZ, 1, 3, [-5, 0, 2]) + @test dense_row(A, 6) == matrix(FlintZZ, 1, 6, [-5, 0, 2, -10, 1, 0]) + @test sparse_row(dense_row(A, 6)) == A end From 55ae7affbc75586ae0818b937a77aaf682c64d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 11:47:53 +0200 Subject: [PATCH 6/9] Fix import --- src/Sparse/Solve.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Sparse/Solve.jl b/src/Sparse/Solve.jl index 8a7d315a39..c025033cc2 100644 --- a/src/Sparse/Solve.jl +++ b/src/Sparse/Solve.jl @@ -1,3 +1,5 @@ +import Nemo: can_solve, can_solve_with_solution + function can_solve_ut(A::SMat{T}, g::SRow{T}) where T <: Union{FieldElem, zzModRingElem} # Works also for non-square matrices #@hassert :HNF 1 ncols(A) == nrows(A) From 93de83db112adc77c9e52dba19ca51b2d65e3242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 13:30:52 +0200 Subject: [PATCH 7/9] Add more consistent multiplication --- src/Sparse/Matrix.jl | 26 +++++++++++++------------- src/Sparse/Row.jl | 28 +++++++++++++++++++++++++--- test/Sparse/Matrix.jl | 13 +++++++++++++ test/Sparse/Row.jl | 4 +++- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index c583dde9d6..ca9e3081aa 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -691,37 +691,37 @@ end # ################################################################################ -function *(b::T, A::SMat{T}) where {T <: RingElem} - B = sparse_matrix(base_ring(A), 0, ncols(A)) +function *(b::T, A::SMat{T}) where T + B = sparse_matrix(base_ring(A), nrows(A), ncols(A)) if iszero(b) return B end - for a = A + for a in A push!(B, b*a) end return B end -function *(b::Integer, A::SMat{T}) where T +function *(b::Union{NCRingElem,RingElement}, A::SMat{T}) where T return base_ring(A)(b)*A end -function *(b::ZZRingElem, A::SMat{T}) where {T <: RingElement} - return base_ring(A)(b)*A -end - -function *(b::ZZRingElem, A::SMat{ZZRingElem}) +function *(A::SMat{T}, b::T) where T + B = sparse_matrix(base_ring(A), nrows(A), ncols(A)) if iszero(b) - return zero_matrix(SMat, FlintZZ, nrows(A), ncols(A)) + return B end - B = sparse_matrix(base_ring(A)) - B.c = ncols(A) for a in A - push!(B, b * a) + push!(B, a*b) end return B end +function *(A::SMat{T}, b::Union{NCRingElem,RingElement}) where T + return A*base_ring(A)(b) +end + + ################################################################################ # # Submatrix diff --git a/src/Sparse/Row.jl b/src/Sparse/Row.jl index fc6a587563..a06738b35f 100644 --- a/src/Sparse/Row.jl +++ b/src/Sparse/Row.jl @@ -476,8 +476,8 @@ function *(b::T, A::SRow{T}) where T if iszero(b) return B end - for (p,v) = A - nv = v*b + for (p,v) in A + nv = b*v if !iszero(nv) # there are zero divisors - potentially push!(B.pos, p) push!(B.values, nv) @@ -486,13 +486,35 @@ function *(b::T, A::SRow{T}) where T return B end -function *(b::Integer, A::SRow{T}) where T +function *(b::Union{NCRingElem,RingElement}, A::SRow{T}) where T if length(A.values) == 0 return sparse_row(base_ring(A)) end return base_ring(A)(b)*A end +function *(A::SRow{T}, b::T) where T + B = sparse_row(parent(b)) + if iszero(b) + return B + end + for (p,v) in A + nv = v*b + if !iszero(nv) # there are zero divisors - potentially + push!(B.pos, p) + push!(B.values, nv) + end + end + return B +end + +function *(A::SRow{T}, b::Union{NCRingElem,RingElement}) where T + if length(A.values) == 0 + return sparse_row(base_ring(A)) + end + return A*base_ring(A)(b) +end + function div(A::SRow{T}, b::T) where T B = sparse_row(base_ring(A)) if iszero(b) diff --git a/test/Sparse/Matrix.jl b/test/Sparse/Matrix.jl index 6d7cd2ed9f..47dd6c0b6b 100644 --- a/test/Sparse/Matrix.jl +++ b/test/Sparse/Matrix.jl @@ -177,19 +177,32 @@ using Hecke.SparseArrays E = @inferred 0 * D @test E == zero_matrix(SMat, FlintZZ, 3) @test E == sparse_matrix(FlintZZ, 3, 3) + E = @inferred D * 0 + @test E == zero_matrix(SMat, FlintZZ, 3) + @test E == sparse_matrix(FlintZZ, 3, 3) E = @inferred BigInt(2) * D @test E == sparse_matrix(FlintZZ, [2 10 6; 0 0 0; 0 2 0]) + E = @inferred D * BigInt(2) + @test E == sparse_matrix(FlintZZ, [2 10 6; 0 0 0; 0 2 0]) E = @inferred ZZRingElem(2) * D @test E == sparse_matrix(FlintZZ, [2 10 6; 0 0 0; 0 2 0]) + E = @inferred D * ZZRingElem(2) + @test E == sparse_matrix(FlintZZ, [2 10 6; 0 0 0; 0 2 0]) R = residue_ring(FlintZZ, 6) D = sparse_matrix(R, [1 2 2; 0 0 1; 2 2 2]) E = @inferred ZZRingElem(3) * D @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) + E = @inferred D * ZZRingElem(3) + @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) E = @inferred Int(3) * D @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) + E = @inferred D * Int(3) + @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) E = @inferred R(3) * D @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) + E = @inferred D * R(3) + @test E == sparse_matrix(R, [3 0 0; 0 0 3; 0 0 0]) # Submatrix diff --git a/test/Sparse/Row.jl b/test/Sparse/Row.jl index 24f95b203e..3a0cc489d4 100644 --- a/test/Sparse/Row.jl +++ b/test/Sparse/Row.jl @@ -100,7 +100,9 @@ for T in [Int, BigInt, ZZRingElem] b = T(2) B = @inferred b * A - @test B == map_entries(x -> T(2) * x, A) + @test B == map_entries(x -> b * x, A) + B = @inferred A * b + @test B == map_entries(x -> x * b, A) b = T(2) B = @inferred div(A, b) From aeb3f9b79ff1ca31ee246d42fa78fdb73bd388ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 14:02:26 +0200 Subject: [PATCH 8/9] Fix infinite loop --- src/Sparse/Matrix.jl | 4 ++-- src/Sparse/Row.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index ca9e3081aa..a0bf484898 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -702,7 +702,7 @@ function *(b::T, A::SMat{T}) where T return B end -function *(b::Union{NCRingElem,RingElement}, A::SMat{T}) where T +function *(b, A::SMat) return base_ring(A)(b)*A end @@ -717,7 +717,7 @@ function *(A::SMat{T}, b::T) where T return B end -function *(A::SMat{T}, b::Union{NCRingElem,RingElement}) where T +function *(A::SMat, b) return A*base_ring(A)(b) end diff --git a/src/Sparse/Row.jl b/src/Sparse/Row.jl index a06738b35f..79a10c479f 100644 --- a/src/Sparse/Row.jl +++ b/src/Sparse/Row.jl @@ -486,7 +486,7 @@ function *(b::T, A::SRow{T}) where T return B end -function *(b::Union{NCRingElem,RingElement}, A::SRow{T}) where T +function *(b, A::SRow) if length(A.values) == 0 return sparse_row(base_ring(A)) end @@ -508,7 +508,7 @@ function *(A::SRow{T}, b::T) where T return B end -function *(A::SRow{T}, b::Union{NCRingElem,RingElement}) where T +function *(A::SRow, b) if length(A.values) == 0 return sparse_row(base_ring(A)) end From d9ca7a5b07243d7edae01c197cc80e43ee3d0548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 21 Sep 2023 15:55:55 +0200 Subject: [PATCH 9/9] Fix multiplication --- src/Sparse/Matrix.jl | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index a0bf484898..54912301da 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -691,37 +691,36 @@ end # ################################################################################ -function *(b::T, A::SMat{T}) where T - B = sparse_matrix(base_ring(A), nrows(A), ncols(A)) +function *(b::T, A::SMat{T}) where {T} if iszero(b) - return B + return sparse_matrix(base_ring(A), nrows(A), ncols(A)) end + B = sparse_matrix(base_ring(A), 0, ncols(A)) for a in A - push!(B, b*a) + push!(B, b * a) end return B end function *(b, A::SMat) - return base_ring(A)(b)*A + return base_ring(A)(b) * A end -function *(A::SMat{T}, b::T) where T - B = sparse_matrix(base_ring(A), nrows(A), ncols(A)) +function *(A::SMat{T}, b::T) where {T} if iszero(b) - return B + return sparse_matrix(base_ring(A), nrows(A), ncols(A)) end + B = sparse_matrix(base_ring(A), 0, ncols(A)) for a in A - push!(B, a*b) + push!(B, a * b) end return B end function *(A::SMat, b) - return A*base_ring(A)(b) + return A * base_ring(A)(b) end - ################################################################################ # # Submatrix