@@ -514,15 +514,21 @@ function add!(A::NDSparseArray{T, N}, B::NDSparseArray{S, N}) where {T, S, N}
514514end
515515
516516"""
517- add!(A::NDSparseArray, scalar::Number )
517+ add!(A::NDSparseArray, scalar)
518518
519519In-place addition of scalar to all stored elements in sparse array `A`.
520520More efficient than `A = A + scalar`.
521- """
522- function add! (A:: NDSparseArray{T} , scalar:: Number ) where {T}
523- # If scalar is zero, no operation needed
524- if scalar == zero (typeof (scalar))
525- return A
521+ Works with any type that supports addition with the array's element type.
522+ """
523+ function add! (A:: NDSparseArray{T} , scalar) where {T}
524+ # For generic types, we can't assume zero() exists, so we try to check if it's zero
525+ # For most numeric types this will work, for others we'll skip the check
526+ try
527+ if scalar == zero (typeof (scalar))
528+ return A
529+ end
530+ catch MethodError
531+ # zero() not defined for this type, continue with the operation
526532 end
527533
528534 # Add scalar to all stored values, converting to A's type
@@ -568,15 +574,21 @@ function sub!(A::NDSparseArray{T, N}, B::NDSparseArray{S, N}) where {T, S, N}
568574end
569575
570576"""
571- sub!(A::NDSparseArray, scalar::Number )
577+ sub!(A::NDSparseArray, scalar)
572578
573579In-place subtraction of scalar from all stored elements in sparse array `A`.
574580More efficient than `A = A - scalar`.
575- """
576- function sub! (A:: NDSparseArray{T} , scalar:: Number ) where {T}
577- # If scalar is zero, no operation needed
578- if scalar == zero (typeof (scalar))
579- return A
581+ Works with any type that supports subtraction with the array's element type.
582+ """
583+ function sub! (A:: NDSparseArray{T} , scalar) where {T}
584+ # For generic types, we can't assume zero() exists, so we try to check if it's zero
585+ # For most numeric types this will work, for others we'll skip the check
586+ try
587+ if scalar == zero (typeof (scalar))
588+ return A
589+ end
590+ catch MethodError
591+ # zero() not defined for this type, continue with the operation
580592 end
581593
582594 # Subtract scalar from all stored values, converting to A's type
@@ -589,21 +601,30 @@ function sub!(A::NDSparseArray{T}, scalar::Number) where {T}
589601end
590602
591603"""
592- mul!(A::NDSparseArray, scalar::Number )
604+ mul!(A::NDSparseArray, scalar)
593605
594606In-place scalar multiplication of sparse array `A`.
595607More efficient than `A = A * scalar`.
596- """
597- function mul! (A:: NDSparseArray{T} , scalar:: Number ) where {T}
598- # If scalar is zero, clear all elements
599- if scalar == zero (typeof (scalar))
600- empty! (A. data)
601- return A
608+ Works with any type that supports multiplication with the array's element type.
609+ """
610+ function mul! (A:: NDSparseArray{T} , scalar) where {T}
611+ # For generic types, we can't assume zero() or one() exist, so we try to check
612+ # For most numeric types this will work, for others we'll skip the check
613+ try
614+ if scalar == zero (typeof (scalar))
615+ empty! (A. data)
616+ return A
617+ end
618+ catch MethodError
619+ # zero() not defined for this type, continue
602620 end
603621
604- # If scalar is one, no operation needed
605- if scalar == one (typeof (scalar))
606- return A
622+ try
623+ if scalar == one (typeof (scalar))
624+ return A
625+ end
626+ catch MethodError
627+ # one() not defined for this type, continue
607628 end
608629
609630 # Multiply all stored values, converting scalar to A's type
0 commit comments