Skip to content

set gfp_mat or nmod_mat entry with Int value#2301

Open
simonbrandhorst wants to merge 3 commits into
Nemocas:masterfrom
simonbrandhorst:master
Open

set gfp_mat or nmod_mat entry with Int value#2301
simonbrandhorst wants to merge 3 commits into
Nemocas:masterfrom
simonbrandhorst:master

Conversation

@simonbrandhorst
Copy link
Copy Markdown
Contributor

Before

julia> F = Hecke.Native.GF(17)
Finite field of characteristic 17

julia> A = F[1 2; 3 4];

julia> @btime A[1,1] = UInt(2);
  19.783 ns (0 allocations: 0 bytes)

julia> @btime A[1,1] = 2;
  44.078 ns (2 allocations: 32 bytes)

After

julia> @btime A[1,1] = 2;
  25.585 ns (0 allocations: 0 bytes)

Maybe not optimal, but in any case it is an improvement.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.30%. Comparing base (3a96b0c) to head (3b29c7e).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2301      +/-   ##
==========================================
- Coverage   88.36%   88.30%   -0.07%     
==========================================
  Files         102      102              
  Lines       37510    37526      +16     
==========================================
- Hits        33147    33136      -11     
- Misses       4363     4390      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Tobias271828
Copy link
Copy Markdown
Contributor

That's a good idea – optimising the code! Thanks!

I don’t quite understand your code, though: what do you need the function _nmod_reduce_int for? The method mod(::Int, ::UInt) returns a value >= 0 of type UInt. Hence, it should be enough to just paste this code into nmod_mat.jl:

@inline function setindex!(a::T, u::Int, i::Int, j::Int) where T <: Zmodn_mat
  @boundscheck _checkbounds(a, i, j)
  R = base_ring(a)
  setindex_raw!(a, mod(u, R.n), i, j)
end

It works on my machine, and I obtained this benchmarking result by using it.

julia> F = Native.GF(17)
Finite field of characteristic 17

julia> A = F[1 2; 3 4];

julia> @b A[1,1] = UInt(2)
15.700 ns

julia> @b A[1,1] = 2
14.675 ns

Comment thread src/flint/nmod_mat.jl Outdated
Comment thread src/flint/nmod_mat.jl Outdated
Co-authored-by: Simon Brandhorst <brandhorst@math.uni-sb.de>
@simonbrandhorst
Copy link
Copy Markdown
Contributor Author

You are of course right. Thanks!

Comment thread src/flint/nmod_mat.jl Outdated
Co-authored-by: Lars Göttgens <lars.goettgens@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants