CMP EMBEDDED.COM

Login | Register     Welcome Guest  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Reusing code and inverting matrices
What is reusable code? Is it a template to base projects on or single piece of code used in multiple programs? Microsoft seems confused.



Embedded.com

In SimpleMat, I'm providing two such functions. Function mSet(s) builds the scalar matrix with s's down the diagonal. Function mUnit performs the same function, with s equal to 1. Note that all scalar matrices are square, so only one size parameter is needed.

The functions mAdd and mSub are logical extensions of their vector counterparts. Not so with the next two functions, which have no counterparts in the vector functions.

We've talked about the matrix product before. Remember the "pour row into column" mnemonic that I gave last time? I won't bother to rehash the algorithm here. Function mMult takes two matrices as arguments and yields a matrix result. Note carefully that in this case, the output argument cannot be either of the input arguments. That output argument gets built one element at a time, and that process would corrupt the input matrices.

When you multiply matrices, the dimensions are critically important. If you think back to the "pour rows into columns" mnemonic, you'll see that the number of columns in the first argument matrix must match the number of rows in the second. If the first matrix is, say, m×k, the second matrix must be k by something. For an easy mnemonic, write:

(2)

In a sense, the two middle dimensions "cancel" each other. In general, whenever we multiply matrices, we must take care that the dimensions match up. In a way, the function mMult "enforces" this rule, by simply assuming it. In the spirit of the Fortranish, low-level functions of SimpleVec and SimpleMat, no error checking is done. If you violate the rule, mMult will happily give you a product you didn't expect. It can also overwrite your code. Caveat emptor.

The next function, mInv, is even more interesting. The mathematical entity called vector has no division operator, and the matrix entity doesn't, either. But it has the next best thing: an inversion operator. Consider the nature of the inverse operation for scalars. By definition, x-1 has the property that:

(3)

For this scalar case, the inverse is clearly:

(4)

From this definition, we can write things like:

(5)

1 | 2 | 3 | 4 | 5 | 6 | 7

Rate this article: Low High
Current rating
  • .
Embedded.com Career Center
Looking for a new job?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS





 :