Update generated files

This commit is contained in:
Nicolas P. Rougier 2025-02-18 08:13:08 +01:00
parent 58ed6a9a86
commit af8d83ac89
4 changed files with 263 additions and 211 deletions

File diff suppressed because it is too large Load Diff

View File

@ -202,7 +202,7 @@ color = np.dtype([("r", np.ubyte),
`hint:` `hint:`
```python ```python
Z = np.dot(np.ones((5,3)), np.ones((3,2))) Z = np.matmul(np.ones((5, 3)), np.ones((3, 2)))
print(Z) print(Z)
# Alternative solution, in Python 3.5 and above # Alternative solution, in Python 3.5 and above
@ -894,6 +894,32 @@ P0 = np.random.uniform(-10, 10, (10,2))
P1 = np.random.uniform(-10,10,(10,2)) P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10, 10, (10,2)) p = np.random.uniform(-10, 10, (10,2))
print(np.array([distance(P0,P1,p_i) for p_i in p])) print(np.array([distance(P0,P1,p_i) for p_i in p]))
# Author: Yang Wu (Broadcasting)
def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) -> np.ndarray:
x_0, y_0 = p.T # Shape -> (n points, )
x_1, y_1 = p_1.T # Shape -> (n lines, )
x_2, y_2 = p_2.T # Shape -> (n lines, )
# Displacement vector coordinates from p_1 -> p_2
dx = x_2 - x_1 # Shape -> (n lines, )
dy = y_2 - y_1 # Shape -> (n lines, )
# The 'cross product' term
cross_term = x_2 * y_1 - y_2 * x_1 # Shape -> (n lines, )
# Broadcast x_0, y_0 (n points, 1) and dx, dy, cross_term (1, n lines) -> (n points, n lines)
numerator = np.abs(
dy[np.newaxis, :] * x_0[:, np.newaxis]
- dx[np.newaxis, :] * y_0[:, np.newaxis]
+ cross_term[np.newaxis, :]
)
denominator = np.sqrt(dx**2 + dy**2) # Shape -> (n lines, )
# Shape (n points, n lines) / (1, n_lines) -> (n points, n lines)
return numerator / denominator[np.newaxis, :]
distance_points_to_lines(p, P0, P1)
``` ```
#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★) #### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
`hint: minimum maximum` `hint: minimum maximum`

View File

@ -202,7 +202,7 @@ color = np.dtype([("r", np.ubyte),
```python ```python
Z = np.dot(np.ones((5,3)), np.ones((3,2))) Z = np.matmul(np.ones((5, 3)), np.ones((3, 2)))
print(Z) print(Z)
# Alternative solution, in Python 3.5 and above # Alternative solution, in Python 3.5 and above
@ -894,6 +894,32 @@ P0 = np.random.uniform(-10, 10, (10,2))
P1 = np.random.uniform(-10,10,(10,2)) P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10, 10, (10,2)) p = np.random.uniform(-10, 10, (10,2))
print(np.array([distance(P0,P1,p_i) for p_i in p])) print(np.array([distance(P0,P1,p_i) for p_i in p]))
# Author: Yang Wu (Broadcasting)
def distance_points_to_lines(p: np.ndarray, p_1: np.ndarray, p_2: np.ndarray) -> np.ndarray:
x_0, y_0 = p.T # Shape -> (n points, )
x_1, y_1 = p_1.T # Shape -> (n lines, )
x_2, y_2 = p_2.T # Shape -> (n lines, )
# Displacement vector coordinates from p_1 -> p_2
dx = x_2 - x_1 # Shape -> (n lines, )
dy = y_2 - y_1 # Shape -> (n lines, )
# The 'cross product' term
cross_term = x_2 * y_1 - y_2 * x_1 # Shape -> (n lines, )
# Broadcast x_0, y_0 (n points, 1) and dx, dy, cross_term (1, n lines) -> (n points, n lines)
numerator = np.abs(
dy[np.newaxis, :] * x_0[:, np.newaxis]
- dx[np.newaxis, :] * y_0[:, np.newaxis]
+ cross_term[np.newaxis, :]
)
denominator = np.sqrt(dx**2 + dy**2) # Shape -> (n lines, )
# Shape (n points, n lines) / (1, n_lines) -> (n points, n lines)
return numerator / denominator[np.newaxis, :]
distance_points_to_lines(p, P0, P1)
``` ```
#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★) #### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "e06c1964", "id": "ed5b154a",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 100 numpy exercises\n", "# 100 numpy exercises\n",
@ -18,7 +18,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "c108c1c4", "id": "f21bc71d",
"metadata": {}, "metadata": {},
"source": [ "source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically." "File automatically generated. See the documentation to update questions/answers/hints programmatically."
@ -26,7 +26,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "2aefe09b", "id": "7a3d8374",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n",
@ -36,7 +36,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "55c8f855", "id": "a3aaf46b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -46,7 +46,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "8f1e8a4a", "id": "6db78cf9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [