solutions update from 2bd3a6d
This commit is contained in:
parent
2bd3a6dcf7
commit
7bd00d196d
File diff suppressed because it is too large
Load Diff
@ -870,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
|
||||
@ -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])? (★★★)
|
||||
`No hints provided...`
|
||||
|
@ -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])? (★★★)
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c1a8ea63",
|
||||
"id": "738eba3f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 100 numpy exercises\n",
|
||||
@ -18,7 +18,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8481f467",
|
||||
"id": "f65f901e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"File automatically generated. See the documentation to update questions/answers/hints programmatically."
|
||||
@ -26,7 +26,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7c1a3673",
|
||||
"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": "6db52927",
|
||||
"id": "0d23aa5b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -46,7 +46,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2c9f898f",
|
||||
"id": "4a6a613b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
Loading…
x
Reference in New Issue
Block a user