Skip to content

Commit bfcd3e9

Browse files
authored
sincos for non-float symmetric matrices (JuliaLang#56484)
Ensures that the `eltype` of the array to which the result of `sincos` is a floating-point one, even if the argument doesn't have a floating-point `eltype`. After this, the following works: ```julia julia> A = diagm(0=>1:3) 3×3 Matrix{Int64}: 1 0 0 0 2 0 0 0 3 julia> sincos(A) ([0.8414709848078965 0.0 0.0; 0.0 0.9092974268256817 0.0; 0.0 0.0 0.1411200080598672], [0.5403023058681398 0.0 0.0; 0.0 -0.4161468365471424 0.0; 0.0 0.0 -0.9899924966004454]) ```
1 parent 2f58a4b commit bfcd3e9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

stdlib/LinearAlgebra/src/symmetric.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ for (hermtype, wrapper) in [(:Symmetric, :Symmetric), (:SymTridiagonal, :Symmetr
969969
function sincos(A::$hermtype{<:Real})
970970
n = checksquare(A)
971971
F = eigen(A)
972-
S, C = Diagonal(similar(A, (n,))), Diagonal(similar(A, (n,)))
972+
T = float(eltype(F.values))
973+
S, C = Diagonal(similar(A, T, (n,))), Diagonal(similar(A, T, (n,)))
973974
for i in 1:n
974975
S.diag[i], C.diag[i] = sincos(F.values[i])
975976
end
@@ -980,7 +981,8 @@ end
980981
function sincos(A::Hermitian{<:Complex})
981982
n = checksquare(A)
982983
F = eigen(A)
983-
S, C = Diagonal(similar(A, (n,))), Diagonal(similar(A, (n,)))
984+
T = float(eltype(F.values))
985+
S, C = Diagonal(similar(A, T, (n,))), Diagonal(similar(A, T, (n,)))
984986
for i in 1:n
985987
S.diag[i], C.diag[i] = sincos(F.values[i])
986988
end

stdlib/LinearAlgebra/test/symmetric.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,15 @@ end
11671167
@test a*H == H
11681168
end
11691169

1170+
@testset "trigonometric functions for Integer matrices" begin
1171+
A = diagm(0=>1:4, 1=>1:3, -1=>1:3)
1172+
for B in (Symmetric(A), Symmetric(complex.(A)))
1173+
SC = @inferred(sincos(B))
1174+
@test SC[1] sin(B)
1175+
@test SC[2] cos(B)
1176+
@test cos(A) real(exp(im*A))
1177+
@test sin(A) imag(exp(im*A))
1178+
end
1179+
end
1180+
11701181
end # module TestSymmetric

0 commit comments

Comments
 (0)