Class Quaternion
java.lang.Object
org.apache.commons.numbers.quaternion.Quaternion
- All Implemented Interfaces:
Serializable
This class implements
quaternions (Hamilton's hypercomplex numbers).
Wherever quaternion components are listed in sequence, this class follows the
convention of placing the scalar (w) component first, e.g. [w, x, y, z].
Other libraries and textbooks may place the w component last.
Instances of this class are guaranteed to be immutable.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Quaternioni.static final Quaternionj.static final Quaternionk.static final QuaternionIdentity quaternion.static final QuaternionZero quaternion. -
Method Summary
Modifier and TypeMethodDescriptionadd(Quaternion q) Computes the sum of the instance and another quaternion.static Quaternionadd(Quaternion q1, Quaternion q2) Computes the sum of two quaternions.Returns the conjugate of this quaternion number.divide(double alpha) Divides the instance by a scalar.doubledot(Quaternion q) Computes the dot-product of the instance by a quaternion.static doubledot(Quaternion q1, Quaternion q2) Computes the dot-product of two quaternions.booleanbooleanequals(Quaternion q, double eps) Checks whether this instance is equal to another quaternion within a given tolerance.doubleGets the scalar part of the quaternion.double[]Gets the three components of the vector part of the quaternion.doublegetW()Gets the first component of the quaternion (scalar part).doublegetX()Gets the second component of the quaternion (first component of the vector part).doublegetY()Gets the third component of the quaternion (second component of the vector part).doublegetZ()Gets the fourth component of the quaternion (third component of the vector part).inthashCode()inverse()Returns the inverse of this instance.booleanisPure(double eps) Checks whether the instance is a pure quaternion within a given tolerance.booleanisUnit(double eps) Checks whether the instance is a unit quaternion within a given tolerance.multiply(double alpha) Multiplies the instance by a scalar.Returns the Hamilton product of the instance by a quaternion.static Quaternionmultiply(Quaternion q1, Quaternion q2) Returns the Hamilton product of two quaternions.negate()Returns the opposite of this instance.doublenorm()Computes the norm of the quaternion.Computes the normalized quaternion (the versor of the instance).doublenormSq()Computes the square of the norm of the quaternion.static Quaternionof(double[] v) Builds a pure quaternion from a vector (assuming that the scalar part is zero).static Quaternionof(double scalar, double[] v) Builds a quaternion from scalar and vector parts.static Quaternionof(double w, double x, double y, double z) Builds a quaternion from its components.static QuaternionParses a string that would be produced bytoString()and instantiates the corresponding object.Returns the polar form of the quaternion.Subtracts a quaternion from the instance.static Quaternionsubtract(Quaternion q1, Quaternion q2) Subtracts two quaternions.toString()
-
Field Details
-
ZERO
Zero quaternion. -
ONE
Identity quaternion. -
I
i. -
J
j. -
K
k.
-
-
Method Details
-
of
Builds a quaternion from its components.- Parameters:
w- Scalar component.x- First vector component.y- Second vector component.z- Third vector component.- Returns:
- a quaternion instance.
-
of
Builds a quaternion from scalar and vector parts.- Parameters:
scalar- Scalar part of the quaternion.v- Components of the vector part of the quaternion.- Returns:
- a quaternion instance.
- Throws:
IllegalArgumentException- if the array length is not 3.
-
of
Builds a pure quaternion from a vector (assuming that the scalar part is zero).- Parameters:
v- Components of the vector part of the pure quaternion.- Returns:
- a quaternion instance.
-
conjugate
Returns the conjugate of this quaternion number. The conjugate ofa + bi + cj + dkisa - bi -cj -dk.- Returns:
- the conjugate of this quaternion object.
-
multiply
Returns the Hamilton product of two quaternions.- Parameters:
q1- First quaternion.q2- Second quaternion.- Returns:
- the product
q1andq2, in that order.
-
multiply
Returns the Hamilton product of the instance by a quaternion.- Parameters:
q- Quaternion.- Returns:
- the product of this instance with
q, in that order.
-
add
Computes the sum of two quaternions.- Parameters:
q1- Quaternion.q2- Quaternion.- Returns:
- the sum of
q1andq2.
-
add
Computes the sum of the instance and another quaternion.- Parameters:
q- Quaternion.- Returns:
- the sum of this instance and
q.
-
subtract
Subtracts two quaternions.- Parameters:
q1- First Quaternion.q2- Second quaternion.- Returns:
- the difference between
q1andq2.
-
subtract
Subtracts a quaternion from the instance.- Parameters:
q- Quaternion.- Returns:
- the difference between this instance and
q.
-
dot
Computes the dot-product of two quaternions.- Parameters:
q1- Quaternion.q2- Quaternion.- Returns:
- the dot product of
q1andq2.
-
dot
Computes the dot-product of the instance by a quaternion.- Parameters:
q- Quaternion.- Returns:
- the dot product of this instance and
q.
-
norm
Computes the norm of the quaternion.- Returns:
- the norm.
-
normSq
Computes the square of the norm of the quaternion.- Returns:
- the square of the norm.
-
normalize
Computes the normalized quaternion (the versor of the instance). The norm of the quaternion must not be near zero.- Returns:
- a normalized quaternion.
- Throws:
IllegalStateException- if the norm of the quaternion is NaN, infinite, or near zero.
-
equals
-
hashCode
-
equals
Checks whether this instance is equal to another quaternion within a given tolerance.- Parameters:
q- Quaternion with which to compare the current quaternion.eps- Tolerance.- Returns:
trueif the each of the components are equal within the allowed absolute error.
-
isUnit
Checks whether the instance is a unit quaternion within a given tolerance.- Parameters:
eps- Tolerance (absolute error).- Returns:
trueif the norm is 1 within the given tolerance,falseotherwise
-
isPure
Checks whether the instance is a pure quaternion within a given tolerance.- Parameters:
eps- Tolerance (absolute error).- Returns:
trueif the scalar part of the quaternion is zero.
-
positivePolarForm
Returns the polar form of the quaternion.- Returns:
- the unit quaternion with positive scalar part.
-
negate
Returns the opposite of this instance.- Returns:
- the quaternion for which all components have an opposite sign to this one.
-
inverse
Returns the inverse of this instance. The norm of the quaternion must not be zero.- Returns:
- the inverse.
- Throws:
IllegalStateException- if the norm (squared) of the quaternion is NaN, infinite, or near zero.
-
getW
Gets the first component of the quaternion (scalar part).- Returns:
- the scalar part.
-
getX
Gets the second component of the quaternion (first component of the vector part).- Returns:
- the first component of the vector part.
-
getY
Gets the third component of the quaternion (second component of the vector part).- Returns:
- the second component of the vector part.
-
getZ
Gets the fourth component of the quaternion (third component of the vector part).- Returns:
- the third component of the vector part.
-
getScalarPart
Gets the scalar part of the quaternion.- Returns:
- the scalar part.
- See Also:
-
getVectorPart
Gets the three components of the vector part of the quaternion.- Returns:
- the vector part.
- See Also:
-
multiply
Multiplies the instance by a scalar.- Parameters:
alpha- Scalar factor.- Returns:
- a scaled quaternion.
-
divide
Divides the instance by a scalar.- Parameters:
alpha- Scalar factor.- Returns:
- a scaled quaternion.
-
parse
Parses a string that would be produced bytoString()and instantiates the corresponding object.- Parameters:
s- String representation.- Returns:
- an instance.
- Throws:
NumberFormatException- if the string does not conform to the specification.
-
toString
-