[comp.lang.c++] Inline transposition of a matrix.

mmin@slate.mines.colorado.edu (Man-Sik Min ) (04/27/91)

Can anybody send me a code of algorithm which transposes
m by n matrix(read in as 1-d array).

The code must transpose using only 1-d array.

I know this was talked about many times before.....

Thanks in advance

bjaspan@athena.mit.edu (Barr3y Jaspan) (04/28/91)

In article <1991Apr26.182944.35669@slate.mines.colorado.edu>, mmin@slate.mines.colorado.edu (Man-Sik Min ) writes:
|> Can anybody send me a code of algorithm which transposes
|> m by n matrix(read in as 1-d array).

Well, here is the "transpose" method from my Matrix class.  It uses two for
loops and calls operator() which accesses an element.  However, operator()
just computes an index into a 1-d array, so from this you should be able to
write code to do what you want.

Matrix Matrix::T() const
{
     Matrix t(M_n, M_m);

     for (int i = 1; i <= M_m; i++)
	  for (int j = 1; j <= M_n; j++)
	       t(j, i) = (*this)(i, j);

     return t;
}

inline double& Matrix::operator()(int m, int n) const
{
     /* My kingdom for exception handling ... */
     if (m > M_m || n > M_n || m < 1 || n < 1)
	  abort();
     
     return d[M_n*(m-1) + n - 1];
}



-- 
Barr3y Jaspan, bjaspan@mit.edu