python - I am using a numpy array of randomly generated ordered pairs, I need to determin if the ordered pairs are different types of triangles -


i jst started using numpy week, , confused it. seems different normal python functions.

with array, shape of 1000x6, there way go row row in array , check example equilateral triangle.i have 6 columns there triples in each row, 2 integers each point.

import numpy np pnts = np.random.randint(0,50,(1000, 6)) 

i thought may better create 3 arrays this:

import numpy np = np.random.random((10,2)) b = np.random.random((10,2)) c = np.random.random((10,2)) 

to create ordered pairs , use algorithm find triangle.

is there better way create array represent 1000 triples of ordered pairs , how can find triangles in array, equilateral triangle example.

i have made changes now. made 2 arrays x coordinates , y coordinates.

x = np.random.randint(0,10,(3,1000)) y = np.random.randint(0,10,(3,1000)) 

############# adding question #############

i have algorithms take each matching x , y coordinates find there side length , angles each triangle. post code. , have functions use angles , side lengths find scalene, equilateral, right isoceles, , non-right isoceles.

my question more index related. use equilateral triangle again example because have been working with.

e = np.column_stack((acxy,abxy,cbxy)) es = np.logical_and(e[:,0] == e[:,1], e[:,1] == e[:,2]) 

i have find equilateral triangles.

- acxy = distance point c - abxy = distance point b - cbxy = distance point c b 

i want able take coordinate triples equilateral triangles, index them , put them new array called e_tri. dont think need function creating boolean values. ive thought maybe if: else: statements maybe better way it.

also may too, display e = np.column_stack((acxy,abxy,cbxy)) understand array of (e).

[[  4.           4.47213595   7.21110255]  [  3.60555128   2.23606798   5.83095189]  [  2.23606798   9.05538514   8.54400375]  ...,   [  3.60555128   9.05538514   6.08276253]  [  8.94427191   8.54400375   1.        ]  [ 10.63014581   1.          10.        ]] 

e that. make sense, if not please let me know.

something perhaps, though not work adding question.

e = np.column_stack((acxy,abxy,cbxy)) equilateral = []  def e_tri(e):     if e[:,0] == e[:,1] , e[:,1] == e[:,2]:         equilateral.append(e_tri)     else:         return e 

you've described how storing data, not algorithm is. example, if want answer question "is set of 3 (x,y) points p1..p3 equilateral triangle," can formulate way:

dist(p1,p2) == dist(p2,p3) == dist(p3,p1) 

where dist(p1,p2) uses pythagorean theorem:

sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2) 

but note sqrt() unnecessary because care if 3 legs equal length (and if are, squares equal well).

in numpy want in parallelizable way. if have 1000x6 array representing 1000 triangles, need operations on 1000 elements @ time. if array called , columns are:

p1.x, p1.y, p2.x, p2.y, p3.x, p3.y 

then first operations are:

a[0] - a[2] # p1.x - p2.x a[1] - a[3] # p1.y - p2.y a[2] - a[4] a[3] - a[5] a[4] - a[0] a[5] - a[1] 

which can more succinctly written:

r = - np.roll(a, -2, axis=0) # 1000x6 array of differences 

that being done, can square 1000x6 results @ once, giving 1000x6 array r add x , y pairs squares-of-distances:

r[0] + r[1] # (p1.x - p2.x)**2 + (p1.y - p2.y)**2 r[2] + r[3] r[4] + r[5] 

which say:

s = r[0::2] + r[1::2] # 3 column-wise additions @ once 

this gives 1000x3 squares-of-distances array s. check each row if columns equal:

np.logical_and(s[0] == s[1], s[1] == s[2]) 

this gives 1000x1 boolean vector tells if each row equilateral triangle.

note never went row-by-row in iterative fashion. that's because doing in numpy slower doing column-wise operations.

note have written above assuming shape of arrays (6,1000) when 1000x6. convenience of notation (a[0] instead of a[:,0]) , because more efficient when operating on columns since numpy default uses row-major order. can np.transpose() input data if needed.

so in end it's just:

a = pnts.t r = np.square(a - np.roll(a, -2, axis=0)) s = r[0::2] + r[1::2] # 1000x3 squares of distances np.logical_and(s[0] == s[1], s[1] == s[2]) # 1000 true/false results 

Comments

  1. This topic explains how to use a **NumPy array containing randomly generated ordered pairs (coordinates)** to determine what type of triangle the points form. The three ordered pairs represent three points, such as **(x₁, y₁), (x₂, y₂), and (x₃, y₃)**. First, the distances between each pair of points can be calculated using the distance formula. These three distances represent the triangle's side lengths. By comparing the side lengths, the triangle can be classified as **equilateral** (all three sides equal), **isosceles** (two sides equal), or **scalene** (all sides different). Airflow Course.The angles can also be determined using the squared side lengths to identify whether the triangle is **right, acute, or obtuse**. NumPy makes this process easier by performing the calculations efficiently on arrays of coordinates.

    ReplyDelete

Post a Comment

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Perform a PayPal payment automatically using PHP -

configuring Airflow to work with CeleryExecutor -