Re: Algorithm for Vector Cross Product of N Dimensions
hobbes wrote:
so just to clarify what i am doing, for reasons unknown i am making a
vector class in java and i want the vector object to be able to
perform all the operations that can be performed on a vector. the way
i am doing it now it could be possible to do this:
Vector v2d = new Vector(1, 2);
Vector v3d = new Vector(3, 4, 5);
Vector vx = v2d.cross(v3d);
i guess it should throw an exception here? i was looking for some
algorithm that could compute the cross product of vectors of arbitrary
length but it doesn't sound like i should be doing that in the first
place?
If you really want to do cross product with this form of Java call, I
suggest limiting it to length 3.
You could subclass IllegalArgumentException to report inappropriate inputs.
An alternative, which moves the checking to compile time, would be to
define a 3 dimensional subclass of your Vector, say Vector3, and only
provide cross product in class Vector3, as "Vector3 crossProduct(Vector3
v3d)"
Also, I suggest not using "Vector". I know you can distinguish it from
java.util.Vector through package name, but the functions are a bit too
similar to avoid all risk of confusion if someone reading your code
skips the import statements.
i also stumpled upon the java3d library and i think they are already
doing something like this but so far i dont like/understand the api
and it's kind of fun to do this anyway. but i notice their vector
classes inherit from tuple. is a vector a form of tuple and is the
cartesian product of a tuple the same as the cross product of a
vector? if so, can someone recommend an algorithm laid out somewhere i
can reference?
A tuple is one way of representing a vector, but a vector has specific
semantics, such as rules for addition and products, that are not
inherent to a tuple.
Cartesian product is more closely related to outer product than to cross
product, and you say you really want cross product, so it does not help.
Patricia Shanahan wrote:
What exactly do you mean by "mathematically correct", and what is the data type of the >input? The formula would be easiest to apply, in Java, in double, but that would involve >rounding error. If you choose one of the representations that only involves addition, >subtraction, and multiplication you could do it exactly in BigDecimal.
so the vector coordinates could be read as int, float, or double for
what i am doing and actually the coordinates are stored as an array of
Point2D objects so that I can store the initial and terminal points in
pairs, which on second thought should be tuples which contain pairs of
"Number" instead of primitives?
What are "the initial and terminal points"?
I'm still not sure what you mean by "mathematically correct". You will
not, in general, get exact results using double or float, because of
rounding error.
Note that all possible values of int and float can be represented
exactly as double, so you could make things easier for yourself by
always storing double, and converting to float or int if you are asked
for those values.
Regardless of how you store your data, be careful to define rules for
rounding and overflows.
Patricia