From: edwin@maui.cs.ucla.edu (Edwin Tisdale)
Subject: Re: Overloading [] for 2d+ matrixes
Organization: UCLA, Computer Science Department
Date: Wed, 6 Jan 93 20:28:22 GMT

In article <1idfu8INNen2@shelley.u.washington.edu>
rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
>In article <HUGH.93Jan5194117@kahu.cosc.canterbury.ac.nz>
>hugh@kahu.cosc.canterbury.ac.nz (Hugh Emberson) writes:
>
>[...elision....]
>
>>
>>2) The not so elegant solution.
>>
>>Don't use [], use () instead.
>>
>>	Y& X::operator () (whatever, whatever)
>>
>>and use
>>
>>	a(i,j);
>>
>>
>>It should be a bit faster, but its not as pretty.  It has the same
>>precedence as [] too.
>
>Ugliness here is in the eye of beholder.  Personally, I think addressing
>a two-dimensional array as a[i][j] is ugly.  And more work.  Maybe it's
>because I touch-type, but I find a(i,j) a lot easier to type than a[i][j].
>
>Trying to overload operator [] for matrix classes generally seems to 
>create more problems than it solves.  I've some experience with matrix
>classes and that experience suggests that it is better to stick to 

An array `a' declared `<type> a[M][N]' is not a two dimensional array.
It is a one dimensional array of M one dimensional arrays of N <type> elements.
The reference `a[j]' is a pointer to the j'th array and `a[j][i]' references
the i'th element of that array.  But compilers do in fact allocate a contiguous
block of memory for the all M arrays in sequence.  This obviates the need for
a separate array of pointers to locate each of the M arrays since the pointers
can be easily computed on-the-fly.  If you think of `a[j]' as representing the
j'th column of a matrix, then `a[j][i]' would be the element in the i'th row
of that column.  But apparently there is such a strong compulsion to associate
the first "index", j, with the row of a matrix and the second "index", i, with
the column of a matrix that so-called two dimensional arrays are said to be
organized in row major order in C programs.

It really shouldn't matter much whether you use `[][]' or `(,)' if you
implement them as inline functions because the optimizing compiler will
end up generating pretty much the same code either way.  Nor is there
any reason why you shouldn't implement both indexing schemes.

The C++ Matrix class is available via anonymous ftp from `ftp.cs.ucla.edu'.
Get the compressed tarfile `pub/Matrix.tar.Z'.  Uncompress `Matrix.tar.Z'.
Extract the `Matrix' directory from `Matrix.tar'.  Then go to the `Matrix'
directory and type `make'.  Now print and read the paper in `Matrix.dvi'.

In order to demonstrate an application of the \verb"Matrix" class,
it was used to implement the backward error propagation algorithm
for multi-layer, feed-forward artificial neural networks.

Enjoy, Bob Tisdale (edwin@cs.ucla.edu)

P.S.  I hope the following notes will be helpful.

unix% ftp ftp.cs.ucla.edu
Name (ftp.cs.ucla.edu:your_login_ID): anonymous
Password: your_login_ID
ftp> cd pub
ftp> binary
ftp> get Matrix.tar.Z
ftp> bye
unix% uncompress Matrix.tar.Z
unix% tar xvf Matrix.tar
unix% cd Matrix
unix% make
unix% dvips Matrix | lpr
unix% mv ffnet.new ffnet.old
unix% cat ffnet.old ffnet.S | (backprop -v > ffnet.new) >>& ffnet.err
unix% cat ffnet.new ffnet.S | evaluate | graph -m 0 | plot

Note: If you have trouble, verify that the `SYS5' variable
      in the `Matrix/src/genclass' shell script contains
      the path to the System V version of the `m4' macro
      pre-processor.
      The `fig2dev' program is part of the `transfig' package
      available via anonymous ftp from `ftp.cs.cornell.edu'.
      Get `pub/transfig/transfig.tar.Z'.
      The EPIC style file is available via anonymous ftp from
      `ymir.claremont.edu'. Get `tex/inputs/latex-contrib/epic.sty'.
      File `pub/Matrix.sun.tar.Z' includes all the files created by
      `make' on UCLA CSD Sun4 computers.  It may be useful to Sun
      computer users or those who just want to print the documentation.