Skip to content

Commit be9198c

Browse files
authored
Merge pull request JuliaLang#19507 from xorJane/LinAlgDoctests
Add more doctests for linear algebra standard library
2 parents 7b5b3f3 + 459e693 commit be9198c

File tree

7 files changed

+215
-17
lines changed

7 files changed

+215
-17
lines changed

base/arraymath.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,22 @@ end
473473
transpose(A)
474474
475475
The transposition operator (`.'`).
476+
477+
# Example
478+
479+
```jldoctest
480+
julia> A = [1 2 3; 4 5 6; 7 8 9]
481+
3×3 Array{Int64,2}:
482+
1 2 3
483+
4 5 6
484+
7 8 9
485+
486+
julia> transpose(A)
487+
3×3 Array{Int64,2}:
488+
1 4 7
489+
2 5 8
490+
3 6 9
491+
```
476492
"""
477493
function transpose(A::AbstractMatrix)
478494
ind1, ind2 = indices(A)

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,23 +2081,6 @@ Raise an `ErrorException` with the given message.
20812081
"""
20822082
error
20832083

2084-
"""
2085-
sqrtm(A)
2086-
2087-
If `A` has no negative real eigenvalues, compute the principal matrix square root of `A`,
2088-
that is the unique matrix ``X`` with eigenvalues having positive real part such that
2089-
``X^2 = A``. Otherwise, a nonprincipal square root is returned.
2090-
2091-
If `A` is symmetric or Hermitian, its eigendecomposition ([`eigfact`](:func:`eigfact`)) is
2092-
used to compute the square root. Otherwise, the square root is determined by means of the
2093-
Björck-Hammarling method, which computes the complex Schur form ([`schur`](:func:`schur`))
2094-
and then the complex square root of the triangular factor.
2095-
2096-
[^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](http://dx.doi.org/10.1016/0024-3795(83)80010-X)
2097-
2098-
"""
2099-
sqrtm
2100-
21012084
"""
21022085
readcsv(source, [T::Type]; options...)
21032086

base/linalg/dense.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,19 @@ triangular factor.
469469
470470
[^AHR13]: Awad 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](http://dx.doi.org/10.1137/120885991)
471471
472+
# Example
473+
474+
```jldoctest
475+
julia> A = 2.7182818 * eye(2)
476+
2×2 Array{Float64,2}:
477+
2.71828 0.0
478+
0.0 2.71828
479+
480+
julia> logm(A)
481+
2×2 Array{Float64,2}:
482+
1.0 0.0
483+
0.0 1.0
484+
```
472485
"""
473486
function logm(A::StridedMatrix)
474487
# If possible, use diagonalization
@@ -508,6 +521,34 @@ function logm(a::Number)
508521
end
509522
logm(a::Complex) = log(a)
510523

524+
"""
525+
sqrtm(A)
526+
527+
If `A` has no negative real eigenvalues, compute the principal matrix square root of `A`,
528+
that is the unique matrix ``X`` with eigenvalues having positive real part such that
529+
``X^2 = A``. Otherwise, a nonprincipal square root is returned.
530+
531+
If `A` is symmetric or Hermitian, its eigendecomposition ([`eigfact`](:func:`eigfact`)) is
532+
used to compute the square root. Otherwise, the square root is determined by means of the
533+
Björck-Hammarling method, which computes the complex Schur form ([`schur`](:func:`schur`))
534+
and then the complex square root of the triangular factor.
535+
536+
[^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](http://dx.doi.org/10.1016/0024-3795(83)80010-X)
537+
538+
# Example
539+
540+
```jldoctest
541+
julia> A = [4 0; 0 4]
542+
2×2 Array{Int64,2}:
543+
4 0
544+
0 4
545+
546+
julia> sqrtm(A)
547+
2×2 Array{Float64,2}:
548+
2.0 0.0
549+
0.0 2.0
550+
```
551+
"""
511552
function sqrtm{T<:Real}(A::StridedMatrix{T})
512553
if issymmetric(A)
513554
return full(sqrtm(Symmetric(A)))

