Quiz 2: Numerical Linear Algebra (Due 9/3)

In class we talked about how modern computer graphics algorithms make heavy use of fast numerical linear algebra. Your homework will give you plenty of opportunity to review the fundamentals of linear algebra. We thought it would also be useful to give you some hands-on experience with solving linear systems in C++. Doing linear algebra in C++ used to be quite difficult, but in recent years there's been a push to create free, open source, and easy-to-use numerical linear algebra code. For this quiz, we'll use a popular package called Eigen. Eigen gives you access to lots of different linear solvers; you'll just use the most basic one to solve a little linear system.

Eigen Setup Instructions

One reason Eigen has become so popular is that it's dead simple to use: the library is a "header only" library, which in practice means you don't have to install or link anything---you just download a bunch of source code files, and include them in your project. Here's what you need to do:

  1. Create an empty quiz2 directory, where you will write the code for this quiz.
  2. Go to the Eigen download page: http://eigen.tuxfamily.org/index.php?title=Main_Page#Download
  3. Download the source code---your probably want this zip file: http://bitbucket.org/eigen/eigen/get/3.3.7.zip
  4. Unzip the zip file somewhere (anywhere)
  5. Copy the Eigen subdirectory into your quiz2 directory.

That's it! You've installed Eigen. :-)

Solving a Linear System

Now you just need to write a small piece of code that solves a linear system with three equations and three unknowns. In particular, we want you to solve the linear system

1.2 x + 3.4 y + 5.6 z = 36.4

7.8 x + 9.0 y + 1.2 z = 87.6

3.4 x + 5.6 y + 7.8 z = 62.8

for the unknowns x, y, and z. To do this, create a new file called quiz2.cpp. At the top of the file, add the lines

#include <iostream>
using namespace std;

#include "Eigen/Dense"
using namespace Eigen;

The first pair of lines give you basic input/output (i.e., writing text to the console); the second pair of lines give you access to the linear algebra features in Eigen.

Next, add the main body of your code:

int main()
{
   // YOUR CODE HERE!
}

This is the main function that will be executed; we just need to fill it in so that it solves the linear system above, and prints out the solution.

Fortunately, Eigen has lots of great documentation. The most relevant page for this quiz is the one here:

https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html

All you need to do is modify the example under Basic linear solving to setup and solve the system given above. In other words, replace the constants from the example in the Eigen documentation with the constants from this quiz. Be careful to put them in the right order! Most linear algebra libraries will expect values in row-major, column-minor order, i.e., you give all the values from the first row, then all the values from the second row, etc.

Building and Running the Code

Now you just need to compile and execute the program you just wrote, using any standard C++ compiler. If you've never compiled C++ code before, this is a great opportunity to get setup---you will need C++ for your coding assignments in this class! (more info below).

Mac/Linux

On Mac OS X and Linux you can just use a standard Makefile. On Mac you must be sure that XCode is installed. Likewise, on Linux you must be sure there is a C++ compiler installed, such as g++. To see if something is installed, try typing which c++ or which g++ at the command line (and see if anything shows up).

Now create a plain text file called Makefile in your quiz2 directory. As long as you've made sure to copy the Eigen directory into your quiz2 directory, this file should be extremely simple:

all:
     c++ -Wall -I. quiz2.cpp -o quiz2

(Depending on your platform, you might need to change c++ to g++.) Save this file. To build the code, you should now just be able to type make in the Terminal/at the command prompt. To run it, type ./quiz2 at the console.

Windows

On Windows you probably want to use VisualStudio, which is available through CMU. Setting up a VisualStudio C++ project is straightforward---Microsoft has a good introduction here: Creating Command-Line Applications (C++). Once you've created a project, copy in the source code above, and make sure that the Eigen directory is in your include path. Compile and run.

If you would prefer to avoid using the IDE, Visual Studio also ships with a "Visual Studio Command Prompt" shortcut, which drops you into a cmd.exe shell where cl.exe is your compiler and nmake is your make tool. The command line arguments to cl.exe are a bit different than to g++, but are well documented.

all:
     cl.exe /nologo /c /EHsc /W3 /WX /MD /I. /Foquiz2.obj quiz2.cpp
     link.exe /nologo /SUBSYSTEM:CONSOLE /out:quiz2.exe quiz2.obj

Handing In the Quiz

When you finally run the code, it should print out the solution to the linear system. Append this output to your file quiz2.cpp, print it out, write your name and AndrewID on it, and bring it to the next class. In other words, your final quiz should be a single printout containing:

  • The code you wrote (quiz.cpp)
  • The output of your program
  • Your name and Andrew ID

That's it!

Although this may seem like a very simple exercise, you're now hooked in to a super powerful numerical linear algebra library that can do all sorts of useful stuff. If you want to learn more, feel free to dig in to the Eigen documentation, play around with other solvers, and so forth. Especially for graphics code, which is often written in C++, having easy access to numerical linear algebra can be super powerful...