Vector
There are two main implementations of vectors in this calculator, a generic n-th tuple vector and a specific 3 dimensional case.
Mathematically, the first can be thought of as some vector, and the last as some vector,
so that:
So, how do we represent these in code? The first we represent using an array of complex numbers which we can determine the size of at initialization. For each function, we then need to loop through the arrays performing the necessary proceedures. The second we can simply represent with solid member variables for the x, y and z dimensions. We then hard-code these in each function, hopefully improving efficiency.
Addition
We specify our addition function using the following prototype:
public Vector add( Vector rhs )
And can be called as follows:
Vector vec1, vec2, vec3;
vec3 = vec1.add(vec2);
Obviously this example will not work, as we have not inialized the vectors, but it demonstrates the concept simply enough.
We compute addition through a simple for loop that iterates between 0 and the size of the vector -1 (due to zero-array indexing), adding the components of each vector together and puting it at that index in the result vector.
Mathematically, for two vectors, and
in
, the addition function returns the following:
The addition function is also specifically overloaded for the 3D case, such that it's performance should be marginally greater, due to the removal of the loop overhead.
Subtraction
Subtraction follows a similar paradigm as the vector addition, and is also overloaded for the specific 3D case, the subtraction function is prototyped as following:
public Vector sub( Vector rhs )
And can be called in the same manner to addition (again, this example will not work):
Vector vec1, vec2, vec3;
vec3 = vec1.sub(vec2);
Subtraction is performed by looping over each of the n indices of the vectors, and subtracting the values at those indices, and placing it at that index of the result vector:
Mathematically, for two vectors, and
in
, the subtraction function returns the following:
Again, the subtraction function is defined for both the 3D-case and the n-th tuple case.
Multiplication
Scalar multiplication is defined for both the n-th tuple vector and the 3D vector. We use the following function prototype for multiplication:
public Vector multiply( Complex rhs )
And this can then be called as follows:
Complex cmplxScalar;
Vector vec1, vec2;
vec2 = vec1.multiply(cmplxScalar);
Multiplication is performed by looping over each element in the vector and multiplying that element by the given scalar, and then placing it in the return vector.
Mathematically for some vector, in
and some scalar,
in
, the multiplication function returns the following:
Again, this is defined without the loop overhead for the 3D case.
Division
Scalar division is defined for both the n-th tuple and 3D vectors. We use the following prototype for division:
public Vector divide( Complex rhs )
And this can then be called as follows:
Complex cmplxScalar;
Vector vec1, vec2;
vec2 = vec1.divide(cmplxScalar);
Scalar division is performed by looping over each element in the vector and dividing that element by the given scalar, and then placing it in the return vector.
Mathematically for some vector, in
and some scalar,
in
, the division function returns the following:
Again, this is defined without the loop overhead for the 3D case.
Dot/Scalar Product
The dot/scalar product is again defined for both the n-th tuple and 3D vectors. We use the following prototype for the dot product:
public Complex dot( Vector rhs )
And this can then be called as follows:
Complex cmplxScalar;
Vector vec1, vec2;
cmplxScalar = vec1.dot(vec2);
The dot product is computed by computing the summation between 1 and n, where n is the dimensionality of the vector of the products of the elements at the each index of the vector.
Mathematically, the dot/scalar product is defined, for two vectors, and
in
as:
Cross Product
The cross product is currently only defined for the 3D case, although there are plans to implement the 7D cross product also, (although the non-uniqueness of the 7D cross-product is a present issue, we need to decide on a standard). The function prototype is the following:
public Vector3D cross( Vector3D rhs )
And can be called in the following manner:
Vector3D vec1, vec2, vec3;
vec3 = vec1.cross(vec2);
The cross product is hard coded, and for efficiency utilizes the formula shown below. The cross product is used in determining a mutually orthogonal vector to the two input vectors, our implementation returns the orthonormal vector corresponding to a right-handed co-ordinate system.
Mathematically, the cross product is, for two vectors and
in
, defined as:
m_cmplxMembers (Vector only)
The n-th tuple vector uses an array of complex numbers to represent the elements.
In code it is declared as:
public Complex[] m_cmplxMembers;
This array can be accessed externally as follows:
Complex cmplxScalar;
Vector vec1;
cmplxScalar = vec1.m_cmplxMembers[0]; // Get the x-co-ordinate of the vector.
m_iLength (Vector only)
The n-th tuple vector implementation uses this variable to keep track of it's length.
In code it is declared as:
private int m_iLength;
m_x, m_y & m_z
Vector3D does not use an array to keep it's variables, it stores them in individual Complex numbers. Representing the x-, y- and z-coordinates of the vector.
They are declared as:
public Complex m_x, m_y, m_z;
0 Comments
