Quiz 3: Basic Vector Class

You've spent a lot of time in your homework working out various linear algebra and vector calculus expressions by hand, but if course in practice we really want our computer to do all these calculations for us! In the previous quiz you just plugged into Eigen to do linear algebra; likewise, in Assignments 1--4 we'll hand you a bunch of skeleton code that already handles vector operations, etc. However, we don't just want this code to be a total black box!. We want you to have some real understanding of how things work "under the hood." So, for this quiz you'll implement a basic version of the Vector class found in the skeleton code---with a small twist. Whereas the skeleton code has special classes for 2- and 3-dimensional vectors, you'll implement some basic code that handles general n-vectors. Doing so will also help you review basic concepts in linear algebra and vector calculus, just to make completely sure you're on the right track!

Building the code template

The code template for this quiz can be found at this link. It consists of a single C++ file that defines a basic vector class. It also provides some simple code for checking your implementation (which gets called from main).

Download the code above, and make sure it compiles. As in the quiz on Eigen, you should be able to compile it using a simple Makefile that looks something like this:

all:
 c++ -std=c++11 vector.cpp -o vector

The only difference is that we've added the flag -std=c++11 to ensure that your compiler uses features from C++11 (in particular initializer lists, which are used to initialize vectors). If none of this makes sense to you, it's no big deal: you just need to make sure the flag is there so that the code compiles. If you're using VisualStudio on Windows, C++ should work by default (but let us know if you have any trouble...).

Go ahead and try compiling the code. If it compiles, you should be able to run it via

./vector

Implementing the vector class

The vector class has already been outlined for you; all you need to do is implement the methods marked with a TODO in the template code. One example (addition, via the + operator) has been provided for you; the other methods you need to implement shouldn't look dramatically different from this one.

You are responsible for implementing the following methods:

  • subtraction (operator-())
  • right scalar multiplication (operator*(Vector,double))
  • Euclidean norm (norm())
  • Euclidean inner product (inner())
  • cross product (cross())
  • determinant (det())
  • left scalar multiplication (operator*(double,Vector))

Note that all your methods need to work for vectors of length N, except for the cross product and determinant methods, which need only work for 3-vectors.

Crash Course in C++ Operator Overloading

If you're looking at C++ code for the first time in this class, you might be unfamiliar with funky method names like

Vector<N> operator+( const Vector<N>& v )

What this line says is:

  • This method returns a vector of length N
  • Rather than an ordinary name, like foo(), this method is called by typing the + symbol (see below for an example)
  • It takes as input a vector called v, of length N; the keyword const tells the compiler that the input vector v will not be modified by the method, and the symbol & indicates that it is passed by reference, not by value.

Since this method is defined inside of the Vector class, it has access to the internal data of the calling vector. That's why it takes just one argument, rather than two: when we type the expression

Vector<N> u, v, w;
w = u + v;

what's really happening is that the first vector, u, is calling the method operator+ with the argument v. In fact, C++ is perfectly happy to let you write the equivalent line

w = u.operator+( v );

It's just a lot harder to read! :-)

This technique is called operator overloading. Though operator overloading can result in some pretty crazy-looking code when misused, it tends to be really nice for graphics where we want to write a lot of complicated expressions involving matrices, vectors, etc.

Handing In the Quiz

When correctly implemented, the code should print out some vectors and scalars computed using your code, as well as a comparison to the correct reference values. Concatenate this output to the methods you wrote for this quiz. Your final quiz should be a single printout containing:

  • the code you wrote,
  • the output of your program, and
  • your name and Andrew ID

Please delete all the code above/below the CUT HERE lines, as well as any large comment blocks. We just need to see the code you wrote, and want to save paper if possible. Also, use a small (but still readable) font size!

Hopefully this exercise will help you refresh your knowledge of vector calculus, while also getting you more familiar with C++ as we ramp up to the first coding assignment. Remember that the assignment is open book, open notes, open internet---in particular, you are allowed and encourage to consult any reference on C++ (including each other). However, your final code should be your own. Also: don't forget to ask for help! :-)