Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Dense Matrices

The simplest way to implement a matrix is to use a multi-dimensional array with two dimensions as shown in Program gif. The DenseMatrix class extends the Matrix class discussed in the preceding section. The DenseMatrix class adds a instance attribute called _array which is a multi-dimensional array. The __init__ method of the DenseMatrix class takes two arguments, tex2html_wrap_inline60541 and tex2html_wrap_inline60543, and constructs the corresponding tex2html_wrap_inline60545 multi-dimensional array. Clearly, the running time of the __init__ method is O(mn).

   program3221
Program: DenseMatrix class __init__, __getitem__ and __setitem__ methods.

Program gif also defines the __getitem__ and setitem methods for the DenseMatrix class. These methods each take an ordered pair, (i,j), as the index expression and use the pair as the index into the multi-dimensional array. Because the number of dimensions is fixed at two, the running time for each of these methods is O(1).


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.