Data Structures, Algorithms, & Applications in Java
Chapter 8, Exercise 29
Since the class UpperTriangularMatrix of Exercise 27 stores an
upper-triangular matrix by columns and the class LowerTriangularMatrix
of the text stores the matrix by rows, the transpose operation can
be done by just copying the nonzero elements as below:
public class ExtendedLowerTriangularMatrix extends LowerTriangularMatrix
{
// constructor
public ExtendedLowerTriangularMatrix(int theRows, Object theZero)
{super(theRows, theZero);}
/** return the transpose of this */
public UpperTriangularMatrix transpose()
{
// create result matrix
UpperTriangularMatrix u = new UpperTriangularMatrix(rows, zero);
// form the transpose
for (int i = 0; i < rows * (rows + 1) / 2; i++)
u.element[i] = element[i];
return u;
}
}
The time complexity of transpose is Theta(rows2)
as rows(rows+1)/2 elements are copied.
The test program and output are in the files ExtendedLowerTriangularMatrix.*.