Skip to content

Commit

Permalink
Make LinearAlgebra.haszero public (#56223)
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
jishnub authored Oct 27, 2024
1 parent 446d20f commit fcf7ec0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Standard library changes
Custom array types may specialize this function to return an appropriate result ([#55252]).
* The matrix multiplication `A * B` calls `matprod_dest(A, B, T::Type)` to generate the destination.
This function is now public ([#55537]).
* The function `haszero(T::Type)` is used to check if a type `T` has a unique zero element defined as `zero(T)`.
This is now public.

#### Logging

Expand Down
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ LinearAlgebra.LAPACK.hseqr!

```@docs
LinearAlgebra.matprod_dest
LinearAlgebra.haszero
```

```@meta
Expand Down
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export
public AbstractTriangular,
Givens,
checksquare,
haszero,
hermitian,
hermitian_type,
isbanded,
Expand Down
12 changes: 12 additions & 0 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ norm2(x::Union{Array{T},StridedVector{T}}) where {T<:BlasFloat} =
length(x) < NRM2_CUTOFF ? generic_norm2(x) : BLAS.nrm2(x)

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

2 comments on commit fcf7ec0

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.