Skip to content

Commit

Permalink
DOC: fix python syntax errors in tutorials
Browse files Browse the repository at this point in the history
Only deal with trivial syntax errors in this commit.
  • Loading branch information
ev-br committed Nov 9, 2015
1 parent 4b69874 commit ad285e6
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 275 deletions.
70 changes: 36 additions & 34 deletions doc/source/tutorial/arpack.rst
Expand Up @@ -113,16 +113,18 @@ of ``X`` and compare them to the known results:
>>> print evals_large
[ 29.1446102 30.05821805 31.19467646]
>>> print np.dot(evecs_large.T, evecs_all[:,-3:])
[[-1. 0. 0.]
[ 0. 1. 0.]
[-0. 0. -1.]]
array([[-1. 0. 0.], # may vary (signs)
[ 0. 1. 0.],
[-0. 0. -1.]])

The results are as expected. ARPACK recovers the desired eigenvalues, and they
match the previously known results. Furthermore, the eigenvectors are
orthogonal, as we'd expect. Now let's attempt to solve for the eigenvalues
with smallest magnitude:

>>> evals_small, evecs_small = eigsh(X, 3, which='SM')
Traceback (most recent call last):
...
scipy.sparse.linalg.eigen.arpack.arpack.ArpackNoConvergence:
ARPACK error -1: No convergence (1001 iterations, 0/3 eigenvectors converged)

Expand All @@ -132,27 +134,27 @@ addressed. We could increase the tolerance (``tol``) to lead to faster
convergence:

>>> evals_small, evecs_small = eigsh(X, 3, which='SM', tol=1E-2)
>>> print evals_all[:3]
[ 0.0003783 0.00122714 0.00715878]
>>> print evals_small
[ 0.00037831 0.00122714 0.00715881]
>>> print np.dot(evecs_small.T, evecs_all[:,:3])
[[ 0.99999999 0.00000024 -0.00000049]
[-0.00000023 0.99999999 0.00000056]
[ 0.00000031 -0.00000037 0.99999852]]
>>> evals_all[:3]
array([0.0003783, 0.00122714, 0.00715878])
>>> evals_small
array([0.00037831, 0.00122714, 0.00715881])
>>> np.dot(evecs_small.T, evecs_all[:,:3])
array([[ 0.99999999 0.00000024 -0.00000049], # may vary (signs)
[-0.00000023 0.99999999 0.00000056],
[ 0.00000031 -0.00000037 0.99999852]])

This works, but we lose the precision in the results. Another option is
to increase the maximum number of iterations (``maxiter``) from 1000 to 5000:

>>> evals_small, evecs_small = eigsh(X, 3, which='SM', maxiter=5000)
>>> print evals_all[:3]
[ 0.0003783 0.00122714 0.00715878]
>>> print evals_small
[ 0.0003783 0.00122714 0.00715878]
>>> print np.dot(evecs_small.T, evecs_all[:,:3])
[[ 1. 0. 0.]
[-0. 1. 0.]
[ 0. 0. -1.]]
>>> evals_all[:3]
array([0.0003783, 0.00122714, 0.00715878])
>>> evals_small
array([0.0003783, 0.00122714, 0.00715878])
>>> np.dot(evecs_small.T, evecs_all[:,:3])
array([[ 1. 0. 0.], # may vary (signs)
[-0. 1. 0.],
[ 0. 0. -1.]])

We get the results we'd hoped for, but the computation time is much longer.
Fortunately, ``ARPACK`` contains a mode that allows quick determination of
Expand All @@ -164,14 +166,14 @@ then satisfy :math:`\nu = 1/(\sigma - \lambda) = 1/\lambda`, so our
small eigenvalues :math:`\lambda` become large eigenvalues :math:`\nu`.

