四元数比较?
四元数比较可能吗?我正在编写一个 Java 类四元数,我想实现 Comparable
接口使用 Collections.sort(List
工具。我不是数学专家,我真的不明白我读到的有关四元数的内容。那么,谁能告诉我我可以重写四元数的compareTo方法以及如何重写吗?
我的班级宣言:
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;
}
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 Quaternions. 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过比较其字段来实现compareTo。但是,您需要确定您想要的顺序。 AFAIK,对于复数之前或之后的内容没有标准定义,更不用说四元数了。
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.
您当然可以比较它们;这种比较是否有意义还有待商榷。由于四元数可以由四个实数表示,因此您只需执行类似的操作(伪代码)
由于值是实数,因此您可以使用基于 epsilon 的比较,并且需要将小的正差和负差转换为正差和负差整数。但你明白了。
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)
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.
没有理由不能比较两个四元数。假设您要比较幅度,请计算并比较四元数范数。您的四元数类应该有一个范数(大小)方法,允许 toCompare 类似于以下内容:
更好的版本是:
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:
A better version would be:
四元数是一种 4 维向量。
您想如何订购?最合理的方法是使用规范。
请注意,使用范数将使长度相等但指向不同方向的四元数相等,并且某些算法将无法区分它们。排序算法很可能会丢弃“重复项”。只是一个友好的警告。
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.
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.
你可以,但我认为你不应该这样做。
该参数与复数相同。给定两个四元数,它们要么相等,要么不相等,无法判断哪个四元数大于另一个。四元数形成除法代数,它是无序的(例如与实数域不同)。我能想到的唯一(合理的)方法是使用范数来比较两个四元数。
在这种情况下,您可以定义,四元数a大于四元数b,当且仅当a的范数大于b的范数。但这绝对不是标准定义。我会小心地使四元数或复数具有可比性。但是,这取决于您的用例。请考虑到,没有标准方法对这些数字进行排序。
请参阅此 Google 搜索,获取一些有关比较复数的好链接。四元数的论证基本相同。
比较四元数的另一种方法是使用字典顺序。
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.
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.
四元数或复数没有数学标准排序。
不过,您可能希望实现 Comparable 接口,以便方便地排序并将它们存储在 TreeSet 和 TreeMap 集合中。
为了明确排序是任意的,我将使用四元数组件的字典组合。这也确保了排序与
equals
一致,并且算法按需要工作。对于更自然的排序,例如考虑范数的排序,您始终可以显式定义比较器。
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 inTreeSet
andTreeMap
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.