Matrix Initialization
This can be done in two major ways:
Via the class constructor
The Matrix class constructor accepts two forms of arguments:
- Given two positive integers; the number of rows and columns respectively.
Matrix(rows, cols)
- initializes as a null matrix of the given dimension.
- Given a 2-D iterable of real numbers.
Matrix(array, zfill=False)
- If all rows have the same length, the matrix is initialized taking the array in row-major order.
- If row lengths don’t match but zfill is
True
, the rows of the resulting matrix are padded with zeros to the right to match up.
NOTE: The constructor arguments can only be passed positionally, not by keyword.
Using the provided utility functions
The library provided the following functions at the top level for generating matrix instances:
unit_matrix(n)
- Returns a “n by n” unit matrix.
randint_matrix(nrow, ncol, _range)
- Returns a matrix with random integer elements where the third argument (_range) should be a
range
instance representing the range of integers from which the random elements are to be selected. - All arguments must be positional.
- Returns a matrix with random integer elements where the third argument (_range) should be a
random_matrix(nrow, ncol, start, stop)
- Returns a matrix with random floating-point elements where:
- start is the minimum value of an element.
- stop is the maximum value of an element.
- All arguments must be positional.
- Returns a matrix with random floating-point elements where: