Part F: Objects, Classes, and Error Handling
Exercises
-
Write a class MyMatrix that represents a matrix. Passed the
number of rows and columns, a MyMatrix matrix is constructed
from a two-dimensional array of floats. These matrices
should support the following operations:
- Addition and subtraction (element-by-element),
- Multiplication (matrix product), and
- Generalized inverse (returning another matrix object).
- (Feel free to use the numpy package inside your methods
for these operations.)
-
Write a specialized subclass SqMatrix that represents a square matrix.
- Implement SqMatrix by inheriting from the MyMatrix class.
- SqMatrix should verify that it is constructed from a square array.
- If it is not square, it should raise a ValueError and print an informative
error message.
- In addition to the inherited matrix operations, the class should provide:
- Inversion (returning another matrix object) and
- Eigenvalues (returning an array).
- (Feel free to use the numpy package inside your methods
for these operations.)
-
Rewrite the one-dimensional map tool that we've been developing in an
object-oriented fashion:
-
Define a class OneDimMap representing a one-dimensional map, with
attributes consisting of an initial condition, current state, and parameter
list.
-
As part of the class, define an iteration method that iterates the current
state some number (passed in as an argument) of time steps, returning the
final state reached.
-
Recall the MatrixIO.py module that reads in and writes out a matrix.
The function that reads in a matrix does not catch the error when the file
to be opened does not exist. Modify that module to catch this error and
respond gracefully: The program should not terminate with Python
error messages! Instead, it should print an informative error message, giving
the file's name, saying that it could not be opened because it does not
exist, and then exit.
Table of Contents