\section{Jamaica: Why a new matrix class?}

The pre-existing Java matrix classes, such as Jama and java.vecmath,
lack some fundamental functions which is necessary to our programming
language.  The most important ones are sub-matrix sharing the same
data of the super-matrix, {\em matrix slicing}, and assigning selected
elements of a matrix, {\em matrix masking}.  Also, it is not very
possible to add these functions by a wrapper.  In Jama, the fundamental
data structure does not support matrix slicing, and we can not get
the source code of java.vecmath.

\subsection{Underlying data structures}

It is well known that there are two access methods for matrices or
multiple dimensional arrays.  The first one is using a continuous one
dimensional array, the actual index of a matrix entry is computed by
adding the second index to the product of the second dimension and the
first index, (or reverse, whatever) i.e., if $A$ is an $m\times n$ matrix,
\[ A[i,j] = A[n*i+j]\quad(\mbox{or, }A[i+m*j]). \]
The second implementation is using a level of indirection,
\[ A[i,j] = *(*(A+i)+j), \]
where $*$ is to get the content of a pointer.  Many systems use the
second approach, such as numerical recipe and Jama.  The C programming
language uses the first approach to allocate a fix-sized multiple
dimensional array.  People might think the first approach is
inefficient because of multiplication; however, there is one more
memory access in the second approach: the memory access is at the
third location, which might have bad cache performance.  Moreover,
since all arrays in Java are objects, and Java will check index bounds
for arrays, so in Java a level of indirection might be much more
inefficient.

Jamaica uses the first approach.  The underlying data structure
contains the following fields:
\begin{itemize}
\item the one-dimensional array
\item two dimensions of the matrix, $m$ and $n$
\item the start index of the upper-left entry of the matrix, $s$
\item the difference of the indexes between two vertical neighboring 
entries, $ms$
\item the difference of the indexes between two horizontal neighboring 
entries, $ns$
\end{itemize}
This data structure will make in-place transpose, flip, mirror, and
slicing much easier: just create a new object and share the same
array.  For example, to create a transposed matrix, just swap $m$ and
$n$, and also swap $ms$ and $ns$.  To get a sub-matrix, just re-compute
the start index $s$.  We can even get an in-place diagonal vector of a
matrix.

\subsection{Comparison of functions between Jama and Jamaica}

This is a brief comparison between Jama and our Java matrix class,
Jamaica.

\begin{tabular}{|l|l|l|}
\hline
  & Jamaica  & Jama \\
\hline
status & developing & discontinued \\
\hline
matrix arithmetic operations & yes & yes \\
matrix generators (random,zeros,...) & yes & yes \\
index matrix generator (indgen) & yes & no \\
matrix inverse & yes & yes \\
sub-matrix & yes & yes \\
in-place sub-matrix & yes & no \\
transpose & yes & yes \\
flip/mirror & yes & no \\
matrix masking assignment & yes & no \\
matrix comparison & yes & no \\
solve linear equations & yes & yes \\
LU decomposition & yes & yes \\
QR decomposition & no & yes \\
singular value decomposition & no & yes \\
eigenvalues/Eigenvectors & no & yes \\
Cholesky decomposition & no & yes \\
printing & yes & yes \\
plotting and painting & yes & no \\
file I/O & yes & no \\
\hline
\end{tabular}

\section{Mx: Implemented as a translative language}

Conceptually, our Mx interpreter receives input program files, processes
them, and generates desired outputs, so it can also be considered as a
compiler or translator.  However, practically, we define program
processors that directly generate the result of the program as
interpreters, and those that generate an intermediate program in
other programming language as translators, and those that generates
code which can be directly executed on hardware as a compilers.

Under this definition, initially, we want to create a translative
system.  After a few discussions, we conclude that it will be too hard
to implement a translative system like Mx in just one semester,
although by now I personally think it is possibly not that hard.  So
here we just present some ideas in translating the Mx programing
language or something-alike, such as Matlab and IDL, to an imperative,
statical-scoped, strong-typed programming language such as C, and hope
that it will be helpful for people who want to build such a system.
These ideas might contain some fundamental errors, or might have been
in some formal literatures which we have never got a chance to read.

\subsection{Interpretive versus translative: which is easier?}

