Skip to content

Commit 44fe78c

Browse files
Sacha0andreasnoack
authored andcommitted
Deprecate sqrtm in favor of sqrt. (#23504)
1 parent b729e58 commit 44fe78c

File tree

13 files changed

+53
-88
lines changed

13 files changed

+53
-88
lines changed

NEWS.md

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

358+
* `sqrtm` has been deprecated in favor of `sqrt` ([#23504]).
359+
358360
* `expm` has been deprecated in favor of `exp` ([#23233]).
359361

360362
* `logm` has been deprecated in favor of `log` ([#23505]).

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-
:log2, :log10, :lgamma, #=:log1p,=# :sqrt,
256+
:log2, :log10, :lgamma, #=:log1p,=#
257257
# base/floatfuncs.jl
258258
:abs, :abs2, :angle, :isnan, :isinf, :isfinite,
259259
# base/complex.jl
@@ -1620,6 +1620,9 @@ function Tridiagonal(dl::AbstractVector{Tl}, d::AbstractVector{Td}, du::Abstract
16201620
Tridiagonal(map(v->convert(Vector{promote_type(Tl,Td,Tu)}, v), (dl, d, du))...)
16211621
end
16221622

1623+
# deprecate sqrtm in favor of sqrt
1624+
@deprecate sqrtm sqrt
1625+
16231626
# deprecate expm in favor of exp
16241627
@deprecate expm! exp!
16251628
@deprecate expm exp

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ export
609609
schur,
610610
schurfact!,
611611
schurfact,
612-
sqrtm,
613612
svd,
614613
svdfact!,
615614
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
@@ -618,7 +618,7 @@ function log(A::StridedMatrix{T}) where T
618618
end
619619

620620
"""
621-
sqrtm(A)
621+
sqrt(A::AbstractMatrix)
622622
623623
If `A` has no negative real eigenvalues, compute the principal matrix square root of `A`,
624624
that is the unique matrix ``X`` with eigenvalues having positive real part such that
@@ -642,40 +642,38 @@ julia> A = [4 0; 0 4]
642642
4 0
643643
0 4
644644
645-
julia> sqrtm(A)
645+
julia> sqrt(A)
646646
2×2 Array{Float64,2}:
647647
2.0 0.0
648648
0.0 2.0
649649
```
650650
"""
651-
function sqrtm(A::StridedMatrix{<:Real})
651+
function sqrt(A::StridedMatrix{<:Real})
652652
if issymmetric(A)
653-
return full(sqrtm(Symmetric(A)))
653+
return full(sqrt(Symmetric(A)))
654654
end
655655
n = checksquare(A)
656656
if istriu(A)
657-
return full(sqrtm(UpperTriangular(A)))
657+
return full(sqrt(UpperTriangular(A)))
658658
else
659659
SchurF = schurfact(complex(A))
660-
R = full(sqrtm(UpperTriangular(SchurF[:T])))
660+
R = full(sqrt(UpperTriangular(SchurF[:T])))
661661
return SchurF[:vectors] * R * SchurF[:vectors]'
662662
end
663663
end
664-
function sqrtm(A::StridedMatrix{<:Complex})
664+
function sqrt(A::StridedMatrix{<:Complex})
665665
if ishermitian(A)
666-
return full(sqrtm(Hermitian(A)))
666+
return full(sqrt(Hermitian(A)))
667667
end
668668
n = checksquare(A)
669669
if istriu(A)
670-
return full(sqrtm(UpperTriangular(A)))
670+
return full(sqrt(UpperTriangular(A)))
671671
else
672672
SchurF = schurfact(A)
673-
R = full(sqrtm(UpperTriangular(SchurF[:T])))
673+
R = full(sqrt(UpperTriangular(SchurF[:T])))
674674
return SchurF[:vectors] * R * SchurF[:vectors]'
675675
end
676676
end
677-
sqrtm(a::Number) = (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b)
678-
sqrtm(a::Complex) = sqrt(a)
679677

680678
function inv(A::StridedMatrix{T}) where T
681679
checksquare(A)

base/linalg/diagonal.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ eye(::Type{Diagonal{T}}, n::Int) where {T} = Diagonal(ones(T,n))
328328
# Matrix functions
329329
exp(D::Diagonal) = Diagonal(exp.(D.diag))
330330
log(D::Diagonal) = Diagonal(log.(D.diag))
331-
sqrtm(D::Diagonal) = Diagonal(sqrt.(D.diag))
332-
sqrtm(D::Diagonal{<:AbstractMatrix}) = Diagonal(sqrtm.(D.diag))
331+
sqrt(D::Diagonal) = Diagonal(sqrt.(D.diag))
333332

334333
#Linear solver
335334
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, log, 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
@@ -124,7 +124,6 @@ export
124124
schur,
125125
schurfact!,
126126
schurfact,
127-
sqrtm,
128127
svd,
129128
svdfact!,
130129
svdfact,

base/linalg/symmetric.jl

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

610-
for (funm, func) in ([:sqrtm,:sqrt],)
611-
@eval begin
612-
function ($funm)(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 ($funm)(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 func in (:log, #=:sqrtm=#)
610+
for func in (:log, :sqrt)
644611
@eval begin
645612
function ($func)(A::Symmetric{T}) where T<:Real
646613
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 log(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 log(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 matrix logarithm 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
@@ -92,7 +92,6 @@ Base.repmat
9292
Base.kron
9393
Base.SparseArrays.blkdiag
9494
Base.LinAlg.linreg
95-
Base.LinAlg.sqrtm
9695
Base.LinAlg.lyap
9796
Base.LinAlg.sylvester
9897
Base.LinAlg.issuccess

0 commit comments

Comments
 (0)