Skip to content

Commit

Permalink
fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 23, 2021
1 parent a2e5a43 commit f1a6382
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
10 changes: 5 additions & 5 deletions doc/src/devdocs/subarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ julia> A = rand(2,3,4);
julia> S1 = view(A, :, 1, 2:3)
2×2 view(::Array{Float64, 3}, :, 1, 2:3) with eltype Float64:
0.200586 0.066423
0.298614 0.956753
0.762657 0.545403
0.66338 0.866199
julia> S2 = view(A, 1, :, 2:3)
3×2 view(::Array{Float64, 3}, 1, :, 2:3) with eltype Float64:
0.200586 0.066423
0.246837 0.646691
0.648882 0.276021
0.762657 0.545403
0.704125 0.0802701
0.886174 0.535537
```
```@meta
DocTestSetup = nothing
Expand Down
18 changes: 9 additions & 9 deletions doc/src/manual/performance-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ julia> function sum_global()
end;
julia> @time sum_global()
0.009639 seconds (7.36 k allocations: 300.310 KiB, 98.32% compilation time)
496.84883432553846
0.011001 seconds (9.30 k allocations: 417.044 KiB, 98.77% compilation time)
503.4466694614652
julia> @time sum_global()
0.000140 seconds (3.49 k allocations: 70.313 KiB)
496.84883432553846
0.000075 seconds (3.49 k allocations: 70.156 KiB)
503.4466694614652
```

On the first call (`@time sum_global()`) the function gets compiled. (If you've not yet used [`@time`](@ref)
Expand Down Expand Up @@ -113,12 +113,12 @@ julia> function sum_arg(x)
end;
julia> @time sum_arg(x)
0.006202 seconds (4.18 k allocations: 217.860 KiB, 99.72% compilation time)
496.84883432553846
0.007258 seconds (4.24 k allocations: 226.052 KiB, 99.74% compilation time)
503.4466694614652
julia> @time sum_arg(x)
0.000005 seconds (1 allocation: 16 bytes)
496.84883432553846
503.4466694614652
```

The 1 allocation seen is from running the `@time` macro itself in global scope. If we instead run
Expand All @@ -128,8 +128,8 @@ the timing in a function, we can see that indeed no allocations are performed:
julia> time_sum(x) = @time sum_arg(x);
julia> time_sum(x)
0.000001 seconds
496.84883432553846
0.000002 seconds
503.4466694614652
```

In some situations, your function may need to allocate memory as part of its operation, and this
Expand Down
12 changes: 6 additions & 6 deletions stdlib/Random/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,20 @@ Scalar and array methods for `Die` now work as expected:

```jldoctest Die; setup = :(Random.seed!(1))
julia> rand(Die)
Die(15)
Die(6)
julia> rand(MersenneTwister(0), Die)
Die(11)
julia> rand(Die, 3)
3-element Vector{Die}:
Die(18)
Die(5)
Die(15)
Die(19)
Die(4)
julia> a = Vector{Die}(undef, 3); rand!(a)
3-element Vector{Die}:
Die(5)
Die(17)
Die(20)
Die(15)
```
Expand All @@ -175,13 +175,13 @@ In order to define random generation out of objects of type `S`, the following m
julia> Random.rand(rng::AbstractRNG, d::Random.SamplerTrivial{Die}) = rand(rng, 1:d[].nsides);
julia> rand(Die(4))
3
1
julia> rand(Die(4), 3)
3-element Vector{Any}:
3
4
1
1
```

Given a collection type `S`, it's currently assumed that if `rand(::S)` is defined, an object of type `eltype(S)` will be produced. In the last example, a `Vector{Any}` is produced; the reason is that `eltype(Die) == Any`. The remedy is to define `Base.eltype(::Type{Die}) = Int`.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/Random/src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ number generator, see [Random Numbers](@ref).
# Examples
```jldoctest
julia> Random.seed!(3); randstring()
"Y7m62wOj"
"vZmAMp3z"
julia> randstring(MersenneTwister(3), 'a':'z', 6)
"ocucay"
julia> randstring("ACGT")
"ATTTGCGT"
"CAAACACC"
```
!!! note
Expand Down
14 changes: 7 additions & 7 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1585,13 +1585,13 @@ argument specifies a random number generator, see [Random Numbers](@ref).
# Examples
```jldoctest; setup = :(using Random; Random.seed!(1234))
julia> sprand(Bool, 2, 2, 0.5)
2×2 SparseMatrixCSC{Bool, Int64} with 1 stored entry:
⋅ ⋅
1
2×2 SparseMatrixCSC{Bool, Int64} with 4 stored entries:
1 1
1 1
julia> sprand(Float64, 3, 0.75)
3-element SparseVector{Float64, Int64} with 1 stored entry:
[3] = 0.298614
[1] = 0.66338
```
"""
function sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat, rfn::Function, ::Type{T}=eltype(rfn(r, 1))) where T
Expand Down Expand Up @@ -1632,9 +1632,9 @@ argument specifies a random number generator, see [Random Numbers](@ref).
# Examples
```jldoctest; setup = :(using Random; Random.seed!(0))
julia> sprandn(2, 2, 0.75)
2×2 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
⋅ 0.586617
⋅ 0.297336
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
1.49137 1.09378
-1.56692 1.1035
```
"""
sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) =
Expand Down

0 comments on commit f1a6382

Please sign in to comment.