@@ -592,7 +592,7 @@ function schurpow(A::AbstractMatrix, p)
592592 end
593593
594594 # if A has nonpositive real eigenvalues, retmat is a nonprincipal matrix power.
595- if isreal (retmat)
595+ if eltype (A) <: Real && isreal (retmat)
596596 return real (retmat)
597597 else
598598 return retmat
@@ -602,20 +602,19 @@ function (^)(A::AbstractMatrix{T}, p::Real) where T
602602 checksquare (A)
603603 # Quicker return if A is diagonal
604604 if isdiag (A)
605- TT = promote_op ( ^ , T, typeof (p ))
606- retmat = copymutable_oftype (A, TT )
607- for i in diagind (retmat, IndexStyle (retmat))
608- retmat[i] = retmat[i] ^ p
605+ if T <: Real && any ( < ( 0 ), diagview (A ))
606+ return applydiagonal (x -> complex (x) ^ p, A )
607+ else
608+ return applydiagonal (x -> x ^ p, A)
609609 end
610- return retmat
611610 end
612611
613612 # For integer powers, use power_by_squaring
614613 isinteger (p) && return integerpow (A, p)
615614
616615 # If possible, use diagonalization
617616 if ishermitian (A)
618- return (Hermitian (A)^ p)
617+ return parent (Hermitian (A)^ p)
619618 end
620619
621620 # Otherwise, use Schur decomposition
@@ -745,7 +744,7 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
745744 end
746745 return A
747746 elseif ishermitian (A)
748- return copytri! ( parent (exp (Hermitian (A))), ' U ' , true )
747+ return parent (exp (Hermitian (A)))
749748 end
750749 ilo, ihi, scale = LAPACK. gebal! (' B' , A) # modifies A
751750 nA = opnorm (A, 1 )
@@ -918,10 +917,10 @@ julia> log(A)
918917function log (A:: AbstractMatrix )
919918 # If possible, use diagonalization
920919 if isdiag (A) && eltype (A) <: Union{Real,Complex}
921- if eltype (A) <: Real && all ( >= (0 ), diagview (A))
922- return applydiagonal (log, A)
920+ if eltype (A) <: Real && any ( < (0 ), diagview (A))
921+ return applydiagonal (log ∘ complex , A)
923922 else
924- return applydiagonal (log∘ complex , A)
923+ return applydiagonal (log, A)
925924 end
926925 elseif ishermitian (A)
927926 logHermA = log (Hermitian (A))
@@ -1004,13 +1003,14 @@ sqrt(::AbstractMatrix)
10041003function sqrt (A:: AbstractMatrix{T} ) where {T<: Union{Real,Complex} }
10051004 if checksquare (A) == 0
10061005 return copy (float (A))
1007- elseif isdiag (A) && (T <: Complex || all (x -> x ≥ zero (x), diagview (A)))
1008- # Real Diagonal sqrt requires each diagonal element to be positive
1009- return applydiagonal (sqrt, A)
1006+ elseif isdiag (A)
1007+ if T <: Real && any (< (0 ), diagview (A))
1008+ return applydiagonal (sqrt ∘ complex, A)
1009+ else
1010+ return applydiagonal (sqrt, A)
1011+ end
10101012 elseif ishermitian (A)
1011- sqrtHermA = sqrt (Hermitian (A))
1012- PS = parent (sqrtHermA)
1013- return ishermitian (sqrtHermA) ? copytri_maybe_inplace (PS, ' U' , true ) : PS
1013+ return parent (sqrt (Hermitian (A)))
10141014 elseif istriu (A)
10151015 return triu! (parent (sqrt (UpperTriangular (A))))
10161016 elseif isreal (A)
@@ -1044,7 +1044,7 @@ sqrt(A::TransposeAbsMat) = transpose(sqrt(parent(A)))
10441044Computes the real-valued cube root of a real-valued matrix `A`. If `T = cbrt(A)`, then
10451045we have `T*T*T ≈ A`, see example given below.
10461046
1047- If `A` is symmetric, i.e., of type `HermOrSym{<:Real}`, then ([`eigen`](@ref)) is used to
1047+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
10481048find the cube root. Otherwise, a specialized version of the p-th root algorithm [^S03] is
10491049utilized, which exploits the real-valued Schur decomposition ([`schur`](@ref))
10501050to compute the cube root.
@@ -1077,7 +1077,7 @@ function cbrt(A::AbstractMatrix{<:Real})
10771077 elseif isdiag (A)
10781078 return applydiagonal (cbrt, A)
10791079 elseif issymmetric (A)
1080- return cbrt (Symmetric (A, :U ) )
1080+ return copytri! ( parent ( cbrt (Symmetric (A))), ' U ' )
10811081 else
10821082 S = schur (A)
10831083 return S. Z * _cbrt_quasi_triu! (S. T) * S. Z'
@@ -1118,7 +1118,7 @@ end
11181118
11191119Compute the matrix cosine of a square matrix `A`.
11201120
1121- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1121+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
11221122compute the cosine. Otherwise, the cosine is determined by calling [`exp`](@ref).
11231123
11241124# Examples
@@ -1160,7 +1160,7 @@ end
11601160
11611161Compute the matrix sine of a square matrix `A`.
11621162
1163- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1163+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
11641164compute the sine. Otherwise, the sine is determined by calling [`exp`](@ref).
11651165
11661166# Examples
@@ -1265,7 +1265,7 @@ end
12651265
12661266Compute the matrix tangent of a square matrix `A`.
12671267
1268- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1268+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
12691269compute the tangent. Otherwise, the tangent is determined by calling [`exp`](@ref).
12701270
12711271# Examples
@@ -1357,7 +1357,7 @@ _subadd!!(X, Y) = X - Y, X + Y
13571357
13581358Compute the inverse matrix cosine of a square matrix `A`.
13591359
1360- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1360+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
13611361compute the inverse cosine. Otherwise, the inverse cosine is determined by using
13621362[`log`](@ref) and [`sqrt`](@ref). For the theory and logarithmic formulas used to compute
13631363this function, see [^AH16_1].
@@ -1391,7 +1391,7 @@ end
13911391
13921392Compute the inverse matrix sine of a square matrix `A`.
13931393
1394- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1394+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
13951395compute the inverse sine. Otherwise, the inverse sine is determined by using [`log`](@ref)
13961396and [`sqrt`](@ref). For the theory and logarithmic formulas used to compute this function,
13971397see [^AH16_2].
@@ -1425,7 +1425,7 @@ end
14251425
14261426Compute the inverse matrix tangent of a square matrix `A`.
14271427
1428- If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
1428+ If `A` is real- symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to
14291429compute the inverse tangent. Otherwise, the inverse tangent is determined by using
14301430[`log`](@ref). For the theory and logarithmic formulas used to compute this function, see
14311431[^AH16_3].
@@ -1436,8 +1436,8 @@ compute the inverse tangent. Otherwise, the inverse tangent is determined by usi
14361436```julia-repl
14371437julia> atan(tan([0.5 0.1; -0.2 0.3]))
143814382×2 Matrix{ComplexF64}:
1439- 0.5+1.38778e-17im 0.1-2.77556e-17im
1440- -0.2+6.93889e-17im 0.3-4.16334e-17im
1439+ 0.5 0.1
1440+ -0.2 0.3
14411441```
14421442"""
14431443function atan (A:: AbstractMatrix )
@@ -1450,7 +1450,12 @@ function atan(A::AbstractMatrix)
14501450 SchurF = Schur {Complex} (schur (A))
14511451 U = im * UpperTriangular (SchurF. T)
14521452 R = triu! (parent (log ((I + U) / (I - U)) / 2im ))
1453- return SchurF. Z * R * SchurF. Z'
1453+ retmat = SchurF. Z * R * SchurF. Z'
1454+ if eltype (A) <: Real
1455+ return real (retmat)
1456+ else
1457+ return retmat
1458+ end
14541459end
14551460
14561461"""
@@ -1493,7 +1498,12 @@ function asinh(A::AbstractMatrix)
14931498 SchurF = Schur {Complex} (schur (A))
14941499 U = UpperTriangular (SchurF. T)
14951500 R = triu! (parent (log (U + sqrt (I + U^ 2 ))))
1496- return SchurF. Z * R * SchurF. Z'
1501+ retmat = SchurF. Z * R * SchurF. Z'
1502+ if eltype (A) <: Real
1503+ return real (retmat)
1504+ else
1505+ return retmat
1506+ end
14971507end
14981508
14991509"""
0 commit comments