Part H: 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.
- 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
an initial condition, current state, and parameter list.
-
As part of the class, define an iteration method that iterates the current
state some number of time steps, returning the final state reached.
-
Recall the one-dimensional map program that calculates the Lyapunov
characteristic exponent. That program does not catch the error, which
is expected, if not common, of passing a zero derivative to the logarithm.
Modify that program to catch this error and respond gracefully
(not terminate!).
Table of Contents