Log
Base.log
— Functionlog(b,x)
Compute the base b
logarithm of x
. Throws DomainError
for negative Real
arguments.
Examples
julia> log(4,8)
1.5
julia> log(4,2)
0.5
julia> log(-2, 3)
ERROR: DomainError with -2.0:
log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31
[...]
julia> log(2, -3)
ERROR: DomainError with -3.0:
log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31
[...]
log(x)
Compute the natural logarithm of x
.
Throws DomainError
for negative Real
arguments. Use complex arguments to obtain complex results. Has a branch cut along the negative real axis, for which -0.0im
is taken to be below the axis.
See also ℯ
, log1p
, log2
, log10
.
Examples
julia> log(2)
0.6931471805599453
julia> log(-3)
ERROR: DomainError with -3.0:
log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31
[...]
julia> log(-3 + 0im)
1.0986122886681098 + 3.141592653589793im
julia> log(-3 - 0.0im)
1.0986122886681098 - 3.141592653589793im
julia> log.(exp.(-1:1))
3-element Vector{Float64}:
-1.0
0.0
1.0
log(A::AbstractMatrix)
If A
has no negative real eigenvalue, compute the principal matrix logarithm of A
, i.e. the unique matrix $X$ such that $e^X = A$ and $-\pi < Im(\lambda) < \pi$ for all the eigenvalues $\lambda$ of $X$. If A
has nonpositive eigenvalues, a nonprincipal matrix function is returned whenever possible.
If A
is symmetric or Hermitian, its eigendecomposition (eigen
) is used, if A
is triangular an improved version of the inverse scaling and squaring method is employed (see [AH12] and [AHR13]). If A
is real with no negative eigenvalues, then the real Schur form is computed. Otherwise, the complex Schur form is computed. Then the upper (quasi-)triangular algorithm in [AHR13] is used on the upper (quasi-)triangular factor.
Examples
julia> A = Matrix(2.7182818*I, 2, 2)
2×2 Matrix{Float64}:
2.71828 0.0
0.0 2.71828
julia> log(A)
2×2 Matrix{Float64}:
1.0 0.0
0.0 1.0
Complex Plane
- 🔗 WikiPedia: https://en.wikipedia.org/wiki/Complexlogarithm#/media/File:Complexlog_domain.svg
- 🔗 Sage: https://doc.sagemath.org/html/en/reference/plotting/sage/plot/complex_plot.html#codecell6
julia> using UnicodePlots
julia> X = repeat(collect(-20:20)', outer=(41, 1));
julia> Y = repeat(collect(20:-1:-20), outer=(1,41));
julia> grid = complex.(X, Y) ./ 10;
julia> Z = log.(grid);
julia> zangle = angle.(Z);
julia> heatmap(zangle; xfact=0.1, yfact=0.1, xoffset=-2, yoffset=-2)
┌────────────────────────┐ 3 2 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ ┌──┐ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ │▄▄│ -2 │▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄│ └──┘ └────────────────────────┘ -3 -2 2
- AH12Awad H. Al-Mohy and Nicholas J. Higham, "Improved inverse scaling and squaring algorithms for the matrix logarithm", SIAM Journal on Scientific Computing, 34(4), 2012, C153-C169. doi:10.1137/110852553
- AHR13Awad H. Al-Mohy, Nicholas J. Higham and Samuel D. Relton, "Computing the Fréchet derivative of the matrix logarithm and estimating the condition number", SIAM Journal on Scientific Computing, 35(4), 2013, C394-C410. doi:10.1137/120885991