Skip to content

Commit fcf7ec0

Browse files
authored
Make LinearAlgebra.haszero public (#56223)
The trait `haszero` is used to check if a type `T` has a unique zero defined using `zero(T)`. This lets us dispatch to optimized paths without losing generality. This PR makes the function public so that this may be extended by packages (such as `StaticArrays`).
1 parent 446d20f commit fcf7ec0

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ Standard library changes
150150
Custom array types may specialize this function to return an appropriate result ([#55252]).
151151
* The matrix multiplication `A * B` calls `matprod_dest(A, B, T::Type)` to generate the destination.
152152
This function is now public ([#55537]).
153+
* The function `haszero(T::Type)` is used to check if a type `T` has a unique zero element defined as `zero(T)`.
154+
This is now public.
153155

154156
#### Logging
155157

stdlib/LinearAlgebra/docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ LinearAlgebra.LAPACK.hseqr!
895895

896896
```@docs
897897
LinearAlgebra.matprod_dest
898+
LinearAlgebra.haszero
898899
```
899900

900901
```@meta

stdlib/LinearAlgebra/src/LinearAlgebra.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export
169169
public AbstractTriangular,
170170
Givens,
171171
checksquare,
172+
haszero,
172173
hermitian,
173174
hermitian_type,
174175
isbanded,

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ norm2(x::Union{Array{T},StridedVector{T}}) where {T<:BlasFloat} =
108108
length(x) < NRM2_CUTOFF ? generic_norm2(x) : BLAS.nrm2(x)
109109

110110
# Conservative assessment of types that have zero(T) defined for themselves
111+
"""
112+
haszero(T::Type)
113+
114+
Return whether a type `T` has a unique zero element defined using `zero(T)`.
115+
If a type `M` specializes `zero(M)`, it may also choose to set `haszero(M)` to `true`.
116+
By default, `haszero` is assumed to be `false`, in which case the zero elements
117+
are deduced from values rather than the type.
118+
119+
!!! note
120+
`haszero` is a conservative check that is used to dispatch to
121+
optimized paths. Extending it is optional, but encouraged.
122+
"""
111123
haszero(::Type) = false
112124
haszero(::Type{T}) where {T<:Number} = isconcretetype(T)
113125
haszero(::Type{Union{Missing,T}}) where {T<:Number} = haszero(T)

0 commit comments

Comments
 (0)