From 3d136da45bce42bbd72649cd16dcfeb5f22b18c0 Mon Sep 17 00:00:00 2001 From: Hemanth21k Date: Mon, 7 Jul 2025 17:28:47 +0000 Subject: [PATCH 1/3] Q.78 solution expalined --- source/exercises100.ktx | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index dd8dd91..8a76260 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1090,6 +1090,30 @@ Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to comp No hints provided... < a78 +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(P0,P1,p): + ''' + Author: Hemanth Pasupuleti + Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html + + ---- Explainable solution - Slightly Faster as number of lines scale up exponentially ---- + ''' + v = P1- P0 # Shape: (n_lines,2), Compute: [(x2-x1) (y2-y1)] + v[:,[0,1]] = v[:,[1,0]] # Shape: (n_lines,2), Swap along axis to Compute: [(y2-y1) (x2-x1)] + v[:,1]*=-1 # Shape: (n_lines,2), Compute: [(y2-y1) -(x2-x1)] + norm = np.linalg.norm(v,axis=1) # Shape: (n_lines,), Compute: sqrt((x2-x1)**2 + (y2-y1)**2) + r = P0 - p # Shape: (n_lines,2), Compute: [(x1-x0) (y1-y0)] + + # np.einsum('ij,ij->i',r,v) is equivalent to np.multiply(r,v)).sum(axis=1) which is scalar product of two matrices across axis 1. + + d = np.abs(np.einsum("ij,ij->i",r,v)) / norm # Shape: (n_lines,), Compute: d = |(x1-x0)*(y2-y1)-(y1-y0)*(x1-x0)|/sqrt((x2-x1)**2 + (y2-y1)**2) + return d +print(distance(P0, P1, p)) + +##--------------- OR ---------------## def distance(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) @@ -1097,10 +1121,6 @@ def distance(P0, P1, p): U = U.reshape(len(U),1) 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)) < q79 From dbb54197e89e2708ec22b7330e7c814470ef81bb Mon Sep 17 00:00:00 2001 From: Hemanth21k Date: Wed, 23 Jul 2025 21:52:39 +0000 Subject: [PATCH 2/3] q78 explained --- source/exercises100.ktx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 8a76260..88163bb 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1094,7 +1094,7 @@ 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(P0,P1,p): +def distance_faster(P0,P1,p): ''' Author: Hemanth Pasupuleti Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html @@ -1111,17 +1111,17 @@ def distance(P0,P1,p): d = np.abs(np.einsum("ij,ij->i",r,v)) / norm # Shape: (n_lines,), Compute: d = |(x1-x0)*(y2-y1)-(y1-y0)*(x1-x0)|/sqrt((x2-x1)**2 + (y2-y1)**2) return d -print(distance(P0, P1, p)) +print(distance_faster(P0, P1, p)) ##--------------- OR ---------------## -def distance(P0, P1, p): +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 U = U.reshape(len(U),1) D = P0 + U*T - p return np.sqrt((D**2).sum(axis=1)) -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])? (★★★) From 44df2b0d3c1bd2b8c2a194874e5e8a7429e0ebf6 Mon Sep 17 00:00:00 2001 From: Hemanth21k Date: Sat, 26 Jul 2025 23:10:48 +0000 Subject: [PATCH 3/3] simplified answer.78 --- source/exercises100.ktx | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 88163bb..e615e7f 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1095,22 +1095,18 @@ 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 - - ---- Explainable solution - Slightly Faster as number of lines scale up exponentially ---- - ''' - v = P1- P0 # Shape: (n_lines,2), Compute: [(x2-x1) (y2-y1)] - v[:,[0,1]] = v[:,[1,0]] # Shape: (n_lines,2), Swap along axis to Compute: [(y2-y1) (x2-x1)] - v[:,1]*=-1 # Shape: (n_lines,2), Compute: [(y2-y1) -(x2-x1)] - norm = np.linalg.norm(v,axis=1) # Shape: (n_lines,), Compute: sqrt((x2-x1)**2 + (y2-y1)**2) - r = P0 - p # Shape: (n_lines,2), Compute: [(x1-x0) (y1-y0)] - - # np.einsum('ij,ij->i',r,v) is equivalent to np.multiply(r,v)).sum(axis=1) which is scalar product of two matrices across axis 1. + #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 - d = np.abs(np.einsum("ij,ij->i",r,v)) / norm # Shape: (n_lines,), Compute: d = |(x1-x0)*(y2-y1)-(y1-y0)*(x1-x0)|/sqrt((x2-x1)**2 + (y2-y1)**2) return d + print(distance_faster(P0, P1, p)) ##--------------- OR ---------------## @@ -1121,6 +1117,7 @@ def distance_slower(P0, P1, p): U = U.reshape(len(U),1) D = P0 + U*T - p return np.sqrt((D**2).sum(axis=1)) + print(distance_slower(P0, P1, p)) < q79