Comparison of quaternions?

Is a comparison of quaternions possible? I am writing the Java Quaternions class, and I want to implement a Comparable interface for using the Collections.sort(List<Quaternion>) tool. I am not a specialist in mathematics, I really do not understand what I read about quaternions. So, can someone tell me if I can override the compareTo method for Quaternions and how?

My class declaration:

 public class Quaternion implements Serializable, Comparable<Quaternion> { private double s; // scalar part private double i, j, k; // vectorel part public Quaternion() { super(); } public Quaternion(double s, double i, double j, double k) { super(); this.s = s; this.i = i; this.j = j; this.k = k; } 
+6
source share
7 answers

There is no reason why you cannot compare the two quaternions. Assuming you want to compare values, calculate and compare Quaternion Norms . Your Quaternion class should have a norm (size) method that allows toCompare to be something like the following:

 int compareTo(Quaternion o){ return (int)(this.norm() - o.norm()); } 

The best version:

 int compareTo(Quaternion o){ // return (int)(this.norm() - o.norm()); double tNorm = this.norm; double oNorm = o.norm; int retVal = 0; if (tNorm < oNorm){ retVal = -1; } else if (tNorm > oNorm){ retVal = 1; } return retVal; } 
+1
source

You can implement compareTo by comparing its fields. However, you need to determine what you want the order to look like. AFAIK, there is no standard definition of what happens before or after for complex numbers, not to mention the quaternion.

+2
source

You can of course compare them; whether the comparison is significant or not is open to discussion. Since a quaternion can be represented by four real numbers, you just do something like (pseudocode)

 if (q1.a != q2.a) return q1.a - q2.a; else if (q1.b != q2.b) return q1.b - q2.b; else if (q1.c != q2.c) return q1.c - q2.c; else return q1.d - q2.d; 

Since the values โ€‹โ€‹are real numbers, you can use epsilon-based comparisons, and you need to convert small positive and negative differences to positive and negative integers. But you have an idea.

+2
source

Quaternion is a kind of 4-dimensional vector. How do you want to order them? The most reasonable way would be to use the norm.

 public int compareTo(Object o) { if (o instanceOf Quaternion) { // Compute the difference between the square of the norm double result = s*s + i*i + j*j + k*k - os*os - oi*oi - oj*oj - ok*ok; if (result > 0) { return 1; } if (result < 0) { return -1; } return 0; } } 

Note that using the norm will make the quaternions of equal length, but pointing in different directions equal, and some algorithms will not be able to distinguish them. Sorting algorithms may discard duplicates. Just a friendly warning.

+1
source

Think of quaternions as a tuple (ordered list) of four floating point numbers. Defining equality is pretty simple, but how would you define the general order? In other words, how do you want to define more than the ratio between two four sequences?

In fact, there is no general relation more than the relationship between complex numbers and quaternions, can be considered as a pair of complex numbers. A simple comparison is only possible in one-dimensional space. Complex numbers are two-dimensional; quaternions are four.

0
source

You can, but I don't think you should.

The argument is the same as for complex numbers. Given the two quaternions, they are equal or not, there is no way to say which one is larger than the other. Quaternions form a division algebra that is not ordered (in contrast to the field of real numbers, for example). The only (reasonable) way I can think of, compare two quaternions is to use the norm.

 double norm = Math.sqrt(s*s + i*i + j*j + k*k); 

In this case, you can determine that quaternion a is larger than quaternion b if norm a is greater than norm b. But this is definitely not a standard definition. I would be careful in comparing quaternions or complex numbers. However, it depends on your use case. Just keep in mind that there is no standard way to sort such numbers.

See google search for some good links on comparing complex numbers. The argument for quaternions is basically the same.

Another way to compare quaternions is to use the lexicographic order .

0
source

There is no mathematical standard order for quaternions or complex numbers.

However, you can implement the Comparable interface to conveniently sort and store them in the TreeSet and TreeMap collections.

To make it clear that the ordering is arbitrary, I would use a lexicographic combination of quaternion components. It also ensures that ordering is consistent with equals and that algorithms work as desired.

For a more natural ordering, for example, taking into account the norm, you can always explicitly define a comparator.

0
source

Source: https://habr.com/ru/post/886791/


All Articles