Indexing

Computational implementation of problems in $d=3$ dimensions requires careful mapping of three-dimensional objects into a linear memory space. In lecture, we developed an approach to this indexing through the formation of two index matrices M and N, where

$\textstyle \parbox{3in}{
\begin{equation}
{\bf M} \equiv \left[
\begin{array}{c...
...s & \vdots & S_3-1 \\
\vdots & \vdots & S_3
\end{array}\right]
\end{equation}}$$\textstyle \parbox{3in}{
\begin{equation}
{\bf N} \equiv \left[
\begin{array}{c...
...vdots & \vdots & -2 \\
\vdots & \vdots & -1
\end{array}\right]
\end{equation}}$

As a specific example, if S=[3; 3; 2], then
\begin{displaymath}
{\bf M}=\left[
\begin{array}{ccc}
0 & 0 & 0 \\
1 & 0 & 0 \\...
...1 \\
0 & 2 & 1 \\
1 & 2 & 1 \\
2 & 2 & 1
\end{array}\right]
\end{displaymath} (1)

This problem guides you step by step through the setup of these matrices while exploiting the interactive aspects of the octave language.

Begin by creating a directory ``$\sim$/A1'' in which you shall put all of the work for this first assignment. Working in ``$\sim$/A1'', create a file ``setup.m'' containing a single line of octave code:

S=[3; 4; 5]
Verify that you can run octave by starting octave (in the same directory ``$\sim$A1'' with ``setup.m'') and entering ``setup'' at the octave prompt. (Don't forget to hit the enter key!) This will run the octave commands in the file ``setup.m'' Octave should respond:
S =

  3
  4
  5

Next, create the first two columns of M by adding the following lines to ``setup.m'' immediately after the statement declaring S

%# Code fragment to create columns of m1, m2, m3 indices and the matrix M
ms=[0:prod(S)-1]'; %# Count from zero to S1*S2*s3-1 in a column vector
m1=rem(ms,S(1));
m2=rem(floor(ms/S(1)),S(2));
m3=rem(floor(ms/(S(1)*S(2))),S(3));
Rerun ``setup.m'' and inspect and verify the result by entering first just ``m1'', then just ``m2'', and finally just ``m3'' at the octave command prompt. Note that you can inspect any part of the above expressions, such as ``floor(ms/S(1))'', by entering it at the command prompt. Also, by entering ``whos'', you can get a report of all of the variables currently defined and, very usefully, their dimensions. (The function size() may be used to find the size of any single object.) Finally, combine each of your columns into a matrix using the octave block matrix formation
M=[m1, m2, m3];
Inspect and verify your final result by rerunning ``setup.m'' and entering ``M'' at the command prompt.

Finally, write a block of code to compute N, verifying your results in a similar way.

Hint: Compute the columns of N from the corresponding columns of M using forms such as

n1=m1-(m1>S(1)/2)*S(1);
and then combine your result into the the final matrix.

Tomas Arias 2007-04-05