>>> evals_small, evecs_small = eigsh(X, 3, sigma=0, which='LM')
>>> print evals_all[:3]
[ 0.0003783 0.00122714 0.00715878]
>>> print evals_small
[ 0.0003783 0.00122714 0.00715878]
>>> print np.dot(evecs_small.T, evecs_all[:,:3])
[[ 1. 0. 0.]
[ 0. -1. -0.]
[-0. -0. 1.]]
>>> evals_all[:3]
array([0.0003783, 0.00122714, 0.00715878])
>>> evals_small
array([0.0003783, 0.00122714, 0.00715878])
>>> np.dot(evecs_small.T, evecs_all[:,:3])
array([[ 1. 0. 0.], # may vary (signs)
[ 0. -1. -0.],
[-0. -0. 1.]])

We get the results we were hoping for, with much less computational time.
Note that the transformation from :math:`\nu \to \lambda` takes place
Expand All @@ -185,14 +187,14 @@ the rest:

>>> evals_mid, evecs_mid = eigsh(X, 3, sigma=1, which='LM')
>>> i_sort = np.argsort(abs(1. / (1 - evals_all)))[-3:]
>>> print evals_all[i_sort]
[ 1.16577199 0.85081388 1.06642272]
>>> print evals_mid
[ 0.85081388 1.06642272 1.16577199]
>>> evals_all[i_sort]
array([1.16577199, 0.85081388, 1.06642272])
>>> evals_mid
array([0.85081388, 1.06642272, 1.16577199])
>>> print np.dot(evecs_mid.T, evecs_all[:,i_sort])
[[-0. 1. 0.]
[-0. -0. 1.]
[ 1. 0. 0.]]
array([[-0. 1. 0.], # may vary (signs)
[-0. -0. 1.],
[ 1. 0. 0.]]

The eigenvalues come out in a different order, but they're all there.
Note that the shift-invert mode requires the internal solution of a matrix
Expand Down
21 changes: 11 additions & 10 deletions doc/source/tutorial/basic.rst
Expand Up @@ -41,11 +41,11 @@ part will discuss the operation of :obj:`np.mgrid` , :obj:`np.ogrid` ,

For example, rather than writing something like the following

>>> concatenate(([3],[0]*5,arange(-1,1.002,2/9.0)))
>>> a = np.concatenate(([3], [0]*5, np.arange(-1, 1.002, 2/9.0)))

with the :obj:`r_` command one can enter this as

>>> r_[3,[0]*5,-1:1:10j]
>>> a = np.r_[3,[0]*5,-1:1:10j]

which can ease typing and make for more readable code. Notice how
objects are concatenated, and the slicing syntax is (ab)used to
Expand Down Expand Up @@ -76,7 +76,7 @@ N-d arrays which provide coordinate arrays for an N-dimensional
volume. The easiest way to understand this is with an example of its
usage:

>>> mgrid[0:5,0:5]
>>> np.mgrid[0:5,0:5]
array([[[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
Expand All @@ -87,7 +87,7 @@ usage:
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]])
>>> mgrid[0:5:4j,0:5:4j]
>>> np.mgrid[0:5:4j,0:5:4j]
array([[[ 0. , 0. , 0. , 0. ],
[ 1.6667, 1.6667, 1.6667, 1.6667],
[ 3.3333, 3.3333, 3.3333, 3.3333],
Expand Down Expand Up @@ -129,6 +129,7 @@ polynomial. The polynomial object can then be manipulated in algebraic
expressions, integrated, differentiated, and evaluated. It even prints
like a polynomial:

>>> from numpy import poly1d
>>> p = poly1d([3,4,5])
>>> print p
2
Expand All @@ -137,11 +138,11 @@ like a polynomial:
4 3 2
9 x + 24 x + 46 x + 40 x + 25
>>> print p.integ(k=6)
3 2
x + 2 x + 5 x + 6
3 2
1 x + 2 x + 5 x + 6
>>> print p.deriv()
6 x + 4
>>> p([4,5])
>>> p([4, 5])
array([ 69, 100])

The other way to handle polynomials is as an array of coefficients
Expand Down Expand Up @@ -170,7 +171,7 @@ ufuncs). For example, suppose you have a Python function named
which defines a function of two scalar variables and returns a scalar
result. The class vectorize can be used to "vectorize "this function so that ::

>>> vec_addsubtract = vectorize(addsubtract)
>>> vec_addsubtract = np.vectorize(addsubtract)

returns a function which takes array arguments and returns an array
result:
Expand Down Expand Up @@ -238,10 +239,10 @@ a list of conditions. Each element of the return array is taken from
the array in a ``choicelist`` corresponding to the first condition in
``condlist`` that is true. For example

>>> x = r_[-2:3]
>>> x = np.r_[-2:3]
>>> x
array([-2, -1, 0, 1, 2])
>>> np.select([x > 3, x >= 0],[0,x+2])
>>> np.select([x > 3, x >= 0], [0, x+2])
array([0, 0, 2, 3, 4])

Some additional useful functions can also be found in the module
Expand Down
8 changes: 4 additions & 4 deletions doc/source/tutorial/csgraph.rst
Expand Up @@ -119,8 +119,8 @@ reconstruct this path::
>>> path = []
>>> i = i2
>>> while i != i1:
>>> path.append(word_list[i])
>>> i = predecessors[i]
... path.append(word_list[i])
... i = predecessors[i]
>>> path.append(word_list[i1])
>>> print path[::-1]
['ape', 'apt', 'opt', 'oat', 'mat', 'man']
Expand Down Expand Up @@ -198,8 +198,8 @@ hand. We can find the connecting list in the same way as above::
>>> path = []
>>> i = i2[0]
>>> while i != i1[0]:
>>> path.append(word_list[i])
>>> i = predecessors[i1[0], i]
... path.append(word_list[i])
... i = predecessors[i1[0], i]
>>> path.append(word_list[i1[0]])
>>> print path[::-1]
['imp', 'amp', 'asp', 'ask', 'ark', 'are', 'aye', 'rye', 'roe', 'woe', 'woo', 'who', 'oho', 'ohm']
Expand Down
52 changes: 28 additions & 24 deletions doc/source/tutorial/fftpack.rst
Expand Up @@ -53,11 +53,12 @@ respectively as shown in the following example.
>>> x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
>>> y = fft(x)
>>> y
[ 4.50000000+0.j 2.08155948-1.65109876j -1.83155948+1.60822041j
-1.83155948-1.60822041j 2.08155948+1.65109876j]
array([ 4.50000000+0.j , 2.08155948-1.65109876j,
-1.83155948+1.60822041j, -1.83155948-1.60822041j,
2.08155948+1.65109876j])
>>> yinv = ifft(y)
>>> yinv
[ 1.0+0.j 2.0+0.j 1.0+0.j -1.0+0.j 1.5+0.j]
array([ 1.0+0.j, 2.0+0.j, 1.0+0.j, -1.0+0.j, 1.5+0.j])


From the definition of the FFT it can be seen that
Expand Down Expand Up @@ -148,10 +149,10 @@ The function :func:`fftfreq` returns the FFT sample frequency points.
In a similar spirit, the function :func:`fftshift` allows swapping the lower
and upper halves of a vector, so that it becomes suitable for display.

>>> from scipy.fftpack import fftfreq
>>> from scipy.fftpack import fftshift
>>> x = np.arange(8)
>>> sf.fftshift(x)
[4 5 6 7 0 1 2 3]
>>> fftshift(x)
array([4, 5, 6, 7, 0, 1, 2, 3])

The example below plots the FFT of two complex exponentials; note the
asymmetric spectrum.
Expand Down Expand Up @@ -187,18 +188,22 @@ coefficients with this special ordering.
>>> from scipy.fftpack import fft, rfft, irfft
>>> x = np.array([1.0, 2.0, 1.0, -1.0, 1.5, 1.0])
>>> fft(x)
[ 5.50+0.j 2.25-0.4330127j -2.75-1.29903811j 1.50+0.j
-2.75+1.29903811j 2.25+0.4330127j ]
array([ 5.50+0.j , 2.25-0.4330127j , -2.75-1.29903811j,
1.50+0.j , -2.75+1.29903811j, 2.25+0.4330127j ])
>>> yr = rfft(x)
[ 5.5 2.25 -0.4330127 -2.75 -1.29903811 1.5 ]
>>> yr
array([ 5.5 , 2.25 , -0.4330127 , -2.75 , -1.29903811,
1.5 ])
>>> irfft(yr)
[ 1. 2. 1. -1. 1.5 1. ]
array([ 1. , 2. , 1. , -1. , 1.5, 1. ])
>>> x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
>>> fft(x)
[ 4.50000000+0.j 2.08155948-1.65109876j -1.83155948+1.60822041j
-1.83155948-1.60822041j 2.08155948+1.65109876j]
array([ 4.50000000+0.j , 2.08155948-1.65109876j,
-1.83155948+1.60822041j, -1.83155948-1.60822041j,
2.08155948+1.65109876j])
>>> yr = rfft(x)
[ 4.5 2.08155948 -1.65109876 -1.83155948 1.60822041]
>>> yr
array([ 4.5 , 2.08155948, -1.65109876, -1.83155948, 1.60822041])


Two and n-dimensional discrete Fourier transforms
Expand Down Expand Up @@ -339,20 +344,19 @@ and normalizations.
[1.0, 2.0, 1.0, -1.0, 1.5]
>>> # scaling factor 2*N = 10
>>> idct(dct(x, type=2), type=2)
[ 10. 20. 10. -10. 15.]
array([ 10., 20., 10., -10., 15.])
>>> # no scaling factor
>>> idct(dct(x, type=2, norm='ortho'), type=2, norm='ortho')
[ 1. 2. 1. -1. 1.5]
array([ 1. , 2. , 1. , -1. , 1.5])
>>> # scaling factor 2*N = 10
>>> idct(dct(x, type=3), type=3)
[ 10. 20. 10. -10. 15.]
array([ 10., 20., 10., -10., 15.])
>>> # no scaling factor
>>> idct(dct(x, type=3, norm='ortho'), type=3, norm='ortho')
[ 1. 2. 1. -1. 1.5]
array([ 1. , 2. , 1. , -1. , 1.5])
>>> # scaling factor 2*(N-1) = 8
>>> idct(dct(x, type=1), type=1)
[ 8. 16. 8. -8. 12.]

array([ 8., 16., 8., -8., 12.])

Example
_______
Expand Down Expand Up @@ -454,19 +458,19 @@ and normalizations.
>>> x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
>>> # scaling factor 2*N = 10
>>> idst(dst(x, type=2), type=2)
[ 10. 20. 10. -10. 15.]
array([ 10., 20., 10., -10., 15.])
>>> # no scaling factor
>>> idst(dst(x, type=2, norm='ortho'), type=2, norm='ortho')
[ 1. 2. 1. -1. 1.5]
array([ 1. , 2. , 1. , -1. , 1.5])
>>> # scaling factor 2*N = 10
>>> idst(dst(x, type=3), type=3)
[ 10. 20. 10. -10. 15.]
array([ 10., 20., 10., -10., 15.])
>>> # no scaling factor
>>> idst(dst(x, type=3, norm='ortho'), type=3, norm='ortho')
[ 1. 2. 1. -1. 1.5]
array([ 1. , 2. , 1. , -1. , 1.5])
>>> # scaling factor 2*(N+1) = 8
>>> idst(dst(x, type=1), type=1)
[ 8. 16. 8. -8. 12.]
array([ 12., 24., 12., -12., 18.])


Cache Destruction
Expand Down

0 comments on commit ad285e6

Please sign in to comment.