I usually work around issues like this by writing a little code-generator that spits out the structure I want for all primitives.

I did a python script once that would process Java source decorated with special tags to simulate C++ templates and that worked best for performance. The problem however is that it's hard to maintain code this way and integrate it with Eclipse.

I thought about having a hierarchy of Tuples and classes that extend it for all vectors no mater their dimension or representation. So we would have a Tuple interface then other interfaces that extend it like Vector, Point, Quaternion, but unlike javax.vecmath where a Tuple2f is the base class of Vector2f but not a Vector3f or Vector4d in my idea a Tuple would be a base class for every Vector class no what their representation or dimension is.

interface Tuple

{

int getSize();

Tuple setSize(int sz);

Class getDataType(); // the data type used to implement the tupe, ex. Integer.TYPE

<type> get<type>(int i);

<type> get<type>X(); // faster

<type> get<type>Y();

<type> get<type>Z();

<type> get<type>W();

Tuple set(int i, <type> val);

Tuple setX(<type> val); // faster

Tuple setY(<type> val);

Tuple setZ(<type> val);

Tuple setW(<type> val);

boolean equals(Tuple u2);

boolean equals(Tuple u2, float delta); // ignored by integer representations

long hashCode();

String toString();

...

}

Implementing the add operation.

Vector v1 = new FloatVector4(); // FloatVector4 implements Vector interface

Vector v2 = new FloatBufferVector(4); // FloatBufferVector implements Vector interface also

import static LinearAlgebra.*;

Vector v = add(v1,v2);

Point p1 = addVector(v1,v2); // wrong: can't cast vector to point

Point p1 = new FloatPoint4();

Point p = addPoint(p1,v);

Point p = addPoint(v1,v2); // wrong: can only add a vector to a point (not two vectors) to obtain a point

How would the add function be implemented in so that using multiple vector dimensions and representations would not conflict?

My idea is to use type conversions before or after doing calculations:

public class FloatVector4 implements Vector {

float x, y, w, z;

public double getDoubleX() { return x; }

public double getDoubleY() { return y; }

public double getDoubleZ() { return z; }

public double getDoubleW() { return w; }

}

public class LinearAlgebra { // only deals with interfaces

public Vector addVector(Vector v1, Vector v2) {

// since we don't know at this point how v2 is represented we simply use doubles and let their values

// be casted when using get/set methods with any lost of data being ignored

double x1 = v1.getDoubleX();

double y1 = v1.getDoubleY();

double z1 = v1.getDoubleZ();

double w1 = v1.getDoubleW();

double x2 = v2.getDoubleX();

double y2 = v2.getDoubleY();

double z2 = v2.getDoubleZ();

double w2 = v2.getDoubleW();

v1.setX(x1+x2);

v1.setY(y1+z2);

v1.setZ(z1+z2);

v1.setW(w1+w2);

return this;

}

}

The only performance issue is the type casts, but since all Math functions work with double arguments how much would this be a problem?