# Assignment: Extending ndarray

Download this crate for the sixth homework assignment. It imports some useful crates, including the ndarray crate.

In this assignment, you will extend ndarray by specifying a new trait and then implementing this trait for some of ndarray’s types. These extensions will be used in a future assignment to implement a classifier.

## 1 Norms

Several lectures ago, we looked at vector norms:

Commonly-used norms are:

• $p = 1$.
• $p = 2$, which is the Euclidean length of a vector.
• $p = \infty$.

The infinity norm is typically defined to be the maximum absolute value, $\lVert \mathbf{u} \rVert_\infty = \max_i \lvert u_i \rvert$.

### 1.1 Define the Norm trait

Define a trait Norm. Types that implement this trait have a norm method that takes a Norms enum variant and computes the corresponding norm. The Norms enum is defined as follows.

#[derive(Clone, Copy, Debug)]
pub enum Norms {
L1,
L2,
Infinity,
}


### 1.2 Implement the Norm trait

Implement the norm trait for ndarray arrays. At the very minimum, it should be implemented for a concrete vector type such as Array1<f32> or ArrayView1<f32>.

Your grade will increase if you generalize the implementation such that it works for: both owned arrays/borrowed arrays (views), both f32/f64, and any dimensionality. In order to do so, you still need only one implementation, but you need to exploit the various traits defined and used by ndarray and your norm implementation.

Tip: even if you go for a generic implementation, it is the easiest to start implementing the Norm trait for a concrete type. Then generalize the implementation step-by-step.

Add unit tests to verify that your trait implementation computes the norms correctly.

## 2 Euclidean distance

The Euclidean distance is the straight-line distance between two points in a Euclidean space. For two points, represented as the vectors $\mathbf{u}$ and $\mathbf{v}$, the Euclidean distance is:

where $\mathbf{u} - \mathbf{v}$ is the vector difference of $\mathbf{u}$ and $\mathbf{v}$, computed by elementwise subtraction.

### 2.1 Define a trait for Euclidean distance

Define a trait EuclideanDistance. Types that implement this trait have a euclidean_distance method that computes the Euclidean distance between the vector and another vector.

### 2.2 Implement EuclideanDistance

Implement the EuclideanDistance trait twice, such that you can compute the distance for the following:

1. Two vectors.
2. A matrix and a vector. In this case, the Euclidean distances between the rows of the matrix and the vector should be computed. The result is then a vector with the distances.

At the very minimum, it should be implemented for a concrete vector type such as ArrayView1<f32> for (1) and concrete matrix vector types, such as ArrayView2<f32> and ArrayView1<f32> for (2).

Your grade will increase if you generalize the implementation such that it works for: both owned arrays/borrowed arrays (views) and both f32/f64.

• Submit your updated project, archived as a tar.gz or zip file.
• Run cargo clean before archiving the project.
The tar.gz or zip file should be uploaded through this page. The deadline is June 26 at 14:00.