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

Simplify show implementation #31

Merged
merged 1 commit into from
Feb 28, 2025
Merged

Simplify show implementation #31

merged 1 commit into from
Feb 28, 2025

Conversation

lkdvos
Copy link
Contributor

@lkdvos lkdvos commented Feb 12, 2025

This PR simplifies a bit of the code used for defining show by leveraging the built-in Base functionality of replacing values for printing.
This shouldn't change the functionality, although it does change a bit of the exact alignment properties.
In particular, here, the zero-value print is used to determine things like alignment, and only afterwards is this replaced with a centered dot, while I think the previous implementation first replaced the entries with dots, and only then computed alignments.

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.36%. Comparing base (1e54b85) to head (4216bd8).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
- Coverage   72.46%   71.36%   -1.11%     
==========================================
  Files           7        7              
  Lines         454      433      -21     
==========================================
- Hits          329      309      -20     
+ Misses        125      124       -1     
Flag Coverage Δ
docs 37.44% <100.00%> (-2.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@mtfishman
Copy link
Member

Does this work for higher dimensional arrays as well?

@lkdvos
Copy link
Contributor Author

lkdvos commented Feb 12, 2025

Yes, they recursively make views that they call print_matrix on. (don't mind the sprand, PR incoming for that)

julia> A = SparseArraysBase.sprand(Float64, (3,3,3), 0.2)
3×3×3 SparseArrayDOK{Float64, 3, typeof(SparseArraysBase.default_getunstoredindex)}:
[:, :, 1] =
     0.884028   
               
               

[:, :, 2] =
               
               
     0.859288   

[:, :, 3] =
     0.716633  0.826796
               
               

@mtfishman
Copy link
Member

Great, I think I tried to dig into Base.show to see how SparseArrays.jl did it but it wasn't clear to me if it generalized beyond matrices. Nice to see all of that code removed! Can you bump the version?

@lkdvos
Copy link
Contributor Author

lkdvos commented Feb 12, 2025

Honestly, I still think we should use the SparseArrayKit approach of simply printing all non-zero entries manually, because this implementation will still flood the terminal for N-D arrays that are large but sparse.
https://github.com/Jutho/SparseArrayKit.jl/blob/9a7c0a25b6ce6977c195c29ff119a96415da5685/src/sparsearray.jl#L174-L205
Possibly we could even support dynamically switching between the two, but that will be for a follow-up PR.

@mtfishman
Copy link
Member

Honestly, I still think we should use the SparseArrayKit approach of simply printing all non-zero entries manually, because this implementation will still flood the terminal for N-D arrays that are large but sparse. https://github.com/Jutho/SparseArrayKit.jl/blob/9a7c0a25b6ce6977c195c29ff119a96415da5685/src/sparsearray.jl#L174-L205 Possibly we could even support dynamically switching between the two, but that will be for a follow-up PR.

I like the idea of switching over to just listing the stored values after a certain size (say once Julia would start to not show all of the values anyway).

@lkdvos
Copy link
Contributor Author

lkdvos commented Feb 13, 2025

This breaks a downstream BlockSparseArrays test, I didn't realize that the structure was being explicitly used there. Let me fix that first

@mtfishman
Copy link
Member

This breaks a downstream BlockSparseArrays test, I didn't realize that the structure was being explicitly used there. Let me fix that first

Oh yeah, thanks.

@lkdvos
Copy link
Contributor Author

lkdvos commented Feb 13, 2025

I bumped to v0.3.0, but since #33 is also breaking, we might want to bundle these together

@lkdvos
Copy link
Contributor Author

lkdvos commented Feb 28, 2025

Can be merged after #33

@mtfishman mtfishman mentioned this pull request Feb 28, 2025
@lkdvos lkdvos merged commit 3ff1528 into main Feb 28, 2025
12 checks passed
@lkdvos lkdvos deleted the show branch February 28, 2025 14:24
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