In our opinion, if the source language and the destination language are
very similar, the translative job will be easy, even easier than 
developing an interpretive one.  Our Mx programming language is not
very similar to most of imperative programming languages such as
C, C++ and Java, so the translative job would be a little less easy.
However, we think for flow control statements, the translative
procedure might be simpler than to actually execute it, because most
of the programming languages have the similar flow-control statements.
For those operators, we can translate it to function calls.  For
example,
\begin{verbatim}
B = random(8,8);
A = zeros(4,4);
A[C>3]=B[4:7,5:8];
\end{verbatim}
can be translated to Java code
\begin{verbatim}
B = Matrix.random( 8, 8 );
A = new Matrix( 4, 4 );
A.assign( C.gt(3), B.slicing( new Range(4,7), new Range(5:8) ) );
\end{verbatim}
using the Jamaica package.

Another difficulty of translator over interpreter is that the
translator is harder to be debugged and tested.  Besides, the
supporting library evolves along with the project; so without a fixed
interface, the code in the translator will not be stable.

\subsection{Matrix operation: in-line expansion}

Someone may think that it is not necessary to call the matrix class
methods in generated Java code; we can generate in-line loop code
like the following:
\begin{verbatim}
B = new double[64];
for ( int i=0; i=8; i++ )
    for ( int j=0; j<8; j++ )
        B[i*8+j] = Math.random();
A = new double[16];
for ( int i=0; i=4; i++ )
    for ( int j=0; j<4; j++ )
        A[i*4+j] = 0;
for ( int i=0; i=4; i++ )
    for ( int j=0; j=4; j++ )
        if ( C[i*4+j] > 3 )
            A[i*4+j] = B[(4+i)*8+(5+j)];
\end{verbatim}
To improve performance, the assignments of $B$ and $A$ can be both
reduced to one-level loops, and the multiplications can also be saved.  
However the two levels of loops of the last assignment cannot be reduced. 
Moreover, performance of these lines of code can be largely affected
if one swaps two levels of loops.  For example, in a C program,
\begin{verbatim}
int i, j, A[N][N];
for ( i=0; i<N; i++ )    /* [1] */
    for ( j=0; j<N; j++ )
        ... A[i][j] ...;
for ( j=0; j<N; j++ )    /* [2] */
    for ( i=0; i<N; i++ )
        ... A[i][j] ...;
\end{verbatim}
The execution time of [1] might be much shorter than that of [2],
because caching technology is used everywhere in modern computer
architectures, so sequential access will be much faster than random
access.

However, we think it would turn out to be a very hard project, which
is impossible to finish in just one semester as a course project.

\subsection{Variables without declaration}

A common property of interpretive programming language is to use a
variable without declaration.  Our Mx programming language is not
an exception.  People might think that, without declaration, the type
of a variable will be hard to be determined in compiling time.  However,
there are ways to get round of it:
\begin{enumerate}
\item[1.] The type of left-hand side of an assignment can be determined
by the operator and operands, and the type of an operand can be
determined recursively.  In each assignment, we mark the data types of
the right-hand-sides.

\item[2.] Check whether the right-hand sides of all assignments of this 
variable are the same.  If so, the type of this variable is determined.
Functional programming language does not need declarations, because
there are no assignments.

\item[3.] If not so, check whether these types can be all promoted to
some type, which is also compatible at all access points of this
variable.  For example, here $x$ can be promoted to double in the
second line of the following:
\begin{verbatim}
if ( ... ) {
   x = 3;
} else {
   x = 3.5;
}
z = x + 1.5;
\end{verbatim}

\item[4.] Checking the return type of a function might be a hard job,
and it is necessary for determination of types of variables.  This can
be done by checking type in the return statements.  If there is a
recursive call, it can still be done by introducing some algorithms
about logic.

\item[5.] If all above efforts failed, we can promote it to the most
general type (or say a meta type), which is exactly the base class in
the type system of our interpreter, class \textsl{MxDataType}.  (Of
course, classes of our interpreter must be included in the run-time
environment.)  As an alternative, the translator might print messages to
ask whether the programmer can change it.

\item[6.] All global variables accessible by programs but not examined
by the translator can be handled as variables of the meta type.
\end{enumerate}

\subsection{Dual scoping}

We didn't mention the scoping problem in the language reference
manual.  However, we have considered the scoping problem in our
implementation.  Translative languages prefer static scoping, so by
default our Mx programming language is also static scoping.  However,
we have implemented the {\em dual scoping} mechanism in the interpreter
because during the design stage, we do not know what is feasible,
static or dynamic.  In the current implementation, each time we create
a sub symbol table, it has two parent symbol tables, one is its static
parent, the other is its dynamic parent.  We can add a qualifier
before using a variable to force the interpreter to search this
variable in the dynamic ancestors of the current symbol table.

In the current implementation, if we use static scoping, the deep
binding mechanism is used naturally, whereas, if we use dynamic
scoping, the shallow binding mechanism is used.

%\section{Macros: good or bad}

