Skip to content

Commit

Permalink
feat: inplace scalar multiplication for FqMatrix (#1797)
Browse files Browse the repository at this point in the history
(There exists no corresponding function in flint)
  • Loading branch information
thofma authored Jun 21, 2024
1 parent c60bc50 commit befc7d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/flint/fq_default_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,26 @@ function mul!(a::FqMatrix, b::FqMatrix, c::FqMatrix)
return a
end

function mul!(a::FqMatrix, b::FqMatrix, c::FqFieldElem)
F = base_ring(a)
if _fq_default_ctx_type(F) == _FQ_DEFAULT_NMOD
ccall((:nmod_mat_scalar_mul, libflint), Nothing, (Ref{FqMatrix}, Ref{FqMatrix}, UInt), a, b, UInt(lift(ZZ, c)))
return a
end
GC.@preserve a begin
for i in 1:nrows(a)
for j in 1:ncols(a)
x = fq_default_mat_entry_ptr(a, i, j)
y = fq_default_mat_entry_ptr(b, i, j)
ccall((:fq_default_mul, libflint), Nothing, (Ptr{FqFieldElem}, Ptr{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), x, y, c, F)
end
end
end
return a
end

mul!(a::FqMatrix, b::FqFieldElem, c::FqMatrix) = mul!(a, c, b)

function add!(a::FqMatrix, b::FqMatrix, c::FqMatrix)
ccall((:fq_default_mat_add, libflint), Nothing,
(Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}),
Expand Down
15 changes: 15 additions & 0 deletions test/flint/fq_default_mat-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ end
d = transpose(a)*a

@test d == matrix_space(F17, 4, 4)([11 11 8 7; 11 0 14 6; 8 14 14 5; 7 6 5 5])

a = F17[1 2 3 1; 3 2 1 2; 1 3 2 0]
z = F17(2)
b = similar(a)
@test mul!(b, a, z) == a * z
b = similar(a)
@test mul!(b, z, a) == a * z

F, _ = finite_field(ZZRingElem(17), 2, "a")
a = F[1 2 3 1; 3 2 1 2; 1 3 2 0]
z = F(2)
b = similar(a)
@test mul!(b, a, z) == a * z
b = similar(a)
@test mul!(b, z, a) == a * z
end

@testset "FqMatrix.row_col_swapping" begin
Expand Down

0 comments on commit befc7d7

Please sign in to comment.