n-Dimensional Matrix Class
Question submitted by n/a (11 October 2000)




Return to The Archives
 
  This entry is not a response to a question.  
 

 
  As you probably already know, this installment marks the third departure from the standard Ask Midnight format (i.e. a response without a question.) This does not mean that I'm abandoning the standard format of this column. As a matter of fact, the next installment will be back to the normal. I would, however like to discuss my intentions behind the past few installments.

When I received the memory management question, I thought it was such an important topic that it should be covered very thoroughly. So I decided to make it a "two-parter". Part one would cover the general topic (and my experiences) while the second part would cover implementation via source to only a basic memory manager. I knew I would need a memory manager for my current project, so why not get a head start? So I decided to write the full-featured memory manager and release that to the column. I'm very glad I did. I got an overwhelming response, and as a result, the memory manager is a much better tool, thanks to all of your input.

Well... being a person of at least average intelligence (and having only used my head as a landing device a few times as a child) I think I can see some added potential in this column. I would like to continue to post installments that do not come from questions, but rather are components from my current project that might be useful (or might be improved by posting them) here. In each case, there will be no restrictions on their use. Use them in a commercial application if you wish, no credit is even necessary. The only thing I ask is that if you improve a component in such a way that is not proprietary to your work, please let us know.

So my plan is this: I will continue the column in its normal format, but I will periodically interject selected components from my work for your use, your input, and possibly your education and mine (hey, I learn from this column, too.)

So without further ado, I present you with the n-dimensional matrix class.

The idea behind this class template is to cover basic matrices of any dimension, including vectors & points (NxM matrices where M=1). The class is not "purely generic". By this I mean that it does cover a few special cases (dot products and cross products only apply to vectors) as well as some handy vector-only/point-only accessors (x, y, z, w). At the same time, it only covers basic matrices. It provides enough functionality to extend it for view matrices or homogenous transformations (etc.) but it does not provide these features directly. If you want these features, simply derive a new matrix class from it.

Also, because we're dealing with a single class, and there are many "common" uses for operator*, (dot product, matrix multiplication, component-based multiplication.) So you'll find a few non-standard operator uses. For example, the operator* is a component-based multiplication (I chose this so that it remains consistent with +, +=, -, -=, /, /= and *=, which are all component-based operations.) operator^ is used for matrix multiplication and dot() is a dot product. Just browse the comments above each member function to see how each operator works. There are no standards for which operators perform which functions, so if you're used to something else, feel free to change the class to suit your preferences.

If you're looking for the fastest matrix class on the planet, this is not the one to use. I purposely (and consciously) chose code readability over performance. I have a very valid reason for doing this. During development of a project, maintainability and readability are paramount (especially in a team environment.) Also, a slightly slower core-level function can really help point out the areas of an application that are lacking in performance. If a core function is slow to use, you're likely going to try to find some algorithmic improvement so that you use that core function less often. As development of my project nears completion, I expect to optimize, vectorize and specialize this class to squeeze more performance out of it. For now, however, I want to be able to debug with it.

Download: Matrix.h (21k)



Response provided by Paul Nettle
 
 

This article was originally an entry in flipCode's Ask Midnight, a Question and Answer column with Paul Nettle that's no longer active.


 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.