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

Should LinearAlgebra.fzero be declared public? #49033

Open
jishnub opened this issue Mar 17, 2023 · 3 comments
Open

Should LinearAlgebra.fzero be declared public? #49033

jishnub opened this issue Mar 17, 2023 · 3 comments
Labels
domain:broadcast Applying a function over a collection domain:linear algebra Linear algebra

Comments

@jishnub
Copy link
Contributor

jishnub commented Mar 17, 2023

fzero(x::Number) = x
fzero(::Type{T}) where T = T
fzero(r::Ref) = r[]
fzero(t::Tuple{Any}) = t[1]
fzero(S::StructuredMatrix) = zero(eltype(S))
fzero(x) = missing
function fzero(bc::Broadcast.Broadcasted)
args = map(fzero, bc.args)
return any(ismissing, args) ? missing : bc.f(args...)
end

This method is useful in general, as it allows custom types to hook into the structure-preserving broadcast mechanism without having to define their own methods for each function. As an example:

julia> using FillArrays, LinearAlgebra

julia> Diagonal(1:4) .+ Zeros(4,4)
4×4 Matrix{Float64}:
 1.0  0.0  0.0  0.0
 0.0  2.0  0.0  0.0
 0.0  0.0  3.0  0.0
 0.0  0.0  0.0  4.0

julia> LinearAlgebra.fzero(x::Zeros) = zero(eltype(x))

julia> Diagonal(1:4) .+ Zeros(4,4)
4×4 Diagonal{Float64, Vector{Float64}}:
 1.0            
     2.0        
         3.0    
             4.0

Perhaps this could also be done through an isstructuredmatrix trait that is made public.

cc: @dlfivefifty @dkarrasch

@dlfivefifty
Copy link
Contributor

What is meant by "declared public"? I think it should be fine if FillArrays.jl uses this now.

@jishnub
Copy link
Contributor Author

jishnub commented Mar 28, 2023

As in, a guarantee that this won't be removed in the next minor release

@dlfivefifty
Copy link
Contributor

StdLib can have breaking changes in 1.x releases so there is no such thing as a "guarantee". Only Base is guaranteed to be non-breaking.

I think we should just use this in FillArrays.jl. I'm about to tag a breaking release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:broadcast Applying a function over a collection domain:linear algebra Linear algebra
Projects
None yet
Development

No branches or pull requests

3 participants