Skip to content

Commit

Permalink
fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
fkastner committed Mar 13, 2020
1 parent d9cf76a commit ba42ef3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ julia> using NiceNumbers

julia> n = NiceNumber(2,3,5);
Nice number:
2//1 + 3//1 5
2 + 3 5

julia> n^2
Nice number:
49//1 + 12//1 5
49 + 12 5

julia> m = NiceNumber(3//5)
Nice number:
3//5

julia> n+m, n-m, n*m, n/m
(13//5 + 3//1 5, 7//5 + 3//1 5, 6//5 + 9//5 5, 10//3 + 5//1 5)
(13//5 + 3 5, 7//5 + 3 5, 6//5 + 9//5 5, 10//3 + 5 5)

julia> sqrt(m)
Nice number:
0//1 + 1//5 15
1//5 15

julia> sqrt(n)
ERROR: That's not nice anymore!
Expand All @@ -54,12 +54,14 @@ ERROR: That's not nice anymore!

There is also a macro to simplify working with nice numbers:
```julia
julia> using LinearAlgebra

julia> n = norm([4,12,3] * 2)
18.38477631085024

julia> @nice m = norm([4,12,3] * 2)
Nice number:
0//1 + 13//1 2
13 2

julia> n == m
true
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ makedocs(
"Index" => "functions.md",
"Example: SVD" => "example_svd.md"],
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
strict = true
)

deploydocs(repo = "github.com/fkastner/NiceNumbers.jl.git")
46 changes: 23 additions & 23 deletions docs/src/example_svd.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ Decide on an orthogonal basis in ``\mathbb{R}^m`` (here ``m=3``)
```jldoctest SVD
julia> u1 = NiceNumber[3,12,4]
3-element Array{NiceNumber,1}:
3//1
12//1
4//1
3
12
4
julia> u2 = NiceNumber[-4,0,3]
3-element Array{NiceNumber,1}:
-4//1
0//1
3//1
-4
0
3
julia> u3 = cross(u1,u2)
3-element Array{NiceNumber,1}:
36//1
-25//1
48//1
36
-25
48
```
and take note of their norms
```jldoctest SVD
julia> n1 = norm(u1)
Nice number:
13//1
13
julia> n2 = norm(u2)
Nice number:
5//1
5
julia> n3 = norm(u3)
Nice number:
65//1
65
julia> n3 == n1*n2
true
Expand All @@ -61,7 +61,7 @@ to construct ``U``:
julia> U = 1/n3 * [u1*n2 u2*n1 u3]
3×3 Array{NiceNumber,2}:
3//13 -4//5 36//65
12//13 0//1 -5//13
12//13 0 -5//13
4//13 3//5 48//65
```
The same procedure can in principle be used to construct a second unitary matrix in
Expand All @@ -77,33 +77,33 @@ Now we fix the singular values and construct some auxiliary matrices:
```jldoctest SVD
julia> Σ = diagm(NiceNumber[13,5])
2×2 Array{NiceNumber,2}:
13//1 0//1
0//1 5//1
13 0
0 5
julia> S = [Σ;0 0]
3×2 Array{NiceNumber,2}:
13//1 0//1
0//1 5//1
0//1 0//1
13 0
0 5
0 0
julia> R = pinv(S)
2×3 Array{NiceNumber,2}:
1//13 0//1 0//1
0//1 1//5 0//1
1//13 0 0
0 1//5 0
```
And we're ready to construct our matrix ``A`` and it's pseudoinverse ``A^\dagger``:
```jldoctest SVD
julia> A = U*S*V'
3×2 Array{NiceNumber,2}:
-5//1 0//1
-5 0
-36//5 48//5
0//1 5//1
0 5
julia> A⁺ = V*R*U'
2×3 Array{NiceNumber,2}:
-2929//21125 -36//845 1728//21125
-1728//21125 48//845 1921//21125
julia> A⁺ ≈ pinv(A)
julia> float(A⁺) ≈ pinv(float(A))
true
```
12 changes: 7 additions & 5 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ julia> using NiceNumbers
julia> n = NiceNumber(2,3,5)
Nice number:
2//1 + 3//1 ⋅ √5
2 + 3 ⋅ √5
julia> n^2
Nice number:
49//1 + 12//1 ⋅ √5
49 + 12 ⋅ √5
julia> m = NiceNumber(3//5)
Nice number:
3//5
julia> n+m, n-m, n*m, n/m
(13//5 + 3//1 ⋅ √5, 7//5 + 3//1 ⋅ √5, 6//5 + 9//5 ⋅ √5, 10//3 + 5//1 ⋅ √5)
(13//5 + 3 ⋅ √5, 7//5 + 3 ⋅ √5, 6//5 + 9//5 ⋅ √5, 10//3 + 5 ⋅ √5)
julia> sqrt(m)
Nice number:
0//1 + 1//5 ⋅ √15
1//5 ⋅ √15
julia> sqrt(n)
ERROR: That's not nice anymore!
Expand All @@ -49,12 +49,14 @@ ERROR: That's not nice anymore!

There is also a macro to simplify working with nice numbers:
```jldoctest index
julia> using LinearAlgebra
julia> n = norm([4,12,3] * √2)
18.38477631085024
julia> @nice m = norm([4,12,3] * √2)
Nice number:
0//1 + 13//1 ⋅ √2
13 ⋅ √2
julia> n == m
true
Expand Down
7 changes: 7 additions & 0 deletions src/NiceNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ inv(n::NiceNumber) = NiceNumber(n.a, -n.coeff, n.radicand) * inv(n.a^2 - n.coeff
/(n::NiceNumber, m::NiceNumber) = n * inv(m)

sqrt(n::NiceNumber) = isrational(n) ? NiceNumber(0, 1, n.a) : error("That's not nice anymore!")
"""
nthroot(m::NiceNumber, n)
Returns the nth root of `m`. Works by repeatedly determining the square root and thus only for
powers of two.
"""
function nthroot(m::NiceNumber, n)
!ispow2(n) && error("That's not nice anymore!")
while n > 1
Expand All @@ -130,6 +136,7 @@ end
<(n::NiceNumber, m::NiceNumber) = float(n) < float(m)
<=(n::NiceNumber, m::NiceNumber) = n === m || n < m
==(n::NiceNumber, m::AbstractFloat) = float(n) == m
==(m::AbstractFloat, n::NiceNumber) = n == m

//(n::S, m::T) where {S<:Union{NiceNumber,Integer,Rational},T<:Union{NiceNumber,Integer,Rational}} =
n / m
Expand Down

0 comments on commit ba42ef3

Please sign in to comment.