开发者

Quaternion Comparison?

开发者 https://www.devze.com 2023-03-01 13:40 出处:网络
Is quaternion comparison possible? I\'m writing a Java class of Quaternions and I want to implement the Comparable interface to use the Collections.sort(List<Quaternion>) facility. I\'m not expe

Is quaternion comparison possible? I'm writing a Java class of Quaternions and I want to implement the Comparable interface to use the Collections.sort(List<Quaternion>) facility. I'm not expert at math, I really don't understand the things I read about Quatern开发者_如何转开发ions. So, can anyone tell me can I override the compareTo method for Quaternions and how?

My class declarition:

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;
    }


You can implement compareTo, by comparing its fields. However, you need to determine what you want the order to be like. AFAIK, there is no standard definition of what comes before or after for complex numbers let alone a quaternion.


You certainly can compare them; whether the comparison is meaningful or not is open to debate. Since a quaternion can represented by four real numbers, you'd 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 might use an epsilon-based comparison, and you need to convert small positive and negative differences into positive and negative integers. But you get the idea.


There is no reason why you can't compare two quaternions. Assuming that you want to compare magnitudes, compute and compare the Quaternion Norms. Your Quaternion class should have a norm (magnitude) method allowing a toCompare to be something like the following:

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

A better version would be:

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;
}


A 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 - o.s*o.s - o.i*o.i - o.j*o.j - o.k*o.k;
    if (result > 0) { return 1; }
    if (result < 0) { return -1; }
    return 0;
  }
}

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


Think about quaternions as a tuple (ordered list) of four floating-point numbers. Defining equality is pretty straightforward, but how would you define total order? In other words, how do you want to define greater-than relationship between two four-number sequences?

In fact, there is no common greater-than relationship even between complex numbers and quaternions can be considered as a pair of complex numbers. Easy comparison is only possible in one-dimensional space. Complex numbers are two-dimensional, quaternions - four.


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

The argument is the same as for complex numbers. Given two quaternions, they are either equal or not, there is no way to say which one is greater than the other. The quaternions form a division algebra, which is not ordered (unlike the field of the real numbers for example). The only (reasonable) way, I can think of, comparing two quaternions is by using the norm.

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

In that case you could define, that a quaternion a is greater than a quaternion b iff the norm of a is greater than the norm of b. But that is definitely not a standard definition. I would be careful in making quaternions or complex numbers comparable. However, it depends on your use case. Just take into account, that there is no standard way of sorting such numbers.

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

Another way to compare quaternions would be to use a lexicographic order.


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

You may nevertheless want to implement the Comparable interface, for conveniently sorting and for storing them in TreeSet and TreeMap collections.

To make clear that the ordering is arbitrary I'd use the lexicographic combination of the components of the quaternion. This also ensures that the ordering is consistent with equals, and that the algorithms work as desired.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号