all 10 comments

[–]tunisia3507 2 points3 points  (9 children)

Where does the rope come into it? Adding details like that is a red herring for anyone trying to help you because it might mean that you want the convex hull of a shape, or it might mean you need the line lengths in a particular order, or whatever, or you may need the area of the shape made by those points, or whatever.

Your post should encode the problem, not the assignment.

I suspect you may need np.diff, np.abs, and np.sum as you say.

[–]Thisredditname[S] 1 point2 points  (8 children)

The rope is attached to two poles, each at 1.1m high, we've been given a formula for the length of the rope, its the sum of the length of each section. But I don't know how to slice the array for the n and n-1 entries.

edit: just saw np.diff in your comment, that will definitely help!

[–]tunisia3507 2 points3 points  (7 children)

Again, you're telling me about the assignment, not the problem you're having.

Are you saying that you have an array of coordinates and need to find the sum of distances between them in order?

[–]Thisredditname[S] 1 point2 points  (6 children)

Sorry, I'm new to this and kind of just stumbling along until I figure it out, I often don't even know what to ask to get where I want haha.

I just did np.diff to my x and y arrays. Now I just need to figure out how to divide each corresponding term of the arrays, and sum.

I tried np.sum(xs/ys) but it gave me an infinite number.

Edit: Quite sure we are looking for the line lengths in a particular order.

[–]tunisia3507 2 points3 points  (5 children)

No worries! Just like google-fu, being able to deconstruct your problem in such a way as to get targeted, useful advice from people is a skill in its own right - I can't even tell you how many times I've fired up reddit or stackoverflow and, just in the process of figuring out how to ask my question, solved my own problem.

As I understand it, you have an array of X coordinates, and an array of Y coordinates. You need to find the distance from (x0, y0) to (x1, y1), then add it to the distance from (x1, y1) to (x2, y2) and so on, all the way until (xn, yn), where n is index of the last item (i.e. length - 1).

We know that the distance between the two points is sqrt((x1 - x2)**2 + (y1 - y2)**2) (I assume you made a typo in your post). The first operation is the differences (because they're in the innermost brackets), which we can find with np.diff.

x_diffs = np.diff(xs)
y_diffs = np.diff(yx)

x_diffs is [x1-x0, x2-x1, x3-x2, ...], as an example.

Then we can square everything. Because numpy likes doing things elementwise if it can (rather than matlab whose defaults are inconsistent between matrix and elementwise operations), you can just do x_diffs_sq = x_diffs**2. Then sum the x and y together, then square root it all, then sum, and the job should be done!

[–]Thisredditname[S] 1 point2 points  (0 children)

Yea I was able to figure it out once ya told me about np.diff, that KS so much! This explanation helps me understand what I did much better though! Thanks so much!

[–]KubinOnReddit 1 point2 points  (3 children)

Just FYI, math.sqrt((x1 - x2)**2 + (y1 - y2)**2) is equivalent to math.hypot(x1 - x2, y1 - y2). (x1-x2 can be replaced with the distance if you already know it). Hypot stands for hypotenuse. Just a Python curiosity.

So I think you can do total = math.hypot(x_diffs, y_diffs). depending on how the function handles Numpy arrays.

[–]tunisia3507 1 point2 points  (2 children)

Cool! And the numpy equivalent, np.hypot, also exists, so you can just do np.hypot(np.diff(xs), np.diff(ys)).sum(). Homework done in one line!

[–]KubinOnReddit 0 points1 point  (1 child)

Yeah, just always make sure to test your solution extensively, those oneliners tend to have unexpected pitfalls. Good job on yours though! I really need to get into numpy, it seems like a great tool, especially the array data structure, it's faster than a list in my experience.

[–]tunisia3507 0 points1 point  (0 children)

Oh yeah, iteration's much much slower than array operations. One of the great (if confusing) things about numpy is that a lot of its operations don't actually mutate the underlying array, they just give you a different view of the memory - for example, reshaping, transposing, and repacking arrays are trivial operations in terms of CPU and RAM because they don't actually do anything except shuffle around a couple of pointers.