From 6b789c7331b3c955151fc6a8e286e701ed87fae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Wed, 27 Sep 2023 17:18:01 +0200 Subject: [PATCH] Add `mul(::SMat, ::SMat)` --- docs/src/sparse/intro.md | 1 + src/Sparse/Matrix.jl | 19 +++++++++++++++++-- test/Sparse/Matrix.jl | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/src/sparse/intro.md b/docs/src/sparse/intro.md index 6015c9cb1e..d05289b07c 100644 --- a/docs/src/sparse/intro.md +++ b/docs/src/sparse/intro.md @@ -209,6 +209,7 @@ Hecke.mul(::SMat{T}, ::AbstractVector{T}) where {T} Hecke.mul(::SMat{T}, ::AbstractMatrix{T}) where {T} Hecke.mul(::SMat{T}, ::MatElem{T}) where {T} Hecke.mul(::SRow{T}, ::SMat{T}) where {T} +Hecke.mul(::SMat{T}, ::SMat{T}) where {T} ``` Other: diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index c510ebaa63..70a75f2949 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -570,7 +570,6 @@ function mul!(c::MatElem{T}, A::SMat{T}, b::MatElem{T}) where T end # - SMat{T} * MatElem{T} as MatElem{T} - @doc raw""" mul(A::SMat{T}, b::MatElem{T}) -> MatElem @@ -583,7 +582,6 @@ function mul(A::SMat{T}, b::MatElem{T}) where T end # - SRow{T} * SMat{T} as SRow{T} - @doc raw""" mul(A::SRow, B::SMat) -> SRow @@ -600,6 +598,23 @@ function mul(A::SRow{T}, B::SMat{T}) where T return C end +# - SMat{T} * SMat{T} as SMat{T} +@doc raw""" + mul(A::SMat, B::SMat) -> SMat + +Return the product $A\cdot B$ as a sparse matrix. +""" +function mul(A::SMat{T}, B::SMat{T}) where {T} + @req ncols(A) == nrows(B) "Matrices must have compatible dimensions" + @req base_ring(A) == base_ring(B) "Matrices must have same base ring" + C = sparse_matrix(base_ring(B), nrows(A), ncols(B)) + for (i, rA) in enumerate(A.rows) + rC = mul(rA, B) + C[i] = rC + end + return C +end + ################################################################################ # # Multiplication with reduction diff --git a/test/Sparse/Matrix.jl b/test/Sparse/Matrix.jl index a21589269c..a8c1985965 100644 --- a/test/Sparse/Matrix.jl +++ b/test/Sparse/Matrix.jl @@ -156,6 +156,11 @@ using Hecke.SparseArrays w = @inferred mul(v, D) @test w == sparse_row(FlintZZ) + D1 = sparse_matrix(FlintZZ, [81 0 2; 31 0 -5]) + D2 = sparse_matrix(FlintZZ, [12 403 -23; 0 0 122; -1 2 99]) + E = @inferred mul(D1, D2) + @test E == sparse_matrix(matrix(D1) * matrix(D2)) + # Addition D = sparse_matrix(FlintZZ, [1 5 3; 0 0 0; 0 1 0])