Compare commits

...

15 Commits

Author SHA1 Message Date
Nicolas P. Rougier
564a329151 Re-generated files 2025-08-26 11:15:53 +02:00
rougier
19dcbdd422 solutions update from 5c34811 2025-08-26 09:07:46 +00:00
Nicolas P. Rougier
5c3481111f
Merge pull request #244 from Gattocrucco/alt-sol-87
Alternative solution to exercise 87
2025-08-26 11:07:22 +02:00
rougier
2db57d0e16 solutions update from 836eab7 2025-08-26 09:07:01 +00:00
Nicolas P. Rougier
836eab7578
Merge pull request #242 from Gattocrucco/alt-sol-4
Alternative solution to exercise 4
2025-08-26 11:06:42 +02:00
rougier
521b1c0c75 solutions update from 18a5097 2025-08-26 09:06:26 +00:00
Nicolas P. Rougier
18a5097890
Merge pull request #243 from Gattocrucco/improve-sol-82
Improve solution to exercise 82
2025-08-26 11:06:02 +02:00
Gattocrucco
5b6d050a52
reset derived files to original state 2025-08-26 10:56:55 +02:00
Gattocrucco
8ea76d1f91
reset derived files to original state 2025-08-26 10:56:42 +02:00
Gattocrucco
fc72693160
reset derived files to original state 2025-08-26 10:56:10 +02:00
Gattocrucco
301e0eb61e
further simplification to the alt solution of 87
The initial formulation contained a redundant `swapaxes`.
2025-08-26 00:16:34 +02:00
Gattocrucco
59a403da66
alternative solution to exercise 87
Added a more elegant (but more cryptic) solution with reshape and
swapaxes.

Removed one of the existing alternative solutions because it was a
duplicate of a previous alternative solution.
2025-08-25 18:13:30 +02:00
Gattocrucco
fca297a6e1
improve solution to exercise 82
The current solution uses a fixed arbitrary threshold of 1e-10 on the
singular values, I replaced it with the common heuristic n*eps*max(s).
2025-08-25 18:06:25 +02:00
Giacomo Petrillo
ed0a067b01 re-generate markdown files 2025-08-25 17:57:43 +02:00
Giacomo Petrillo
ee15dec2c7 alt solution to exercise 4
Uses nbytes instead of itemsize * size
2025-08-25 17:53:21 +02:00
5 changed files with 232 additions and 229 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,6 +38,9 @@ print(Z)
```python
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
# Simpler alternative
print("%d bytes" % Z.nbytes)
```
#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
`hint: np.info`
@ -993,7 +996,8 @@ print(sliding_window_view(Z, window_shape=4))
Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10)
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
rank = np.sum(S > threshold)
print(rank)
# alternative solution:
@ -1086,11 +1090,8 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
# alternative solution (by @Gattocrucco)
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
```
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
`No hints provided...`

View File

@ -38,6 +38,9 @@ print(Z)
```python
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
# Simpler alternative
print("%d bytes" % Z.nbytes)
```
#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
@ -993,7 +996,8 @@ print(sliding_window_view(Z, window_shape=4))
Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10)
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
rank = np.sum(S > threshold)
print(rank)
# alternative solution:
@ -1086,11 +1090,8 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
# alternative solution (by @Gattocrucco)
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
```
#### 88. How to implement the Game of Life using numpy arrays? (★★★)

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "738eba3f",
"id": "af9e3994",
"metadata": {},
"source": [
"# 100 numpy exercises\n",
@ -18,7 +18,7 @@
},
{
"cell_type": "markdown",
"id": "f65f901e",
"id": "76e99d49",
"metadata": {},
"source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
@ -26,17 +26,17 @@
},
{
"cell_type": "markdown",
"id": "15045647",
"id": "8a54b21a",
"metadata": {},
"source": [
"Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n",
"Run the `initialise.py` module, then call a random question with `pick()` an hint towards its solution with\n",
"`hint(n)` and the answer with `answer(n)`, where n is the number of the picked question."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d23aa5b",
"id": "670bb646",
"metadata": {},
"outputs": [],
"source": [
@ -46,7 +46,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4a6a613b",
"id": "4ff49967",
"metadata": {},
"outputs": [],
"source": [

View File

@ -37,6 +37,9 @@ hint: size, itemsize
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
# Simpler alternative
print("%d bytes" % Z.nbytes)
< q5
How to get the documentation of the numpy add function from the command line? (★☆☆)
@ -1225,7 +1228,8 @@ hint: np.linalg.svd, np.linalg.matrix_rank
Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10)
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
rank = np.sum(S > threshold)
print(rank)
# alternative solution:
@ -1333,11 +1337,8 @@ k = 4
windows = np.lib.stride_tricks.sliding_window_view(Z, (k, k))
S = windows[::k, ::k, ...].sum(axis=(-2, -1))
# Author: Jeff Luo (@Jeff1999)
Z = np.ones((16, 16))
k = 4
print(sliding_window_view(Z, window_shape=(k, k))[::k, ::k].sum(axis=(-2, -1)))
# alternative solution (by @Gattocrucco)
S = Z.reshape(4, 4, 4, 4).sum((1, 3))
< q88
How to implement the Game of Life using numpy arrays? (★★★)