Merge branch 'rougier:master' into typo_fix

This commit is contained in:
Hemanth 2025-07-30 19:00:32 +00:00 committed by GitHub
commit 7022d36e7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 281 additions and 239 deletions

File diff suppressed because it is too large Load Diff

View File

@ -96,15 +96,6 @@ print(Z)
Z = np.random.random((3,3,3))
print(Z)
```
`hint: np.random.default_rng().random`
```python
# Author: KnightSnape
rng = np.random.default_rng()
Z = rng.random((3, 3, 3))
print(Z)
```
#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
`hint: min, max`
@ -879,7 +870,27 @@ np.negative(Z, out=Z)
`No hints provided...`
```python
def distance(P0, P1, p):
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
def distance_faster(P0,P1,p):
#Author: Hemanth Pasupuleti
#Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
v = P1- P0
v[:,[0,1]] = v[:,[1,0]]
v[:,1]*=-1
norm = np.linalg.norm(v,axis=1)
r = P0 - p
d = np.abs(np.einsum("ij,ij->i",r,v)) / norm
return d
print(distance_faster(P0, P1, p))
##--------------- OR ---------------##
def distance_slower(P0, P1, p):
T = P1 - P0
L = (T**2).sum(axis=1)
U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L
@ -887,10 +898,7 @@ def distance(P0, P1, p):
D = P0 + U*T - p
return np.sqrt((D**2).sum(axis=1))
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
print(distance(P0, P1, p))
print(distance_slower(P0, P1, p))
```
#### 79. 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])? (★★★)
`No hints provided...`
@ -956,8 +964,8 @@ Z_start = (np.maximum(Z_start,0)).tolist()
R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
Z_stop = (np.minimum(Z_stop,Zs)).tolist()
r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)])
z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)])
R[r] = Z[z]
print(Z)
print(R)

View File

@ -870,7 +870,27 @@ np.negative(Z, out=Z)
```python
def distance(P0, P1, p):
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
def distance_faster(P0,P1,p):
#Author: Hemanth Pasupuleti
#Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
v = P1- P0
v[:,[0,1]] = v[:,[1,0]]
v[:,1]*=-1
norm = np.linalg.norm(v,axis=1)
r = P0 - p
d = np.abs(np.einsum("ij,ij->i",r,v)) / norm
return d
print(distance_faster(P0, P1, p))
##--------------- OR ---------------##
def distance_slower(P0, P1, p):
T = P1 - P0
L = (T**2).sum(axis=1)
U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L
@ -878,10 +898,7 @@ def distance(P0, P1, p):
D = P0 + U*T - p
return np.sqrt((D**2).sum(axis=1))
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
print(distance(P0, P1, p))
print(distance_slower(P0, P1, p))
```
#### 79. 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])? (★★★)
@ -947,8 +964,8 @@ Z_start = (np.maximum(Z_start,0)).tolist()
R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
Z_stop = (np.minimum(Z_stop,Zs)).tolist()
r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)])
z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)])
R[r] = Z[z]
print(Z)
print(R)

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "ea6cbc4b",
"id": "738eba3f",
"metadata": {},
"source": [
"# 100 numpy exercises\n",
@ -18,7 +18,7 @@
},
{
"cell_type": "markdown",
"id": "354a533b",
"id": "f65f901e",
"metadata": {},
"source": [
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
@ -26,7 +26,7 @@
},
{
"cell_type": "markdown",
"id": "9efa41bf",
"id": "15045647",
"metadata": {},
"source": [
"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",
"execution_count": null,
"id": "1a6e8fdb",
"id": "0d23aa5b",
"metadata": {},
"outputs": [],
"source": [
@ -46,7 +46,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d1e7d785",
"id": "4a6a613b",
"metadata": {},
"outputs": [],
"source": [

View File

@ -1090,7 +1090,27 @@ Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to comp
No hints provided...
< a78
def distance(P0, P1, p):
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
def distance_faster(P0,P1,p):
#Author: Hemanth Pasupuleti
#Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
v = P1- P0
v[:,[0,1]] = v[:,[1,0]]
v[:,1]*=-1
norm = np.linalg.norm(v,axis=1)
r = P0 - p
d = np.abs(np.einsum("ij,ij->i",r,v)) / norm
return d
print(distance_faster(P0, P1, p))
##--------------- OR ---------------##
def distance_slower(P0, P1, p):
T = P1 - P0
L = (T**2).sum(axis=1)
U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L
@ -1098,10 +1118,7 @@ def distance(P0, P1, p):
D = P0 + U*T - p
return np.sqrt((D**2).sum(axis=1))
P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
print(distance(P0, P1, p))
print(distance_slower(P0, P1, p))
< q79
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])? (★★★)
@ -1173,8 +1190,8 @@ Z_start = (np.maximum(Z_start,0)).tolist()
R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()
Z_stop = (np.minimum(Z_stop,Zs)).tolist()
r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
r = tuple([slice(start,stop) for start,stop in zip(R_start,R_stop)])
z = tuple([slice(start,stop) for start,stop in zip(Z_start,Z_stop)])
R[r] = Z[z]
print(Z)
print(R)