Skip to content

Commit 547d461

Browse files
committed
Deprecate sqrtm in favor of sqrt.
1 parent 109b0b4 commit 547d461

File tree

13 files changed

+87
-55
lines changed

13 files changed

+87
-55
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ Deprecated or removed
336336
full path if you need access to executables or libraries in the `JULIA_HOME` directory, e.g.
337337
`joinpath(JULIA_HOME, "7z.exe")` for `7z.exe` ([#21540]).
338338

339+
* `sqrtm` has been deprecated in favor of `sqrt` ([#23504]).
340+
339341
* `expm` has been deprecated in favor of `exp` ([#23233]).
340342

341343
* Calling `union` with no arguments is deprecated; construct an empty set with an appropriate

base/deprecated.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ for f in (
253253
# base/math.jl
254254
:cbrt, :sinh, :cosh, :tanh, :atan, :asinh, :exp2,
255255
:expm1, :exp10, :sin, :cos, :tan, :asin, :acos, :acosh, :atanh,
256-
#=:log,=# :log2, :log10, :lgamma, #=:log1p,=# :sqrt,
256+
#=:log,=# :log2, :log10, :lgamma, #=:log1p,=#
257257
# base/floatfuncs.jl
258258
:abs, :abs2, :angle, :isnan, :isinf, :isfinite,
259259
# base/complex.jl
@@ -1660,6 +1660,9 @@ function Tridiagonal(dl::AbstractVector{Tl}, d::AbstractVector{Td}, du::Abstract
16601660
Tridiagonal(map(v->convert(Vector{promote_type(Tl,Td,Tu)}, v), (dl, d, du))...)
16611661
end
16621662

1663+
# deprecate sqrtm in favor of sqrt
1664+
@deprecate sqrtm sqrt
1665+
16631666
# deprecate expm in favor of exp
16641667
@deprecate expm! exp!
16651668
@deprecate expm exp

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,6 @@ export
613613
schur,
614614
schurfact!,
615615
schurfact,
616-
sqrtm,
617616
svd,
618617
svdfact!,
619618
svdfact,

base/linalg/dense.jl

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ function schurpow(A::AbstractMatrix, p)
374374
retmat = A ^ floor(p)
375375
# Real part
376376
if p - floor(p) == 0.5
377-
# special case: A^0.5 === sqrtm(A)
378-
retmat = retmat * sqrtm(A)
377+
# special case: A^0.5 === sqrt(A)
378+
retmat = retmat * sqrt(A)
379379
else
380380
retmat = retmat * powm!(UpperTriangular(float.(A)), real(p - floor(p)))
381381
end
@@ -385,8 +385,8 @@ function schurpow(A::AbstractMatrix, p)
385385
R = S ^ floor(p)
386386
# Real part
387387
if p - floor(p) == 0.5
388-
# special case: A^0.5 === sqrtm(A)
389-
R = R * sqrtm(S)
388+
# special case: A^0.5 === sqrt(A)
389+
R = R * sqrt(S)
390390
else
391391
R = R * powm!(UpperTriangular(float.(S)), real(p - floor(p)))
392392
end
@@ -622,7 +622,7 @@ end
622622
logm(a::Complex) = log(a)
623623

624624
"""
625-
sqrtm(A)
625+
sqrt(A::AbstractMatrix)
626626
627627
If `A` has no negative real eigenvalues, compute the principal matrix square root of `A`,
628628
that is the unique matrix ``X`` with eigenvalues having positive real part such that
@@ -646,40 +646,38 @@ julia> A = [4 0; 0 4]
646646
4 0
647647
0 4
648648
649-
julia> sqrtm(A)
649+
julia> sqrt(A)
650650
2×2 Array{Float64,2}:
651651
2.0 0.0
652652
0.0 2.0
653653
```
654654
"""
655-
function sqrtm(A::StridedMatrix{<:Real})
655+
function sqrt(A::StridedMatrix{<:Real})
656656
if issymmetric(A)
657-
return full(sqrtm(Symmetric(A)))
657+
return full(sqrt(Symmetric(A)))
658658
end
659659
n = checksquare(A)
660660
if istriu(A)
661-
return full(sqrtm(UpperTriangular(A)))
661+
return full(sqrt(UpperTriangular(A)))
662662
else
663663
SchurF = schurfact(complex(A))
664-
R = full(sqrtm(UpperTriangular(SchurF[:T])))
664+
R = full(sqrt(UpperTriangular(SchurF[:T])))
665665
return SchurF[:vectors] * R * SchurF[:vectors]'
666666
end
667667
end
668-
function sqrtm(A::StridedMatrix{<:Complex})
668+
function sqrt(A::StridedMatrix{<:Complex})
669669
if ishermitian(A)
670-
return full(sqrtm(Hermitian(A)))
670+
return full(sqrt(Hermitian(A)))
671671
end
672672
n = checksquare(A)
673673
if istriu(A)
674-
return full(sqrtm(UpperTriangular(A)))
674+
return full(sqrt(UpperTriangular(A)))
675675
else
676676
SchurF = schurfact(A)
677-
R = full(sqrtm(UpperTriangular(SchurF[:T])))
677+
R = full(sqrt(UpperTriangular(SchurF[:T])))
678678
return SchurF[:vectors] * R * SchurF[:vectors]'
679679
end
680680
end
681-
sqrtm(a::Number) = (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b)
682-
sqrtm(a::Complex) = sqrt(a)
683681

684682
function inv(A::StridedMatrix{T}) where T
685683
checksquare(A)

base/linalg/diagonal.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ eye(::Type{Diagonal{T}}, n::Int) where {T} = Diagonal(ones(T,n))
329329
exp(D::Diagonal) = Diagonal(exp.(D.diag))
330330
logm(D::Diagonal) = Diagonal(log.(D.diag))
331331
logm(D::Diagonal{<:AbstractMatrix}) = Diagonal(logm.(D.diag))
332-
sqrtm(D::Diagonal) = Diagonal(sqrt.(D.diag))
333-
sqrtm(D::Diagonal{<:AbstractMatrix}) = Diagonal(sqrtm.(D.diag))
332+
sqrt(D::Diagonal) = Diagonal(sqrt.(D.diag))
334333

335334
#Linear solver
336335
function A_ldiv_B!(D::Diagonal, B::StridedVecOrMat)

base/linalg/linalg.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Base: USE_BLAS64, abs, big, broadcast, ceil, conj, convert, copy, copy!,
99
adjoint, eltype, exp, eye, findmax, findmin, fill!, floor, full, getindex,
1010
hcat, imag, indices, inv, isapprox, isone, IndexStyle, kron, length, map,
1111
ndims, oneunit, parent, power_by_squaring, print_matrix, promote_rule, real, round,
12-
setindex!, show, similar, size, transpose, trunc, typed_hcat
12+
setindex!, show, similar, size, sqrt, transpose, trunc, typed_hcat
1313
using Base: hvcat_fill, iszero, IndexLinear, _length, promote_op, promote_typeof,
1414
@propagate_inbounds, @pure, reduce, typed_vcat
1515
# We use `_length` because of non-1 indices; releases after julia 0.5
@@ -125,7 +125,6 @@ export
125125
schur,
126126
schurfact!,
127127
schurfact,
128-
sqrtm,
129128
svd,
130129
svdfact!,
131130
svdfact,

base/linalg/symmetric.jl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,40 @@ function exp(A::Hermitian{T}) where T
607607
end
608608
end
609609

610-
for (funm, func) in ([:logm,:log], [:sqrtm,:sqrt])
610+
for func in (:sqrt, #=:logm=#)
611+
@eval begin
612+
function ($func)(A::Symmetric{T}) where T<:Real
613+
F = eigfact(A)
614+
if all-> λ 0, F.values)
615+
retmat = (F.vectors * Diagonal(($func).(F.values))) * F.vectors'
616+
else
617+
retmat = (F.vectors * Diagonal(($func).(complex.(F.values)))) * F.vectors'
618+
end
619+
return Symmetric(retmat)
620+
end
621+
622+
function ($func)(A::Hermitian{T}) where T
623+
n = checksquare(A)
624+
F = eigfact(A)
625+
if all-> λ 0, F.values)
626+
retmat = (F.vectors * Diagonal(($func).(F.values))) * F.vectors'
627+
if T <: Real
628+
return Hermitian(retmat)
629+
else
630+
for i = 1:n
631+
retmat[i,i] = real(retmat[i,i])
632+
end
633+
return Hermitian(retmat)
634+
end
635+
else
636+
retmat = (F.vectors * Diagonal(($func).(complex(F.values)))) * F.vectors'
637+
return retmat
638+
end
639+
end
640+
end
641+
end
642+
643+
for (funm, func) in ([:logm,:log], )
611644
@eval begin
612645
function ($funm)(A::Symmetric{T}) where T<:Real
613646
F = eigfact(A)

base/linalg/triangular.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ function logm(A0::UpperTriangular{T}) where T<:Union{Float64,Complex{Float64}}
18151815
end
18161816
s0 = s
18171817
for k = 1:min(s, maxsqrt)
1818-
A = sqrtm(A)
1818+
A = sqrt(A)
18191819
end
18201820

18211821
AmI = A - I
@@ -1871,7 +1871,7 @@ function logm(A0::UpperTriangular{T}) where T<:Union{Float64,Complex{Float64}}
18711871
m = tmax
18721872
break
18731873
end
1874-
A = sqrtm(A)
1874+
A = sqrt(A)
18751875
AmI = A - I
18761876
s = s + 1
18771877
end
@@ -2015,7 +2015,7 @@ function invsquaring(A0::UpperTriangular, theta)
20152015
end
20162016
s0 = s
20172017
for k = 1:min(s, maxsqrt)
2018-
A = sqrtm(A)
2018+
A = sqrt(A)
20192019
end
20202020

20212021
AmI = A - I
@@ -2073,7 +2073,7 @@ function invsquaring(A0::UpperTriangular, theta)
20732073
m = tmax
20742074
break
20752075
end
2076-
A = sqrtm(A)
2076+
A = sqrt(A)
20772077
AmI = A - I
20782078
s = s + 1
20792079
end
@@ -2119,7 +2119,7 @@ unw(x::Number) = ceil((imag(x) - pi) / (2 * pi))
21192119

21202120
# End of auxiliary functions for logm and matrix power
21212121

2122-
function sqrtm(A::UpperTriangular)
2122+
function sqrt(A::UpperTriangular)
21232123
realmatrix = false
21242124
if isreal(A)
21252125
realmatrix = true
@@ -2131,9 +2131,9 @@ function sqrtm(A::UpperTriangular)
21312131
end
21322132
end
21332133
end
2134-
sqrtm(A,Val(realmatrix))
2134+
sqrt(A,Val(realmatrix))
21352135
end
2136-
function sqrtm(A::UpperTriangular{T},::Val{realmatrix}) where {T,realmatrix}
2136+
function sqrt(A::UpperTriangular{T},::Val{realmatrix}) where {T,realmatrix}
21372137
B = A.data
21382138
n = checksquare(B)
21392139
t = realmatrix ? typeof(sqrt(zero(T))) : typeof(sqrt(complex(zero(T))))
@@ -2151,7 +2151,7 @@ function sqrtm(A::UpperTriangular{T},::Val{realmatrix}) where {T,realmatrix}
21512151
end
21522152
return UpperTriangular(R)
21532153
end
2154-
function sqrtm(A::UnitUpperTriangular{T}) where T
2154+
function sqrt(A::UnitUpperTriangular{T}) where T
21552155
B = A.data
21562156
n = checksquare(B)
21572157
t = typeof(sqrt(zero(T)))
@@ -2169,8 +2169,8 @@ function sqrtm(A::UnitUpperTriangular{T}) where T
21692169
end
21702170
return UnitUpperTriangular(R)
21712171
end
2172-
sqrtm(A::LowerTriangular) = sqrtm(A.').'
2173-
sqrtm(A::UnitLowerTriangular) = sqrtm(A.').'
2172+
sqrt(A::LowerTriangular) = sqrt(A.').'
2173+
sqrt(A::UnitLowerTriangular) = sqrt(A.').'
21742174

21752175
# Generic eigensystems
21762176
eigvals(A::AbstractTriangular) = diag(A)

doc/src/manual/linear-algebra.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ as well as whether hooks to various optimized methods for them in LAPACK are ava
177177

178178
| Matrix type | `+` | `-` | `*` | `\` | Other functions with optimized methods |
179179
|:------------------------- |:--- |:--- |:--- |:--- |:------------------------------------------------------------------- |
180-
| [`Symmetric`](@ref) | | | | MV | [`inv()`](@ref), [`sqrtm()`](@ref), [`exp()`](@ref) |
181-
| [`Hermitian`](@ref) | | | | MV | [`inv()`](@ref), [`sqrtm()`](@ref), [`exp()`](@ref) |
180+
| [`Symmetric`](@ref) | | | | MV | [`inv()`](@ref), [`sqrt()`](@ref), [`exp()`](@ref) |
181+
| [`Hermitian`](@ref) | | | | MV | [`inv()`](@ref), [`sqrt()`](@ref), [`exp()`](@ref) |
182182
| [`UpperTriangular`](@ref) | | | MV | MV | [`inv()`](@ref), [`det()`](@ref) |
183183
| [`LowerTriangular`](@ref) | | | MV | MV | [`inv()`](@ref), [`det()`](@ref) |
184184
| [`SymTridiagonal`](@ref) | M | M | MS | MV | [`eigmax()`](@ref), [`eigmin()`](@ref) |

doc/src/stdlib/linalg.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ Base.kron
9393
Base.SparseArrays.blkdiag
9494
Base.LinAlg.linreg
9595
Base.LinAlg.logm
96-
Base.LinAlg.sqrtm
9796
Base.LinAlg.lyap
9897
Base.LinAlg.sylvester
9998
Base.LinAlg.issuccess

0 commit comments

Comments
 (0)