What is reusable code? Is it a template to base projects on or single piece of code used in multiple programs? Microsoft seems confused.
Back to the Matrix class
Now let's get back to software. As promised, I'm adding a new file, called SimpleMat.cpp, to the project (the code will be available at www.embedded.com/code/2008code.htm). As the name suggests, this file contains the same kind of low-level functions that are in SimpleVec.cpp. In the last column (June 2008, p.14), I finessed the need for these functions by tricking the SimpleVec functions into doing the work. That trick still works, but since the functions involved are so tiny, we lose almost nothing by following the more straight forward and transparent approach. Furthermore, SimpleMat.cpp contains a few new, and key functions specific to matrix operations.
The first two functions in SimpleMat.cpp are the console I/O functions mGet and mPut. The mGet function is essentially identical to vGet--the only difference is the range of the for-loop. The mPut function is a little more complicated, so that it will output the matrix row by row. I note again that these functions are rather crude, doing the least amount of work that's practical. For specific applications, you might want to elaborate on them.
At first glance, these new routines seem to be identical in function to the higher-level stream I/O routines I showed last time. In fact, it's tempting to just call the low-level functions from the higher-level ones. But be careful: the functions in SimpleMat.cpp are strictly for console I/O. I use them mainly just for testing. The functions in Matrices.cpp can input/output to any compatible stream object.
The next few functions in SimpleMat are virtually identical to their counterparts in SimpleVec. Because they perform the same operation on every element of the matrix, only the range of the loop counter changes.
One exception relates to the vector function vSet. You might remember that this function sets every element of a vector to the same scalar value s. To be honest, even this vector function has limited value, except when s is 0 or 1. For a matrix, it has no value at all.
On the other hand, the notion of a scalar matrix is fundamental. A scalar matrix has the form:
(1)