I do some computational mechanics in C ++ (don't worry, no physical knowledge is required here), and there is something that really bothers me.
Suppose I want to represent a 3D mathematical vector (do nothing with std :: vector):
class Vector { public: Vector(double x=0., double y=0., double z=0.) { coordinates[0] = x; coordinates[1] = y; coordinates[2] = z; } private: double coordinates[3]; };
So far so good. Now I can overload operator [] to retrieve the coordinates:
double& Vector::operator[](int i) { return coordinates[i] ; }
Therefore, I can print:
Vector V; β¦ //complex computation with V double x1 = V[0]; V[1] = coord2;
The problem is that indexing from 0 is not natural here. I mean, when sorting arrays, I donβt mind, but the fact is that the conditional notation in each article, book or even always substitutes the coordinates starting with 1. This may seem like a cripple, but the thing is that in the formulas a double approach is always required to understand what we are doing. Of course, this is much worse with matrices.
One obvious solution is to just slightly overload:
double& Vector::operator[](int i) { return coordinates[i-1] ; }
so i can print
double x1 = V[1]; V[2] = coord2;
It seems perfect, with one exception: this is an i-1 subtraction, which seems like a good candidate for small overheads. There is very little that you would say, but I deal with computational mechanics, so this is usually something we could not afford.
So now (finally) my question is: do you think that the compiler can optimize this, or is there a way to make it optimized? (templates, macro, pointer or kludge link ...)
Logically, in
double xi = V[i];
an integer between the bracket that is literal most of the time (except for 3 iterations for loops), the inlining [] operator should make this possible, right?
(sorry for this question)
EDIT:
Thanks for your comments and answers.
I disagree with what people tell me that we are used to 0-indexed vectors. From an object-oriented point of view, I see no reason for a mathematical vector to be indexed 0, because it is implemented with a 0-indexed array. We do not have to worry about the underlying implementation. Now suppose I don't care about performance and use a map to implement the Vector class. Then it would be natural for me to display "1" with the coordinate "1st".
This says that I tried with 1-indexed vectors and matrices, and after writing some code, I find that it doesn't interact nicely every time I use an array around. I have a Vector and containers (std :: array, std :: vector ...) will not often interact (which means transferring data between each other), but it seems like I was wrong.
Now I have a solution that, in my opinion, is less controversial (please give me your opinion): Every time I use Vector in some kind of physical context, I think about using an enumeration:
enum Coord { x = 0, y = 1, z = 2 }; Vector V; V[x] = 1;
The only drawback that I see is that these x, y and z can be overridden without warning ...