@@ -47,7 +47,7 @@ SparseArray{T, N}(::UndefInitializer, dims::NTuple{N, Int}) where {T, N} =
4747function SparseArray (A:: AbstractArray{T, N} ; atol:: Real = 0 ) where {T, N}
4848 sparse_array = SparseArray {T, N} (size (A))
4949 zero_val = zero (T)
50-
50+
5151 for I in CartesianIndices (A)
5252 val = A[I]
5353 # Only store non-zero values (with tolerance for floating point)
@@ -159,7 +159,6 @@ Return the sparsity ratio (fraction of zero elements) of the array.
159159"""
160160sparsity (A:: SparseArray ) = 1.0 - nnz (A) / length (A)
161161
162-
163162# Display (basic version - improved version defined later)
164163
165164# Basic arithmetic operations
@@ -256,16 +255,16 @@ end
256255Find all stored indices where function `f` returns true.
257256Only searches among explicitly stored values.
258257"""
259- function Base. findall (f:: F , A:: SparseArray ) where {F<: Function }
258+ function Base. findall (f:: F , A:: SparseArray ) where {F <: Function }
260259 result = CartesianIndex{ndims (A)}[]
261-
260+
262261 # Check only stored values
263262 for (idx, val) in A. data
264263 if f (val)
265264 push! (result, idx)
266265 end
267266 end
268-
267+
269268 return result
270269end
271270
@@ -283,10 +282,10 @@ Element-wise addition of two sparse arrays.
283282"""
284283function Base.:+ (A:: SparseArray{T, N} , B:: SparseArray{S, N} ) where {T, S, N}
285284 size (A) == size (B) || throw (DimensionMismatch (" Array dimensions must match" ))
286-
285+
287286 R = promote_type (T, S)
288287 result = SparseArray {R, N} (size (A))
289-
288+
290289 # Add elements from A
291290 for (idx, val_a) in A. data
292291 if haskey (B. data, idx)
@@ -297,15 +296,15 @@ function Base.:+(A::SparseArray{T, N}, B::SparseArray{S, N}) where {T, S, N}
297296 result. data[idx] = val_a
298297 end
299298 end
300-
299+
301300 # Add elements from B that aren't in A
302301 for (idx, val_b) in B. data
303302 if ! haskey (A. data, idx)
304303 # Only B has this index (A is effectively zero here)
305304 result. data[idx] = val_b
306305 end
307306 end
308-
307+
309308 return result
310309end
311310
@@ -316,10 +315,10 @@ Element-wise subtraction of two sparse arrays.
316315"""
317316function Base.:- (A:: SparseArray{T, N} , B:: SparseArray{S, N} ) where {T, S, N}
318317 size (A) == size (B) || throw (DimensionMismatch (" Array dimensions must match" ))
319-
318+
320319 R = promote_type (T, S)
321320 result = SparseArray {R, N} (size (A))
322-
321+
323322 # Subtract elements
324323 for (idx, val_a) in A. data
325324 if haskey (B. data, idx)
@@ -333,7 +332,7 @@ function Base.:-(A::SparseArray{T, N}, B::SparseArray{S, N}) where {T, S, N}
333332 result. data[idx] = val_a
334333 end
335334 end
336-
335+
337336 # Handle elements only in B
338337 for (idx, val_b) in B. data
339338 if ! haskey (A. data, idx)
@@ -344,7 +343,7 @@ function Base.:-(A::SparseArray{T, N}, B::SparseArray{S, N}) where {T, S, N}
344343 end
345344 end
346345 end
347-
346+
348347 return result
349348end
350349
@@ -356,49 +355,49 @@ Scalar multiplication of sparse array.
356355function Base.:* (A:: SparseArray{T, N} , scalar:: Number ) where {T, N}
357356 S = promote_type (T, typeof (scalar))
358357 result = SparseArray {S, N} (size (A))
359-
358+
360359 if scalar != 0
361360 for (idx, val) in A. data
362361 result. data[idx] = val * scalar
363362 end
364363 end
365-
364+
366365 return result
367366end
368367
369368Base.:* (scalar:: Number , A:: SparseArray ) = A * scalar
370369
371370# Base show method (without MIME) - delegates to text/plain
372371function Base. show (io:: IO , A:: SparseArray{T, N} ) where {T, N}
373- show (io, MIME " text/plain" (), A)
372+ return show (io, MIME " text/plain" (), A)
374373end
375374
376375# Improved display with better formatting
377376function Base. show (io:: IO , :: MIME"text/plain" , A:: SparseArray{T, N} ) where {T, N}
378377 compact = get (io, :compact , false )
379-
378+
380379 if compact
381380 print (io, " $(size (A)) SparseArray{$T , $N }" )
382381 return
383382 end
384-
383+
385384 stored_count = nnz (A)
386385 total_elements = length (A)
387- sparsity_pct = round (sparsity (A) * 100 , digits= 2 )
388-
386+ sparsity_pct = round (sparsity (A) * 100 , digits = 2 )
387+
389388 println (io, " $(size (A)) SparseArray{$T , $N } with $stored_count stored entries:" )
390389 println (io, " Sparsity: $sparsity_pct % ($(total_elements - stored_count) zeros)" )
391-
390+
392391 if stored_count > 0
393392 # Show up to 10 entries, sorted by index
394393 sorted_pairs = sort (collect (stored_pairs (A)), by = x -> x[1 ])
395394 display_count = min (10 , length (sorted_pairs))
396-
395+
397396 for i in 1 : display_count
398397 idx, val = sorted_pairs[i]
399398 println (io, " $idx => $val " )
400399 end
401-
400+
402401 if stored_count > 10
403402 println (io, " ⋮" )
404403 println (io, " ($(stored_count - 10 ) more entries)" )
@@ -420,11 +419,11 @@ function dropstored!(A::SparseArray, val)
420419 push! (to_delete, idx)
421420 end
422421 end
423-
422+
424423 for idx in to_delete
425424 delete! (A. data, idx)
426425 end
427-
426+
428427 return A
429428end
430429
@@ -470,7 +469,7 @@ stored_pairs(A::SparseArray) = pairs(A.data)
470469
471470Convert sparse array to dense array, filling unset indices with zero(T).
472471"""
473- function to_dense (A:: SparseArray{T} ) where T
472+ function to_dense (A:: SparseArray{T} ) where {T}
474473 dense = fill (zero (T), size (A))
475474 for (idx, val) in A. data
476475 dense[idx] = val
0 commit comments