base/linalg/generic.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ element type for which `norm` is defined), compute the `p`-norm (defaulting to `
418418
`A` were a vector of the corresponding length.
419419
420420
For example, if `A` is a matrix and `p=2`, then this is equivalent to the Frobenius norm.
421+
422+
# Example
423+
424+
```jldoctest
425+
julia> vecnorm([1 2 3; 4 5 6; 7 8 9])
426+
16.881943016134134
427+
428+
julia> vecnorm([1 2 3 4 5 6 7 8 9])
429+
16.881943016134134
430+
```
421431
"""
422432
function vecnorm(itr, p::Real=2)
423433
isempty(itr) && return float(real(zero(eltype(itr))))
@@ -1134,6 +1144,21 @@ logabsdet(A::AbstractMatrix) = logabsdet(lufact(A))
11341144
11351145
Log of matrix determinant. Equivalent to `log(det(M))`, but may provide increased accuracy
11361146
and/or speed.
1147+
1148+
# Example
1149+
1150+
```jldoctest
1151+
julia> M = [1 0; 2 2]
1152+
2×2 Array{Int64,2}:
1153+
1 0
1154+
2 2
1155+
1156+
julia> logdet(M)
1157+
0.6931471805599453
1158+
1159+
julia> logdet(eye(3))
1160+
0.0
1161+
```
11371162
"""
11381163
function logdet(A::AbstractMatrix)
11391164
d,s = logabsdet(A)

base/linalg/hessenberg.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ factorization object, the unitary matrix can be accessed with `F[:Q]` and the He
2828
matrix with `F[:H]`. When `Q` is extracted, the resulting type is the `HessenbergQ` object,
2929
and may be converted to a regular matrix with [`convert(Array, _)`](:func:`convert`)
3030
(or `Array(_)` for short).
31+
32+
# Example
33+
34+
```jldoctest
35+
julia> A = [4. 9. 7.; 4. 4. 1.; 4. 3. 2.]
36+
3×3 Array{Float64,2}:
37+
4.0 9.0 7.0
38+
4.0 4.0 1.0
39+
4.0 3.0 2.0
40+
41+
julia> F = hessfact(A);
42+
43+
julia> F[:Q] * F[:H] * F[:Q]'
44+
3×3 Array{Float64,2}:
45+
4.0 9.0 7.0
46+
4.0 4.0 1.0
47+
4.0 3.0 2.0
48+
```
3149
"""
3250
function hessfact{T}(A::StridedMatrix{T})
3351
S = promote_type(Float32, typeof(one(T)/norm(one(T))))

base/operators.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,20 @@ fldmod1{T<:Integer}(x::T, y::T) = (fld1(x,y), mod1(x,y))
531531
ctranspose(A)
532532
533533
The conjugate transposition operator (`'`).
534+
535+
# Example
536+
537+
```jldoctest
538+
julia> A = [3+2im 9+2im; 8+7im 4+6im]
539+
2×2 Array{Complex{Int64},2}:
540+
3+2im 9+2im
541+
8+7im 4+6im
542+
543+
julia> ctranspose(A)
544+
2×2 Array{Complex{Int64},2}:
545+
3-2im 8-7im
546+
9-2im 4-6im
547+
```
534548
"""
535549
ctranspose(x) = conj(transpose(x))
536550
conj(x) = x

doc/stdlib/linalg.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,24 @@ Linear algebra functions in Julia are largely implemented by calling functions f
11101110
11111111
Compute the Hessenberg decomposition of ``A`` and return a ``Hessenberg`` object. If ``F`` is the factorization object, the unitary matrix can be accessed with ``F[:Q]`` and the Hessenberg matrix with ``F[:H]``\ . When ``Q`` is extracted, the resulting type is the ``HessenbergQ`` object, and may be converted to a regular matrix with :func:`convert` (or ``Array(_)`` for short).
11121112

1113+
**Example**
1114+
1115+
.. doctest::
1116+
1117+
julia> A = [4. 9. 7.; 4. 4. 1.; 4. 3. 2.]
1118+
3×3 Array{Float64,2}:
1119+
4.0 9.0 7.0
1120+
4.0 4.0 1.0
1121+
4.0 3.0 2.0
1122+
1123+
julia> F = hessfact(A);
1124+
1125+
julia> F[:Q] * F[:H] * F[:Q]'
1126+
3×3 Array{Float64,2}:
1127+
4.0 9.0 7.0
1128+
4.0 4.0 1.0
1129+
4.0 3.0 2.0
1130+
11131131
.. function:: hessfact!(A) -> Hessenberg
11141132

11151133
.. Docstring generated from Julia source
@@ -1808,6 +1826,16 @@ Linear algebra functions in Julia are largely implemented by calling functions f
18081826

18091827
For example, if ``A`` is a matrix and ``p=2``\ , then this is equivalent to the Frobenius norm.
18101828

1829+
**Example**
1830+
1831+
.. doctest::
1832+
1833+
julia> vecnorm([1 2 3; 4 5 6; 7 8 9])
1834+
16.881943016134134
1835+
1836+
julia> vecnorm([1 2 3 4 5 6 7 8 9])
1837+
16.881943016134134
1838+
18111839
.. function:: normalize!(v, [p::Real=2])
18121840

18131841
.. Docstring generated from Julia source
@@ -1899,6 +1927,21 @@ Linear algebra functions in Julia are largely implemented by calling functions f
18991927
19001928
Log of matrix determinant. Equivalent to ``log(det(M))``\ , but may provide increased accuracy and/or speed.
19011929

1930+
**Example**
1931+
1932+
.. doctest::
1933+
1934+
julia> M = [1 0; 2 2]
1935+
2×2 Array{Int64,2}:
1936+
1 0
1937+
2 2
1938+
1939+
julia> logdet(M)
1940+
0.6931471805599453
1941+
1942+
julia> logdet(eye(3))
1943+
0.0
1944+
19021945
.. function:: logabsdet(M)
19031946

19041947
.. Docstring generated from Julia source
@@ -2138,6 +2181,20 @@ Linear algebra functions in Julia are largely implemented by calling functions f
21382181
21392182
.. [AHR13] Awad 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 <http://dx.doi.org/10.1137/120885991>`_
21402183
2184+
**Example**
2185+
2186+
.. doctest::
2187+
2188+
julia> A = 2.7182818 * eye(2)
2189+
2×2 Array{Float64,2}:
2190+
2.71828 0.0
2191+
0.0 2.71828
2192+
2193+
julia> logm(A)
2194+
2×2 Array{Float64,2}:
2195+
1.0 0.0
2196+
0.0 1.0
2197+
21412198
.. function:: sqrtm(A)
21422199

21432200
.. Docstring generated from Julia source
@@ -2148,6 +2205,20 @@ Linear algebra functions in Julia are largely implemented by calling functions f
21482205

21492206
.. [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 <http://dx.doi.org/10.1016/0024-3795(83)80010-X>`_
21502207
2208+
**Example**
2209+
2210+
.. doctest::
2211+
2212+
julia> A = [4 0; 0 4]
2213+
2×2 Array{Int64,2}:
2214+
4 0
2215+
0 4
2216+
2217+
julia> sqrtm(A)
2218+
2×2 Array{Float64,2}:
2219+
2.0 0.0
2220+
0.0 2.0
2221+
21512222
.. function:: lyap(A, C)
21522223

21532224
.. Docstring generated from Julia source
@@ -2320,6 +2391,22 @@ Linear algebra functions in Julia are largely implemented by calling functions f
23202391
23212392
The transposition operator (``.'``\ ).
23222393

2394+
**Example**
2395+
2396+
.. doctest::
2397+
2398+
julia> A = [1 2 3; 4 5 6; 7 8 9]
2399+
3×3 Array{Int64,2}:
2400+
1 2 3
2401+
4 5 6
2402+
7 8 9
2403+
2404+
julia> transpose(A)
2405+
3×3 Array{Int64,2}:
2406+
1 4 7
2407+
2 5 8
2408+
3 6 9
2409+
23232410
.. function:: transpose!(dest,src)
23242411

23252412
.. Docstring generated from Julia source
@@ -2332,6 +2419,20 @@ Linear algebra functions in Julia are largely implemented by calling functions f
23322419
23332420
The conjugate transposition operator (``'``\ ).
23342421

2422+
**Example**
2423+
2424+
.. doctest::
2425+
2426+
julia> A = [3+2im 9+2im; 8+7im 4+6im]
2427+
2×2 Array{Complex{Int64},2}:
2428+
3+2im 9+2im
2429+
8+7im 4+6im
2430+
2431+
julia> ctranspose(A)
2432+
2×2 Array{Complex{Int64},2}:
2433+
3-2im 8-7im
2434+
9-2im 4-6im
2435+
23352436
.. function:: ctranspose!(dest,src)
23362437

23372438
.. Docstring generated from Julia source

0 commit comments

Comments
 (0)