Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: [CartesianIndex...] sparse method #600

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kunzaatko
Copy link

@kunzaatko kunzaatko commented Feb 22, 2025

A method for specifying the indexes to fill with values directly as
CartesianIndices.

For example:

julia> sparse([CartesianIndex(1,1), CartesianIndex(2,2), CartesianIndex(3,3)], [1.0, 2.0, 3.0], 3, 3)

is now possible.

  • documentation?
  • Additional tests?

A method for specifying the indexes to fill with values directly as
CartesianIndices.
Copy link

codecov bot commented Feb 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.11%. Comparing base (ce852af) to head (0021a71).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #600      +/-   ##
==========================================
+ Coverage   84.08%   84.11%   +0.02%     
==========================================
  Files          12       12              
  Lines        9192     9195       +3     
==========================================
+ Hits         7729     7734       +5     
+ Misses       1463     1461       -2     

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

@kunzaatko
Copy link
Author

Another method that should be considered are ones that are created by using a range on instances of CartesianIndex

julia> CartesianIndex(3,4):CartesianIndex(4,9)
CartesianIndices((3:4, 4:9))

julia> CartesianIndex(3,4):CartesianIndex(4,9) |> typeof |> supertypes
(CartesianIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}, AbstractMatrix{CartesianIndex{2}}, Any)

@kunzaatko kunzaatko marked this pull request as ready for review February 22, 2025 09:16
@jishnub
Copy link
Collaborator

jishnub commented Feb 22, 2025

Note that the CartesianIndices block above is dense (although it might be useful as the indices of a block matrix). A sparse range along a 1D axis may be created as a StepRangeLen of CartesianIndexes.

@kunzaatko
Copy link
Author

kunzaatko commented Feb 23, 2025

Perhaps what you are describing is a more realistic use of the dense block. That was what I had in mind although the sparse function does not give an ideal interface for this use, am I correct? Something like this does not have much overhead thought (and maybe any overhead is even compiled out?)

julia> sparse(CartesianIndex(12:14):CartesianIndex(15:17), ones(3*3), 20, 20) + sparse(CartesianIndex(2:4):CartesianIndex(5:7), ones(3*3), 20, 20)

Which would create a sparse matrix with dense blocks inside. The structure of the type does not give the best memory usage for this case...

@@ -1111,6 +1111,11 @@ end
sparse(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer, combine) =
sparse(AbstractVector{Int}(I), AbstractVector{Int}(J), V, m, n, combine)

function sparse(IJ::AbstractVector{Ci}, V::AbstractVector, m::Integer, n::Integer) where {Ci<:CartesianIndex}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you require 2D CartesianIndexes here ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely correct. Fixed this.

NOTE: `CartesianIndex{2}` is no longer an abstract type so the function
no longer needs to be parametric.
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.

2 participants