java矩阵/向量库的最佳实践

发布于 2024-12-09 09:57:34 字数 389 浏览 0 评论 0原文

我必须为我正在从事的一个小型几何相关项目编写一个简单的矢量/矩阵库。这就是我想知道的。

在java环境中对向量进行数学运算时,返回向量的新实例或修改原始向量的状态是更好的做法吗?

我已经反复看过它,只是想得到大多数人的意见。

某些人说向量应该是不可变的,并且应该使用静态方法来创建新向量,其他人说它们应该是可变的,并且应该使用普通方法来修改它们的状态。我在某些情况下看到过,对象是不可变的,并且调用普通方法,该方法从对象返回一个新向量而不改变状态 - 这对我来说似乎有点不对劲。

我只是想了解一下是否有任何最佳实践 - 我想这是已经做过一百万次的事情,我真的只是想知道是否有一个标准方法可以做到这一点。

我注意到 apache commons 数学库每次都会从原始向量返回一个新向量。

i have to write a simple vector/matrix library for a small geometry related project i'm working on. here's what i'm wondering.

when doing mathematical operations on vectors in a java environment, is it better practice to return a new instance of a vector or modify the state of the original.

i've seen it back and forth and would just like to get a majority input.

certain people say that the vectors should be immutable and static methods should be used to create new ones, others say that they should be mutable and normal methods should be used to modify their state. i've seen it in some cases where the object is immutable and normal methods are called which returns a new vector from the object without changing the state - this seems a little off to me.

i would just like to get a feel for if there is any best practice for this - i imagine it's something that's been done a million times and am really just wondering if there's a standard way to do this.

i noticed the apache commons math library returns a new vector every time from the original.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-12-16 09:57:34

性能有多重要?向量运算是否会成为一个很大的组成部分,从而影响整个系统的性能?

如果不是,并且会有大量并发,那么不可变向量将很有用,因为它们减少了并发问题。

如果向量上有很多突变,那么不可变向量所需的新对象的开销将变得很大,并且最好使用可变向量并以困难的方式实现并发。

How important is performance going to be? Is vector arithmetic going to be a large component so that it affects the performance of overall system?

If it is not and there is going to be lot of concurrency then immutable vectors will be useful because they reduce concurrency issues.

If there are lot of mutations on vectors then the overhead of new objects that immutable vectors will require will become significant and it may be better to have mutable vectors and do the concurrency the hard way.

入怼 2024-12-16 09:57:34

这取决于。一般来说,不变性更好。

首先也是最重要的,它自动是线程安全的。更容易维护和测试。

也就是说,有时您需要速度,而创建新实例会花费太多时间。

(注意:如果您不是 100% 肯定您需要那么快的速度,那么您就不需要它。想想高频交易和实时数学密集型应用程序。尽管如此,您应该首先变得简单,然后稍后优化。)

至于静态方法与普通方法,遵循良好的 OOP 原则,您不应该使用静态方法。要创建新的向量/矩阵,您可以使用构造函数。

接下来,你的支持结构是什么?您最好的选择可能是向量的单维双精度数组和矩阵的双精度多维数组。这至少可以让您通过使用原始对象保持相对快速的速度。

如果您需要更高的性能,您可以在矢量/矩阵上添加可以更改支持数据的修改器。您甚至可以决定维度是不可变的,但内容是可变的,这也将为您提供一些其他安全性。

It depends. Generally speaking, immutability is better.

First and foremost, it is automatically threadsafe. It is easier to maintain and test.

That said, sometimes you need speed where creating new instances will take too much time.

(Note: If you're not 100% positive you need that amount of speed, you don't need it. Think high-frequency trading and real-time math-intensive applications. And even though, you should go simple first, and optimize later.)

As for static vs normal methods, following good OOP principles, you shouldn't have static methods. To create new vectors/matrices you can use the constructor.

Next, what's your backing structure? Your best bet is probably single-dimensional arrays of doubles for vectors and multi-dimensional arrays of doubles for matrices. This at least lets you stay relatively quick by using primitive objects.

If you get to the point that you need even more performance, you can add modifiers on your Vector/Matrix that can change the backing data. You could even decide that the dimensions are immutable but the contents are mutable which would give you some other safeties as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文