Skip to content

Commit dfbc7d3

Browse files
ArunS-tackdkarraschbaggepinnen
authored andcommitted
Float16 input now gives Float 16 factors for svd,eigen and cholesky. (JuliaLang#41352)
Co-authored-by: Daniel Karrasch <[email protected]> Co-authored-by: Fredrik Bagge Carlson <[email protected]>
1 parent dfd2f42 commit dfbc7d3

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

stdlib/LinearAlgebra/src/cholesky.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ true
388388
cholesky(A::Union{StridedMatrix,RealHermSymComplexHerm{<:Real,<:StridedMatrix}},
389389
::Val{false}=Val(false); check::Bool = true) = cholesky!(cholcopy(A); check = check)
390390

391+
function cholesky(A::Union{StridedMatrix{Float16},RealHermSymComplexHerm{Float16,<:StridedMatrix}}, ::Val{false}=Val(false); check::Bool = true)
392+
X = cholesky!(cholcopy(A); check = check)
393+
return Cholesky{Float16}(X)
394+
end
395+
391396

392397
## With pivoting
393398
"""

stdlib/LinearAlgebra/src/eigen.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ function eigen(A::AbstractMatrix{T}; permute::Bool=true, scale::Bool=true, sortb
275275
isdiag(AA) && return eigen(Diagonal(AA); permute=permute, scale=scale, sortby=sortby)
276276
return eigen!(AA; permute=permute, scale=scale, sortby=sortby, jvl=jvl, jvr=jvr, jce=jce, jcv=jcv)
277277
end
278+
function eigen(A::AbstractMatrix{T}; permute::Bool=true, scale::Bool=true, sortby::Union{Function,Nothing}=eigsortby) where {T <: Union{Float16,Complex{Float16}}}
279+
AA = copy_oftype(A, eigtype(T))
280+
isdiag(AA) && return eigen(Diagonal(AA); permute=permute, scale=scale, sortby=sortby)
281+
A = eigen!(AA; permute, scale, sortby)
282+
values = convert(AbstractVector{isreal(A.values) ? Float16 : Complex{Float16}}, A.values)
283+
vectors = convert(AbstractMatrix{isreal(A.vectors) ? Float16 : Complex{Float16}}, A.vectors)
284+
vectorsl = convert(AbstractMatrix{isreal(A.vectors) ? Float16 : Complex{Float16}}, A.vectorsl)
285+
rconde = convert(Vector{Float16}, A.rconde)
286+
rcondv = convert(Vector{Float16}, A.rcondv)
287+
return Eigen(values, vectors, vectorsl, A.unitary, rconde, rcondv)
288+
end
278289
eigen(x::Number) = Eigen([x], fill(one(x), 1, 1))
279290

280291
"""

stdlib/LinearAlgebra/src/svd.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ true
175175
function svd(A::StridedVecOrMat{T}; full::Bool = false, alg::Algorithm = default_svd_alg(A)) where {T}
176176
svd!(copy_oftype(A, eigtype(T)), full = full, alg = alg)
177177
end
178+
function svd(A::StridedVecOrMat{T}; full::Bool = false, alg::Algorithm = default_svd_alg(A)) where {T <: Union{Float16,Complex{Float16}}}
179+
A = svd!(copy_oftype(A, eigtype(T)), full = full, alg = alg)
180+
return SVD{T}(A)
181+
end
178182
function svd(x::Number; full::Bool = false, alg::Algorithm = default_svd_alg(x))
179183
SVD(x == 0 ? fill(one(x), 1, 1) : fill(x/abs(x), 1, 1), [abs(x)], fill(one(x), 1, 1))
180184
end

stdlib/LinearAlgebra/test/cholesky.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,17 @@ end
483483
@test F\b == F'\b
484484
end
485485

486+
@testset "Float16" begin
487+
A = Float16[4. 12. -16.; 12. 37. -43.; -16. -43. 98.]
488+
B = cholesky(A)
489+
B32 = cholesky(Float32.(A))
490+
@test B isa Cholesky{Float16, Matrix{Float16}}
491+
@test B.U isa UpperTriangular{Float16, Matrix{Float16}}
492+
@test B.L isa LowerTriangular{Float16, Matrix{Float16}}
493+
@test B.UL isa UpperTriangular{Float16, Matrix{Float16}}
494+
@test B.U B32.U
495+
@test B.L B32.L
496+
@test B.UL B32.UL
497+
end
498+
486499
end # module TestCholesky

stdlib/LinearAlgebra/test/eigen.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,36 @@ end
177177
@test isequal(eigen(A), eigen(A))
178178
end
179179

180+
@testset "Float16" begin
181+
A = Float16[4. 12. -16.; 12. 37. -43.; -16. -43. 98.]
182+
B = eigen(A)
183+
B32 = eigen(Float32.(A))
184+
C = Float16[3 -2; 4 -1]
185+
D = eigen(C)
186+
D32 = eigen(Float32.(C))
187+
F = eigen(complex(C))
188+
F32 = eigen(complex(Float32.(C)))
189+
@test B isa Eigen{Float16, Float16, Matrix{Float16}, Vector{Float16}, Vector{Float16}}
190+
@test B.values isa Vector{Float16}
191+
@test B.vectors isa Matrix{Float16}
192+
@test B.vectorsl isa Matrix{Float16}
193+
@test B.values B32.values
194+
@test B.vectors B32.vectors
195+
@test B.vectorsl B32.vectorsl
196+
@test D isa Eigen{ComplexF16, ComplexF16, Matrix{ComplexF16}, Vector{ComplexF16}, Vector{Float16}}
197+
@test D.values isa Vector{ComplexF16}
198+
@test D.vectors isa Matrix{ComplexF16}
199+
@test D.vectorsl isa Matrix{ComplexF16}
200+
@test D.values D32.values
201+
@test D.vectors D32.vectors
202+
@test D.vectorsl D32.vectorsl
203+
@test F isa Eigen{ComplexF16, ComplexF16, Matrix{ComplexF16}, Vector{ComplexF16}, Vector{Float16}}
204+
@test F.values isa Vector{ComplexF16}
205+
@test F.vectors isa Matrix{ComplexF16}
206+
@test F.vectorsl isa Matrix{ComplexF16}
207+
@test F.values F32.values
208+
@test F.vectors F32.vectors
209+
@test F.vectorsl F32.vectorsl
210+
end
211+
180212
end # module TestEigen

stdlib/LinearAlgebra/test/svd.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,17 @@ end
248248
end
249249
end
250250

251+
@testset "Float16" begin
252+
A = Float16[4. 12. -16.; 12. 37. -43.; -16. -43. 98.]
253+
B = svd(A)
254+
B32 = svd(Float32.(A))
255+
@test B isa SVD{Float16, Float16, Matrix{Float16}}
256+
@test B.U isa Matrix{Float16}
257+
@test B.Vt isa Matrix{Float16}
258+
@test B.S isa Vector{Float16}
259+
@test B.U B32.U
260+
@test B.Vt B32.Vt
261+
@test B.S B32.S
262+
end
263+
251264
end # module TestSVD

0 commit comments

Comments
 (0)