Root
Base.sqrt
— Functionsqrt(x)
Return $\sqrt{x}$.
Throws DomainError
for negative Real
arguments. Use complex negative arguments instead. Note that sqrt
has a branch cut along the negative real axis.
The prefix operator √
is equivalent to sqrt
.
See also: hypot
.
Examples
julia> sqrt(big(81))
9.0
julia> sqrt(big(-81))
ERROR: DomainError with -81.0:
NaN result for non-NaN input.
Stacktrace:
[1] sqrt(::BigFloat) at ./mpfr.jl:501
[...]
julia> sqrt(big(complex(-81)))
0.0 + 9.0im
julia> sqrt(-81 - 0.0im) # -0.0im is below the branch cut
0.0 - 9.0im
julia> .√(1:4)
4-element Vector{Float64}:
1.0
1.4142135623730951
1.7320508075688772
2.0
sqrt(A::AbstractMatrix)
If A
has no negative real eigenvalues, compute the principal matrix square root of A
, that is the unique matrix $X$ with eigenvalues having positive real part such that $X^2 = A$. Otherwise, a nonprincipal square root is returned.
If A
is real-symmetric or Hermitian, its eigendecomposition (eigen
) is used to compute the square root. For such matrices, eigenvalues λ that appear to be slightly negative due to roundoff errors are treated as if they were zero. More precisely, matrices with all eigenvalues ≥ -rtol*(max |λ|)
are treated as semidefinite (yielding a Hermitian square root), with negative eigenvalues taken to be zero. rtol
is a keyword argument to sqrt
(in the Hermitian/real-symmetric case only) that defaults to machine precision scaled by size(A,1)
.
Otherwise, the square root is determined by means of the Björck-Hammarling method [BH83], which computes the complex Schur form (schur
) and then the complex square root of the triangular factor. If a real square root exists, then an extension of this method [H87] that computes the real Schur form and then the real square root of the quasi-triangular factor is instead used.
Examples
julia> A = [4 0; 0 4]
2×2 Matrix{Int64}:
4 0
0 4
julia> sqrt(A)
2×2 Matrix{Float64}:
2.0 0.0
0.0 2.0
Base.isqrt
— Functionisqrt(n::Integer)
Integer square root: the largest integer m
such that m*m <= n
.
julia> isqrt(5)
2
Base.Math.cbrt
— Functioncbrt(x::Real)
Return the cube root of x
, i.e. $x^{1/3}$. Negative values are accepted (returning the negative real root when $x < 0$).
The prefix operator ∛
is equivalent to cbrt
.
Examples
julia> cbrt(big(27))
3.0
julia> cbrt(big(-27))
-3.0
cbrt(A::AbstractMatrix{<:Real})
Computes the real-valued cube root of a real-valued matrix A
. If T = cbrt(A)
, then we have T*T*T ≈ A
, see example given below.
If A
is symmetric, i.e., of type HermOrSym{<:Real}
, then (eigen
) is used to find the cube root. Otherwise, a specialized version of the p-th root algorithm [S03] is utilized, which exploits the real-valued Schur decomposition (schur
) to compute the cube root.
Examples
julia> A = [0.927524 -0.15857; -1.3677 -1.01172]
2×2 Matrix{Float64}:
0.927524 -0.15857
-1.3677 -1.01172
julia> T = cbrt(A)
2×2 Matrix{Float64}:
0.910077 -0.151019
-1.30257 -0.936818
julia> T*T*T ≈ A
true
- BH83Åke Björck and Sven Hammarling, "A Schur method for the square root of a matrix", Linear Algebra and its Applications, 52-53, 1983, 127-140. doi:10.1016/0024-3795(83)80010-X
- H87Nicholas J. Higham, "Computing real square roots of a real matrix", Linear Algebra and its Applications, 88-89, 1987, 405-430. doi:10.1016/0024-3795(87)90118-2
- S03Matthew I. Smith, "A Schur Algorithm for Computing Matrix pth Roots", SIAM Journal on Matrix Analysis and Applications, vol. 24, 2003, pp. 971–989. doi:10.1137/S0895479801392697