Skip to content

Commit

Permalink
some doc/manual fixes found by doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Nov 22, 2013
1 parent 1325b6e commit b995da5
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 353 deletions.
122 changes: 67 additions & 55 deletions doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,31 @@ scalar.
The following example computes a weighted average of the current element
and its left and right neighbor along a 1-d grid. :

.. doctest::
.. testsetup:: *

srand(314)

.. doctest:: array-rand

julia> const x = rand(8)
8-element Float64 Array:
0.276455
0.614847
0.0601373
0.896024
0.646236
0.143959
0.0462343
0.730987
8-element Array{Float64,1}:
0.843025
0.869052
0.365105
0.699456
0.977653
0.994953
0.41084
0.809411

julia> [ 0.25*x[i-1] + 0.5*x[i] + 0.25*x[i+1] for i=2:length(x)-1 ]
6-element Float64 Array:
0.391572
0.407786
0.624605
0.583114
0.245097
0.241854
6-element Array{Float64,1}:
0.736559
0.57468
0.685417
0.912429
0.8446
0.656511

.. note:: In the above example, ``x`` is declared as constant because type
inference in Julia does not work as well on non-constant global
Expand All @@ -150,7 +154,7 @@ array of type ``Any``:
.. doctest::

julia> { i/2 for i = 1:3 }
3-element Any Array:
3-element Array{Any,1}:
0.5
1.0
1.5
Expand Down Expand Up @@ -188,16 +192,17 @@ Example:
.. doctest::

julia> x = reshape(1:16, 4, 4)
4x4 Int64 Array
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
4x4 Array{Int64,2}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16


julia> x[2:3, 2:end-1]
2x2 Int64 Array
6 10
7 11
2x2 Array{Int64,2}:
6 10
7 11

Assignment
----------
Expand Down Expand Up @@ -230,16 +235,19 @@ Example:
.. doctest::

julia> x = reshape(1:9, 3, 3)
3x3 Int64 Array
1 4 7
2 5 8
3 6 9
3x3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9

julia> x[1:2, 2:3] = -1
3x3 Int64 Array
1 -1 -1
2 -1 -1
3 6 9
-1

julia> x
3x3 Array{Int64,2}:
1 -1 -1
2 -1 -1
3 6 9

Concatenation
-------------
Expand Down Expand Up @@ -321,9 +329,9 @@ the name of the function to vectorize. Here is a simple example:

julia> methods(square)
# 4 methods for generic function "square":
square{T<:Number}(x::AbstractArray{T<:Number,1}) at operators.jl:236
square{T<:Number}(x::AbstractArray{T<:Number,2}) at operators.jl:237
square{T<:Number}(x::AbstractArray{T<:Number,N}) at operators.jl:239
square{T<:Number}(x::AbstractArray{T<:Number,1}) at operators.jl:248
square{T<:Number}(x::AbstractArray{T<:Number,2}) at operators.jl:249
square{T<:Number}(x::AbstractArray{T<:Number,N}) at operators.jl:251
square(x) at none:1

julia> square([1 2 4; 5 6 7])
Expand Down Expand Up @@ -495,13 +503,15 @@ you can use the same names with an ``sp`` prefix:
.. doctest::

julia> spzeros(3,5)
3x5 sparse matrix with 0 nonzeros:
3x5 sparse matrix with 0 Float64 nonzeros:
<BLANKLINE>

julia> speye(3,5)
3x5 sparse matrix with 3 nonzeros:
[1, 1] = 1.0
[2, 2] = 1.0
[3, 3] = 1.0
3x5 sparse matrix with 3 Float64 nonzeros:
[1, 1] = 1.0
[2, 2] = 1.0
[3, 3] = 1.0
<BLANKLINE>

The ``sparse`` function is often a handy way to construct sparse
matrices. It takes as its input a vector ``I`` of row indices, a
Expand All @@ -514,35 +524,37 @@ values. ``sparse(I,J,V)`` constructs a sparse matrix such that
julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];

julia> S = sparse(I,J,V)
5x18 sparse matrix with 4 nonzeros:
[1 , 4] = 1
[4 , 7] = 2
[5 , 9] = 3
[3 , 18] = -5
5x18 sparse matrix with 4 Int64 nonzeros:
[1 , 4] = 1
[4 , 7] = 2
[5 , 9] = 3
[3 , 18] = -5
<BLANKLINE>

The inverse of the ``sparse`` function is ``findn``, which
retrieves the inputs used to create the sparse matrix.

.. doctest::

julia> findn(S)
([1, 4, 5, 3],[4, 7, 9, 18])
([1,4,5,3],[4,7,9,18])

julia> findnz(S)
([1, 4, 5, 3],[4, 7, 9, 18],[1, 2, 3, -5])
([1,4,5,3],[4,7,9,18],[1,2,3,-5])

Another way to create sparse matrices is to convert a dense matrix
into a sparse matrix using the ``sparse`` function:

.. doctest::

julia> sparse(eye(5))
5x5 sparse matrix with 5 nonzeros:
[1, 1] = 1.0
[2, 2] = 1.0
[3, 3] = 1.0
[4, 4] = 1.0
[5, 5] = 1.0
5x5 sparse matrix with 5 Float64 nonzeros:
[1, 1] = 1.0
[2, 2] = 1.0
[3, 3] = 1.0
[4, 4] = 1.0
[5, 5] = 1.0
<BLANKLINE>

You can go in the other direction using the ``dense`` or the ``full``
function. The ``issparse`` function can be used to query if a matrix
Expand Down
18 changes: 11 additions & 7 deletions doc/manual/complex-and-rational-numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defined for complex numbers:

.. doctest::

julia> sqrt(im)
julia> sqrt(1im)
0.7071067811865476 + 0.7071067811865475im

julia> sqrt(1 + 2im)
Expand All @@ -150,8 +150,10 @@ versus ``-1 + 0im`` even though ``-1 == -1 + 0im``:
.. doctest::

julia> sqrt(-1)
ERROR: DomainError()
in sqrt at math.jl:111
ERROR: DomainError
sqrt will only return a complex result if called with a complex argument.
try sqrt(complex(x))
in sqrt at math.jl:284

julia> sqrt(-1 + 0im)
0.0 + 1.0im
Expand Down Expand Up @@ -288,14 +290,16 @@ Constructing infinite rational values is acceptable:
-Inf

julia> typeof(ans)
Rational{Int64}
Rational{Int64} (constructor with 1 method)

Trying to construct a ``NaN`` rational value, however, is not:

.. doctest::

julia> 0//0
invalid rational: 0//0
ERROR: invalid rational: 0//0
in Rational at rational.jl:7
in // at rational.jl:17

As usual, the promotion system makes interactions with other numeric
types effortless:
Expand All @@ -306,7 +310,7 @@ types effortless:
8//5

julia> 3//5 - 0.5
0.1
0.09999999999999998

julia> 2//7 * (1 + 2im)
2//7 + 4//7im
Expand All @@ -321,7 +325,7 @@ types effortless:
1//2 + 2//1im

julia> 1 + 2//3im
1//1 + 2//3im
1//1 - 2//3im

julia> 0.5 == 1//2
true
Expand Down
Loading

0 comments on commit b995da5

Please sign in to comment.