Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/cusolver/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ function LinearAlgebra.eigen(A::CuMatrix{T}) where {T<:BlasComplex}
ishermitian(A) ? Eigen(heevd!('V', 'U', A2)...) : error("GPU eigensolver supports only Hermitian or Symmetric matrices.")
end

# matrix functions
for func in (:(Base.exp), :(Base.cos), :(Base.sin), :(Base.tan), :(Base.cosh), :(Base.sinh), :(Base.tanh), :(Base.atan), :(Base.asinh), :(Base.atanh), :(Base.cbrt))
@eval begin
function ($func)(A::Symmetric{T, <:StridedCuMatrix}) where {T<:BlasReal}
F = eigen(A)
return Symmetric((F.vectors * Diagonal(($func).(F.values))) * F.vectors')
end
function ($func)(A::Hermitian{T, <:StridedCuMatrix}) where {T<:BlasReal}
F = eigen(A)
return Hermitian((F.vectors * Diagonal(($func).(F.values))) * F.vectors')
end
function ($func)(A::Hermitian{<:Complex, <:StridedCuMatrix})
F = eigen(A)
retmat = (F.vectors * Diagonal(($func).(F.values))) * F.vectors'
@static if VERSION >= v"1.11"
d_ixs = diagind(retmat, IndexStyle(retmat))
else
d_ixs = diagind(retmat)
end
@. retmat[d_ixs] = real(retmat[d_ixs])
return Hermitian(retmat)
end
end
end

# factorizations

Expand Down
16 changes: 16 additions & 0 deletions test/libraries/cusolver/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,19 @@ end
@inferred d_A \ d_b
end
end

@testset "Hermitian/Symmetric matrix functions, elty = $elty" for elty in [Float32, Float64, ComplexF32, ComplexF64]
A = rand(elty, n, n)
Ah = A * A' # make posdef for atan, asinh, atanh
d_Ah = CuArray(Ah)
@testset for func in (exp, cos, sin, tan, cosh, sinh, tanh, atan, asinh)
@test Array(func(d_Ah)) ≈ func(Ah)
end
@static if VERSION >= v"1.11.0" # not supported on 1.10 or for Complex
if elty <: Real
@testset for func in (cbrt,) # have to dispatch explicitly
@test Array(parent(func(Hermitian(d_Ah)))) ≈ func(Ah)
end
end
end
end