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:

The infinity norm is typically defined to be the maximum absolute value, .

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.

1.3 Add unit tests

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 and , the Euclidean distance is:

where is the vector difference of and , 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.

2.3 Add unit tests

Add unit tests to verify that your trait implementations compute the euclidean distance correctly.

Submission

The tar.gz or zip file should be uploaded through this page. The deadline is June 26 at 14:00.