i don’t believe such a function exists, but Julia provides all the tools to construct one.
julia> singlify(x::Float64) = Float32(x)
singlify (generic function with 1 method)
julia> singlify(x::Complex{Float64}) = Complex{Float32}(x)
singlify (generic function with 2 methods)
Broadcasting works quite nicely for applying the function elementwise to various different kinds of arrays:
julia> x = rand(2, 2)
2×2 Array{Float64,2}:
0.689747 0.539186
0.607853 0.858326
julia> singlify.(x)
2×2 Array{Float32,2}:
0.689747 0.539186
0.607853 0.858326
julia> x = sprand(2, 2, 0.5)
2×2 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
[1, 1] = 0.0518641
[2, 1] = 0.896834
[1, 2] = 0.0067949
[2, 2] = 0.57429
julia> singlify.(x)
2×2 SparseMatrixCSC{Float32,Int64} with 4 stored entries:
[1, 1] = 0.0518641
[2, 1] = 0.896834
[1, 2] = 0.0067949
[2, 2] = 0.57429
Unfortunately, Diagonals aren’t handled perfectly:
julia> x = Diagonal([1., 2])
2×2 Diagonal{Float64}:
1.0 ⋅
⋅ 2.0
julia> singlify.(x)
2×2 SparseMatrixCSC{Float32,Int64} with 2 stored entries:
[1, 1] = 1.0
[2, 2] = 2.0
The fact that broadcasting over a Diagonal
returns a plain sparse matrix is, fortunately, resolved in the latest nightly builds of Julia, so in the future, you should get a Diagonal
out.