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

test/linalg/dense.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ bimg = randn(n,2)/2
115115
end
116116

117117
@testset "Matrix square root" begin
118-
asq = sqrtm(a)
118+
asq = sqrt(a)
119119
@test asq*asq a
120120
asym = a'+a # symmetric indefinite
121-
asymsq = sqrtm(asym)
121+
asymsq = sqrt(asym)
122122
@test asymsq*asymsq asym
123123
end
124124

@@ -370,10 +370,10 @@ end
370370

371371
@testset "issue #2246" begin
372372
A = [1 2 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 0]
373-
Asq = sqrtm(A)
373+
Asq = sqrt(A)
374374
@test Asq*Asq A
375375
A2 = view(A, 1:2, 1:2)
376-
A2sq = sqrtm(A2)
376+
A2sq = sqrt(A2)
377377
@test A2sq*A2sq A2
378378

379379
N = 3
@@ -569,12 +569,12 @@ end
569569
end
570570

571571
for A in (Aa, Ab, Ac, Ad, Ah, ADi)
572-
@test A^(1/2) sqrtm(A)
573-
@test A^(-1/2) inv(sqrtm(A))
574-
@test A^(3/4) sqrtm(A) * sqrtm(sqrtm(A))
575-
@test A^(-3/4) inv(A) * sqrtm(sqrtm(A))
576-
@test A^(17/8) A^2 * sqrtm(sqrtm(sqrtm(A)))
577-
@test A^(-17/8) inv(A^2 * sqrtm(sqrtm(sqrtm(A))))
572+
@test A^(1/2) sqrt(A)
573+
@test A^(-1/2) inv(sqrt(A))
574+
@test A^(3/4) sqrt(A) * sqrt(sqrt(A))
575+
@test A^(-3/4) inv(A) * sqrt(sqrt(A))
576+
@test A^(17/8) A^2 * sqrt(sqrt(sqrt(A)))
577+
@test A^(-17/8) inv(A^2 * sqrt(sqrt(sqrt(A))))
578578
@test (A^0.2)^5 A
579579
@test (A^(2/3))*(A^(1/3)) A
580580
@test (A^im)^(-im) A
@@ -680,7 +680,6 @@ end
680680
@testset "test ops on Numbers for $elty" for elty in [Float32,Float64,Complex64,Complex128]
681681
a = rand(elty)
682682
@test isposdef(one(elty))
683-
@test sqrtm(a) == sqrt(a)
684683
@test lyap(one(elty),a) == -a/2
685684
end
686685

test/linalg/diagonal.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ srand(1)
6666
@test log(Diagonal(abs.(D.diag))) log(abs.(DM)) atol=n^3*eps(relty)
6767
end
6868
if elty <: BlasComplex
69-
for func in (logdet, sqrtm)
69+
for func in (logdet, sqrt)
7070
@test func(D) func(DM) atol=n^2*eps(relty)*2
7171
end
7272
end
@@ -383,7 +383,7 @@ end
383383

384384
@test exp(D) == Diagonal([exp([1 2; 3 4]), exp([1 2; 3 4])])
385385
@test log(D) == Diagonal([log([1 2; 3 4]), log([1 2; 3 4])])
386-
@test sqrtm(D) == Diagonal([sqrtm([1 2; 3 4]), sqrtm([1 2; 3 4])])
386+
@test sqrt(D) == Diagonal([sqrt([1 2; 3 4]), sqrt([1 2; 3 4])])
387387
end
388388

389389
@testset "multiplication with Symmetric/Hermitian" begin

test/linalg/triangular.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ for elty1 in (Float32, Float64, BigFloat, Complex64, Complex128, Complex{BigFloa
237237
@test ladb fladb atol=sqrt(eps(real(float(one(elty1)))))*n*n
238238

239239
# Matrix square root
240-
@test sqrtm(A1) |> t -> t*t A1
240+
@test sqrt(A1) |> t -> t*t A1
241241

242242
# naivesub errors
243243
@test_throws DimensionMismatch naivesub!(A1,ones(elty1,n+1))
@@ -391,10 +391,10 @@ end
391391
# Matrix square root
392392
Atn = UpperTriangular([-1 1 2; 0 -2 2; 0 0 -3])
393393
Atp = UpperTriangular([1 1 2; 0 2 2; 0 0 3])
394-
@test sqrtm(Atn) |> t->t*t Atn
395-
@test typeof(sqrtm(Atn)[1,1]) <: Complex
396-
@test sqrtm(Atp) |> t->t*t Atp
397-
@test typeof(sqrtm(Atp)[1,1]) <: Real
394+
@test sqrt(Atn) |> t->t*t Atn
395+
@test typeof(sqrt(Atn)[1,1]) <: Complex
396+
@test sqrt(Atp) |> t->t*t Atp
397+
@test typeof(sqrt(Atp)[1,1]) <: Real
398398

399399
Areal = randn(n, n)/2
400400
Aimg = randn(n, n)/2
@@ -511,5 +511,5 @@ end
511511
isdefined(Main, :TestHelpers) || @eval Main include("../TestHelpers.jl")
512512
using TestHelpers.Furlong
513513
let A = UpperTriangular([Furlong(1) Furlong(4); Furlong(0) Furlong(1)])
514-
@test sqrtm(A) == Furlong{1//2}.(UpperTriangular([1 2; 0 1]))
514+
@test sqrt(A) == Furlong{1//2}.(UpperTriangular([1 2; 0 1]))
515515
end

0 commit comments

Comments
 (0)