Search for question
Question

Q3 Vector Distances

25 Points

Purpose: Write array functions to solve a mathematical problem.

I'm sure you have all heard of Euclidean distance. In two dimensions, the Euclidean distance

between (21,2₂) and (3.3/2) is

For example, the distance between (3, 1) and (1, 1) is

√(21-31)² + (22-2².

and the Euclidean distance between (1, 1) and (0,0) is

|zw|₂

((3-1)²+(1-1)²)¹/² 2

The Euclidean distance bewteen (0, 0) and (1,1) is approximately 1.4142.

This notion of Euclidean distance can easily be extended to vectors of arbitrary dimension. Indeed,

if is a vector of dimension n (versus dimension 2), then its Euclidean norm, which we will denote

by ||*||2, is given by

12

((1-0)²+(1-0)²)¹/2 1.4142

There, is a shorthand for "sum," as we saw in lecture and z; is just the absolute value of the

real number zi. By the way, this is quite common notation and such distances are used in machine

learning applications like image and speech recognition (you will see an intro later on the

semester).

i=1

||176 — 36|²2) ¹1/²2 — (153 — 301)² + |2₂-M₂² ++ |³n-M³₁ (²) ¹/2

It turns out that the Euclidean distance is just one of many possible vector norms. Another

common one is the 1-norm, aka the "Manhattan distance," so called since that is the distance in city

blocks a taxicab must travel between two points on the streets and avenues of Manhattan. The

formula for the 1-norm is:

||z-y||1=(x₁ - y₁|) = (x₁ − Y₁| + x2 - y2 + ... + In - Yn),

See also, the figure below, which is reproduced from https://en.wikipedia.org/wiki/Taxicab geometry

There, the Euclidean or "straight-line" distance is shown in green and the 1-norm or "Manhattan

distance" are shown by the equally shortest "taxicab" paths in red, blue, and yellow./n(a: 7 points) Use a function to compute the usual, Euclidean distance between the two filled circles

in the example above:

i. Point your browser to https://colab.research.google.com/drive/1kCbywGZ20mmkkncYzMUWJXB zgVKaT?

usp=sharing

File -> Save a copy in Drive and rename it pynb

ii. Read through the function.

iii. Add comments to the function.

iv. Underneath the function, add a single line that both calls it to compute the Euclidean distance

(length of green line) between the point (0,0) and (6,6) in the figure above and also prints the

result.

v. Copy and paste the output in a comment at the end of the block.

vi. Turn in a cut-and-paste of your entire block in the box below:

(b; 3 points) Don't do it, but explain in words how you would alter the function

to work for

and y of arbitrary dimension (that is, not 2)? You may assume that and have the same length./n(b; 3 points) Don't do it, but explain in words how you would alter the function

to work for x

and y of arbitrary dimension that is, not 2)? You may assume that and have the same length.

Save Answer

(b; 3 points) Don't do it, but explain in words how you would alter the function to work for

and of arbitrary dimension (that is, not 2)? You may assume that and have the same length.

Enter your answer in the box below.

(c; 15 points) Next, you will a function that computes the vector norm 21. It should work for

arbitrary vectors z and y of the same length. That is, the function header might look like the

following, where and are lists

det nori, y

If your function is called as below

normi, 1. (1. 1])

then it should return 2. Here, the or dimension of 1,115 11, 13 which is 1

1. In the same notebook as used in (a) above, make a copy of the first block.

Edit the program to compute the Manhattan distance for arbitrary vectors x and y of the

same length (again, arbitary) Also, edit the comments as necessary.

iii. Test your method by computing (and printing) the Manhattan distance between (0,0) and

(6, 6), as seen in red/blue/yellow in the picture above. Again, place the answer as a comment at

the end of your block.

vi. Turn in a cut-and-paste of the entire block in the box below:

Fig: 1

Fig: 2

Fig: 3