Global Sources
EE Times-India
 
EE Times-India > EDA/IP
 
 
EDA/IP  

How to write vector operations in C

Posted: 03 Dec 2006     Print Version  Bookmark and Share

Keywords:C  vector  C++ 

Lately I've been showing you how to create a set of serviceable C++ functions suitable as the basis for a full-up library of C++ classes for vector and matrix arithmetic. I started with some very simple vector functions written in the classical C (some might argue Fortran) style.

One thing has become extremely apparent: a lot of people have a lot of opinions about how to implement the ideas. I can't recall many times when I've received such a flurry of e-mails, most of them telling me that I'm doing it all wrong. All of the opinions carry weight with me, but a few came from people with huge name recognition—people whose opinions I value highly.

Why so many disparate opinions? Partly it's because personal preferences carry weight. Which brand of truck will you choose for your next big, white pickup? What's that you say? You want a black Audi sports sedan?

Sigh.

I've been thinking long and hard about the disparate points of view, and here's my conclusion: opinions vary because the decisions aren't clear cut. As small and simple as the vector functions are, there are many possible ways to do them and many pros and cons to each. What's more, the pros and cons tend to balance each other, leaving no clear winner. Which is perhaps why, after more than four decades of doing this stuff, I still tend to change my mind on alternate Thursdays and full moons. I've mentioned that I've probably written these functions hundreds of times. Why so many? Because each time I write them, I do it a little bit differently.

Speaking of which, it seems that in my last column, I wrote them one time too many. I got them wrong.

In writing and publishing these columns, I and the editorial staff try hard to get them right. Because I use a lot of math equations in this column, it's particularly susceptible to typos. Lots of people look the column over very hard before it gets to you.

In the case of last month's code, however, I'm afraid I got a little too complacent. After all (I reasoned), I've written these functions hundreds of times. They're not exactly the source code to Microsoft Vista; each function has only one to three executable lines of code. How hard can it be? What can go wrong?

Apparently, a lot. Because I didn't exercise the usual due diligence, many errors crept into the code; almost more errors than there were lines of code. Sorry about that. Believe me, I've learned my lesson. The code I'll show you this month is going to be thoroughly tested, as it usually is.

Weighty subject
Back to style decisions. The style one uses to write code depends a lot on the weight one assigns to the different "-ilities." In the past I've given you my list of attributes that I value. They are, pretty much in order:
� Correctness (always #1)
� Ease of use
� Readability
� Maintainability
� Efficiency

You'll note that I almost always place efficiency way down on the list. Yes, it's important, but I try to never obfuscate or complicate the code just to gain efficiency. I've also mentioned, though, that in the case of vector and matrix math, efficiency rises higher on my radar screen than usual. That's because these are low-level routines, which tend to get used way down inside nested loops. Perhaps it's this vague thought that tends to make the choices so difficult, at least for me.

Most regular readers know that it's never my purpose to write code for you, tip the top of your head back, and pour it in. My goal is to show you the idea, give you the pros and cons, and encourage you to make your own decisions and create your own implementations. In this case, as in many others, the best I can do is to show you my implementation (my implementation of the week, that is), and explain the decisions I've made. Your mileage may vary (YMMV).

Let me give you an example. Last time, I showed you the vector add routine, which now looks like this:
// Add two vectors (c = a + b)
// Vector c can coincide with a or b
void vAdd(const double a[ ],
const double b[ ], double c[ ], int n)
{
for(int i=0; ic[i] = a[i] + b[i];
}

(Note the change in spelling.) As I explained, the purpose of the fourth parameter, n, is to allow the vectors to have different lengths. But in most of my scientific work, vectors represent real, physical quantities like position and velocity, so they tend to have length three (or sometimes even two). If every vector in my software has length three, it gets to be a bother always writing:

vAdd(a, b, c, 3);

Aside from the bother of the extra parameter, every literal parameter in a calling list is an opportunity to get it wrong. Perhaps, from force of habit dating from First Grade, I write:

vAdd(a, b, c, d);

I could spend a lot of time scratching my head over that one. For that reason, I also showed you special functions for the case n = 3.

// Add two 3-vectors (c = a + b)
// Vector c can coincide with a or b
void vAdd(const double a[ ], const
double b[ ],

1 • 2 • 3 Next Page Last Page



Comment on "How to write vector operations in C"
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top