Added some new questions
This commit is contained in:
43
README.rst
43
README.rst
@@ -528,6 +528,25 @@ Craftsman
|
|||||||
Z = np.arange(20)
|
Z = np.arange(20)
|
||||||
print moving_average(Z, n=3)
|
print moving_average(Z, n=3)
|
||||||
|
|
||||||
|
5. How to get the documentation of the numpy add function from the command line ?
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
python -c "import numpy; numpy.info(numpy.add)"
|
||||||
|
|
||||||
|
6. How to negate a boolean, or to change the sign of a float inplace ?
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
# Author: Nathaniel J. Smith
|
||||||
|
|
||||||
|
Z = np.random.randint(0,2,100)
|
||||||
|
np.logical_not(arr, out=arr)
|
||||||
|
|
||||||
|
Z = np.random.uniform(-1.0,1.0,100)
|
||||||
|
np.negative(arr, out=arr)
|
||||||
|
|
||||||
|
7.
|
||||||
|
|
||||||
Artisan
|
Artisan
|
||||||
=======
|
=======
|
||||||
@@ -579,7 +598,7 @@ Artisan
|
|||||||
print distance(P0, P1, p)
|
print distance(P0, P1, p)
|
||||||
|
|
||||||
|
|
||||||
4. Consider 2 sets of points P0,P1 describing lines (2d) and a set of point P,
|
4. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P,
|
||||||
how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i]) ?
|
how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i]) ?
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
@@ -727,6 +746,28 @@ Master
|
|||||||
uZ = Z[idx]
|
uZ = Z[idx]
|
||||||
print uZ
|
print uZ
|
||||||
|
|
||||||
|
2. How to implement the Game of Life using numpy arrays ?
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
# Author: Nicolas Rougier
|
||||||
|
|
||||||
|
def iterate(Z):
|
||||||
|
# Count neighbours
|
||||||
|
N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
|
||||||
|
Z[1:-1,0:-2] + Z[1:-1,2:] +
|
||||||
|
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])
|
||||||
|
|
||||||
|
# Apply rules
|
||||||
|
birth = (N==3) & (Z[1:-1,1:-1]==0)
|
||||||
|
survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)
|
||||||
|
Z[...] = 0
|
||||||
|
Z[1:-1,1:-1][birth | survive] = 1
|
||||||
|
return Z
|
||||||
|
|
||||||
|
Z = np.random.randint(0,2,(50,50))
|
||||||
|
for i in range(100): Z = iterate(Z)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Archmaster
|
Archmaster
|
||||||
|
|||||||
Reference in New Issue
Block a user