Recall the Python script for operating on sparse integer-valued 2D-matrices
given in Assignment #4. We can define operations of single
matrices that apply a particular function to each elemnt of that
matrix, e.g., op1 * -1 (negation); similarly, we can define
operations (besides addition and subtraction) on pairs of same-dimensioned
matrices that apply a function to each pair of matched elements,
e.g., (3 * op1) - op2. Each such operation can be specified by a
symbol or string denoting that operation, an integer 1 or 2 indicating
whether that operation is monadic or dyadic, and the Python expression
associated with that operation (phrased in terms of op1 for monadic
operations and op1 and op2 for dyadic operations). Given this, we can define
a very simple scripting language consisting of the following statements:
- import file -- read in operation-specification file
file.s2mop
- read file -- read in sparse 2D-matrix file from
file file.s2m
- print file -- print sparse 2D-matrix file to the
terminal
- write file -- write out sparse 2D-matrix file to
file file.s2m
- res = mop op1 -- apply mondaic operator
mop to sparse 2D-matrix op1 and store result in sparse
2D-matrix res.
- res = op1 dop op2 -- apply dyadic
operator dop to sparse 2D-matrices op1 and op2
and store result in sparse 2D-matrix res.
Let such scripts be stored in files with a ".s2ms" extension.
Write and document a Python script s2m.py which takes as a
command-line argument an s2m script and interprets the
commands in this script. Where possible, define your own functions to
simplify code structure; when you do, make sure these functions are
documented with their own doc-strings and (where necessary) internal
comments. Your script must work on s2m script file
script1.s2ms, s2m operatio-specification file
op1.s2mop, and s2m datafiles
m1.s2m and m2.s2m
to produce the output given in typescript-file
s2m.script. You may assume that all given
datafiles are formatted correctly and are readable.