Skip to content

Honour T and the documented rfn forms in sprand with a value function - #1109

Merged
ViralBShah merged 3 commits into
mainfrom
vs/sprand-rfn-eltype
Oct 26, 2026
Merged

ViralBShah merged 3 commits into
mainfrom
vs/sprand-rfn-eltype

Conversation

@ViralBShah

@ViralBShah ViralBShah commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

sprand with a value function rfn mishandles T. Without an RNG, sprand(m, n, p, rfn, T) ignores T. Without T, it calls rfn extra times just to find the eltype. The vector sprand([rng], n, p, rfn, T) rejects the documented rfn(k) and rfn(rng, k): it only accepts an undocumented rfn(rng, T, k).

Before

julia> calls = Ref(0); rfn = k -> (calls[] += 1; rand(Int8, k));

julia> eltype(sprand(3, 3, 0.5, rfn, Float64)), calls[]
(Int8, 2)

julia> calls[] = 0; eltype(sprand(3, 3, 0.5, rfn)), calls[]
(Int8, 3)

julia> typeof(sprand(10, 0.5, k -> rand(Int8, k), Float64))
ERROR: MethodError: no method matching (::var"#5#6")(::Random.TaskLocalRNG, ::Type{Float64}, ::Int64)

After

julia> eltype(sprand(3, 3, 0.5, rfn, Float64)), calls[]
(Float64, 1)

julia> calls[] = 0; eltype(sprand(3, 3, 0.5, rfn)), calls[]
(Int8, 1)

julia> typeof(sprand(10, 0.5, k -> rand(Int8, k), Float64))
SparseVector{Float64, Int64}

The matrix method without an RNG now passes T on. Without T, the matrix methods take the eltype from the values rfn returns, instead of a throwaway call rfn(1) that also drew from the RNG. The vector methods with T now call the documented rfn(k) or rfn(rng, k) and convert to T, as the matrix methods do. They keep accepting rfn(rng, T, k), checked with applicable. The docstring now says what T after rfn does.

Changed stream: seeded sprand([rng], m, n, p, rfn) calls with a custom rfn and no T draw less from the RNG, so their values change. Every other form passes T itself and is unchanged: sprand([rng], [T], m, n, p), sprandn and the vector forms, sprand(Xoshiro(42), 50, 50, 0.3) among them, which is bitwise identical to main.

🤖 Generated with Claude Code

…method

Without an RNG, `sprand` with a value function dropped `T`, so the
result took the eltype of `rfn` and `rfn` was called once more to
recompute the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l in sprand

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ViralBShah
ViralBShah force-pushed the vs/sprand-rfn-eltype branch from b306f92 to 41c35d7 Compare October 26, 2026 13:48
@ViralBShah ViralBShah changed the title Pass the element type T of sprand(m, n, p, rfn, T) on to the RNG method Honour T and the documented rfn forms in sprand with a value function Oct 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.73%. Comparing base (353601e) to head (2f742f6).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1109   +/-   ##
=======================================
  Coverage   94.72%   94.73%           
=======================================
  Files          16       16           
  Lines       10318    10337   +19     
=======================================
+ Hits         9774     9793   +19     
  Misses        544      544           

☔ View full report in Codecov by Harness.
📢 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.

@ViralBShah
ViralBShah merged commit daa0ac0 into main Oct 26, 2026
12 checks passed
@ViralBShah
ViralBShah deleted the vs/sprand-rfn-eltype branch October 26, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant