Top Description Fields Constructors Methods
org.apache.commons.math3.complex

public final Class Quaternion

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, org.apache.commons.math3.util.FastMath, .MathUtils, .Precision, org.apache.commons.math3.exception.DimensionMismatchException, .ZeroException, org.apache.commons.math3.exception.util.LocalizedFormats

This class implements quaternions (Hamilton's hypercomplex numbers).
Instance of this class are guaranteed to be immutable.
Since
3.1

Field Summary

Modifier and TypeField and Description
public static final Quaternion
I

i

public static final Quaternion
IDENTITY

Identity quaternion.

public static final Quaternion
J

j

public static final Quaternion
K

k

private final double
q0

First component (scalar part).

private final double
q1

Second component (first vector part).

private final double
q2

Third component (second vector part).

private final double
q3

Fourth component (third vector part).

private static final long
serialVersionUID

Serializable version identifier.

public static final Quaternion
ZERO

Zero quaternion.

Constructor Summary

AccessConstructor and Description
public
Quaternion(final double
Scalar component.
a
,
final double
First vector component.
b
,
final double
Second vector component.
c
,
final double
Third vector component.
d
)

Builds a quaternion from its components.

public
Quaternion(final double
Scalar part of the quaternion.
scalar
,
final double[]
Components of the vector part of the quaternion.
v
)

Builds a quaternion from scalar and vector parts.

public
Quaternion(final double[]
Components of the vector part of the pure quaternion.
v
)

Builds a pure quaternion from a vector (assuming that the scalar part is zero).

Method Summary

Modifier and TypeMethod and Description
public static Quaternion

Returns:

the sum of q1 and q2.
add
(final Quaternion
Quaternion.
q1
,
final Quaternion
Quaternion.
q2
)

Computes the sum of two quaternions.

public Quaternion

Returns:

the sum of this instance and q
add
(final Quaternion
Quaternion.
q
)

Computes the sum of the instance and another quaternion.

public static double

Returns:

the dot product of q1 and q2.
dotProduct
(final Quaternion
Quaternion.
q1
,
final Quaternion
Quaternion.
q2
)

Computes the dot-product of two quaternions.

public double

Returns:

the dot product of this instance and q.
dotProduct
(final Quaternion
Quaternion.
q
)

Computes the dot-product of the instance by a quaternion.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public boolean

Returns:

true if the each of the components are equal within the allowed absolute error.
equals
(final Quaternion
Quaternion with which to compare the current quaternion.
q
,
final double
Tolerance.
eps
)

Checks whether this instance is equal to another quaternion within a given tolerance.

public Quaternion

Returns:

the conjugate quaternion
getConjugate
()

Returns the conjugate quaternion of the instance.

public Quaternion

Returns:

the inverse.
getInverse
()

Returns the inverse of this instance.

public double

Returns:

the norm.
getNorm
()

Computes the norm of the quaternion.

public Quaternion

Returns:

the unit quaternion with positive scalar part.
getPositivePolarForm
()

Returns the polar form of the quaternion.

public double

Returns:

the scalar part.
getQ0
()

Gets the first component of the quaternion (scalar part).

public double

Returns:

the first component of the vector part.
getQ1
()

Gets the second component of the quaternion (first component of the vector part).

public double

Returns:

the second component of the vector part.
getQ2
()

Gets the third component of the quaternion (second component of the vector part).

public double

Returns:

the third component of the vector part.
getQ3
()

Gets the fourth component of the quaternion (third component of the vector part).

public double

Returns:

the scalar part.
getScalarPart
()

Gets the scalar part of the quaternion.

public double[]

Returns:

the vector part.
getVectorPart
()

Gets the three components of the vector part of the quaternion.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true if the scalar part of the quaternion is zero.
isPureQuaternion
(double
Tolerance (absolute error).
eps
)

Checks whether the instance is a pure quaternion within a given tolerance.

public boolean

Returns:

true if the norm is 1 within the given tolerance, false otherwise
isUnitQuaternion
(double
Tolerance (absolute error).
eps
)

Checks whether the instance is a unit quaternion within a given tolerance.

public static Quaternion

Returns:

the product q1 and q2, in that order.
multiply
(final Quaternion
First quaternion.
q1
,
final Quaternion
Second quaternion.
q2
)

Returns the Hamilton product of two quaternions.

public Quaternion

Returns:

the product of this instance with q, in that order.
multiply
(final Quaternion
Quaternion.
q
)

Returns the Hamilton product of the instance by a quaternion.

public Quaternion

Returns:

a scaled quaternion.
multiply
(final double
Scalar factor.
alpha
)

Multiplies the instance by a scalar.

public Quaternion

Returns:

a normalized quaternion.
normalize
()

Computes the normalized quaternion (the versor of the instance).

public static Quaternion

Returns:

the difference between q1 and q2.
subtract
(final Quaternion
First Quaternion.
q1
,
final Quaternion
Second quaternion.
q2
)

Subtracts two quaternions.

public Quaternion

Returns:

the difference between this instance and q.
subtract
(final Quaternion
Quaternion.
q
)

Subtracts a quaternion from the instance.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

Iback to summary
public static final Quaternion I

i

IDENTITYback to summary
public static final Quaternion IDENTITY

Identity quaternion.

Jback to summary
public static final Quaternion J

j

Kback to summary
public static final Quaternion K

k

q0back to summary
private final double q0

First component (scalar part).

q1back to summary
private final double q1

Second component (first vector part).

q2back to summary
private final double q2

Third component (second vector part).

q3back to summary
private final double q3

Fourth component (third vector part).

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable version identifier.

ZEROback to summary
public static final Quaternion ZERO

Zero quaternion.

Constructor Detail

Quaternionback to summary
public Quaternion(final double a, final double b, final double c, final double d)

Builds a quaternion from its components.

Parameters
a:double

Scalar component.

b:double

First vector component.

c:double

Second vector component.

d:double

Third vector component.

Quaternionback to summary
public Quaternion(final double scalar, final double[] v) throws DimensionMismatchException

Builds a quaternion from scalar and vector parts.

Parameters
scalar:double

Scalar part of the quaternion.

v:double[]

Components of the vector part of the quaternion.

Exceptions
DimensionMismatchException:
if the array length is not 3.
Quaternionback to summary
public Quaternion(final double[] v)

Builds a pure quaternion from a vector (assuming that the scalar part is zero).

Parameters
v:double[]

Components of the vector part of the pure quaternion.

Method Detail

addback to summary
public static Quaternion add(final Quaternion q1, final Quaternion q2)

Computes the sum of two quaternions.

Parameters
q1:Quaternion

Quaternion.

q2:Quaternion

Quaternion.

Returns:Quaternion

the sum of q1 and q2.

addback to summary
public Quaternion add(final Quaternion q)

Computes the sum of the instance and another quaternion.

Parameters
q:Quaternion

Quaternion.

Returns:Quaternion

the sum of this instance and q

dotProductback to summary
public static double dotProduct(final Quaternion q1, final Quaternion q2)

Computes the dot-product of two quaternions.

Parameters
q1:Quaternion

Quaternion.

q2:Quaternion

Quaternion.

Returns:double

the dot product of q1 and q2.

dotProductback to summary
public double dotProduct(final Quaternion q)

Computes the dot-product of the instance by a quaternion.

Parameters
q:Quaternion

Quaternion.

Returns:double

the dot product of this instance and q.

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
equalsback to summary
public boolean equals(final Quaternion q, final double eps)

Checks whether this instance is equal to another quaternion within a given tolerance.

Parameters
q:Quaternion

Quaternion with which to compare the current quaternion.

eps:double

Tolerance.

Returns:boolean

true if the each of the components are equal within the allowed absolute error.

getConjugateback to summary
public Quaternion getConjugate()

Returns the conjugate quaternion of the instance.

Returns:Quaternion

the conjugate quaternion

getInverseback to summary
public Quaternion getInverse()

Returns the inverse of this instance. The norm of the quaternion must not be zero.

Returns:Quaternion

the inverse.

Exceptions
ZeroException:
if the norm (squared) of the quaternion is zero.
getNormback to summary
public double getNorm()

Computes the norm of the quaternion.

Returns:double

the norm.

getPositivePolarFormback to summary
public Quaternion getPositivePolarForm()

Returns the polar form of the quaternion.

Returns:Quaternion

the unit quaternion with positive scalar part.

getQ0back to summary
public double getQ0()

Gets the first component of the quaternion (scalar part).

Returns:double

the scalar part.

getQ1back to summary
public double getQ1()

Gets the second component of the quaternion (first component of the vector part).

Returns:double

the first component of the vector part.

getQ2back to summary
public double getQ2()

Gets the third component of the quaternion (second component of the vector part).

Returns:double

the second component of the vector part.

getQ3back to summary
public double getQ3()

Gets the fourth component of the quaternion (third component of the vector part).

Returns:double

the third component of the vector part.

getScalarPartback to summary
public double getScalarPart()

Gets the scalar part of the quaternion.

Returns:double

the scalar part.

See Also
getQ0()
getVectorPartback to summary
public double[] getVectorPart()

Gets the three components of the vector part of the quaternion.

Returns:double[]

the vector part.

See Also
getQ1(), getQ2(), getQ3()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isPureQuaternionback to summary
public boolean isPureQuaternion(double eps)

Checks whether the instance is a pure quaternion within a given tolerance.

Parameters
eps:double

Tolerance (absolute error).

Returns:boolean

true if the scalar part of the quaternion is zero.

isUnitQuaternionback to summary
public boolean isUnitQuaternion(double eps)

Checks whether the instance is a unit quaternion within a given tolerance.

Parameters
eps:double

Tolerance (absolute error).

Returns:boolean

true if the norm is 1 within the given tolerance, false otherwise

multiplyback to summary
public static Quaternion multiply(final Quaternion q1, final Quaternion q2)

Returns the Hamilton product of two quaternions.

Parameters
q1:Quaternion

First quaternion.

q2:Quaternion

Second quaternion.

Returns:Quaternion

the product q1 and q2, in that order.

multiplyback to summary
public Quaternion multiply(final Quaternion q)

Returns the Hamilton product of the instance by a quaternion.

Parameters
q:Quaternion

Quaternion.

Returns:Quaternion

the product of this instance with q, in that order.

multiplyback to summary
public Quaternion multiply(final double alpha)

Multiplies the instance by a scalar.

Parameters
alpha:double

Scalar factor.

Returns:Quaternion

a scaled quaternion.

normalizeback to summary
public Quaternion normalize()

Computes the normalized quaternion (the versor of the instance). The norm of the quaternion must not be zero.

Returns:Quaternion

a normalized quaternion.

Exceptions
ZeroException:
if the norm of the quaternion is zero.
subtractback to summary
public static Quaternion subtract(final Quaternion q1, final Quaternion q2)

Subtracts two quaternions.

Parameters
q1:Quaternion

First Quaternion.

q2:Quaternion

Second quaternion.

Returns:Quaternion

the difference between q1 and q2.

subtractback to summary
public Quaternion subtract(final Quaternion q)

Subtracts a quaternion from the instance.

Parameters
q:Quaternion

Quaternion.

Returns:Quaternion

the difference between this instance and q.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override