Java向量数学可读性

Java向量数学可读性,java,readability,Java,Readability,假设您想进行一些简单的向量计算: //this could be C++ MyVectorType position = ...; MyVectorType velocity = ...; float dt; //Here's the expression I want to calculate: position += dt*velocity; 现在让我们假设您想在Java中实现这一点。没有运算符重载-好的,我可以不使用 //this still could be C++ MyVectorT

假设您想进行一些简单的向量计算:

//this could be C++
MyVectorType position = ...;
MyVectorType velocity = ...;
float dt;
//Here's the expression I want to calculate:
position += dt*velocity;
现在让我们假设您想在Java中实现这一点。没有运算符重载-好的,我可以不使用

//this still could be C++
MyVectorType position = ...;
MyVectorType velocity = ...;
float dt;
//Here's the expression I want to calculate:
position.add(velocity.times(dt));
我会说它的可读性较差,但仍然可以。我应该如何用Java编写上面的代码?我想我应该使用
javax.vecmath

//my attempt in Java
Vector3f velocity = new Vector3f(...);
Vector3f position = new Vector3f(...);
float dt;
//Here's the expression I want to calculate - three lines.
Vector3f deltaPosition = new Vector3f(velocity);
deltaPosition.scale(dt);
position.add(deltaPosition);
这么简单的操作真的需要这三条线吗?对于习惯于阅读数学表达式的人来说,我认为这是一种痛苦,尤其是当运算变得更加复杂时。而且,写这样的表达方式并不是一种真正的乐趣


我错过什么了吗?或者是否还有另一个向量数学包可以产生更可读的代码?

不幸的是,javax.vecmath包有点笨重。你可能想看看NIST。它是一个矩阵包,而不是向量包(例如,没有叉积),但至少使用该包,您可以在一行上将操作链接在一起:

double[][] array = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}}; 
Matrix A = new Matrix(array); 
Matrix b = Matrix.random(3,1); 
Matrix x = A.solve(b); 
Matrix Residual = A.times(x).minus(b); 
通过将向量视为1xN矩阵,可以进行很多向量运算


NIST还发布了一个网站,如果JAMA不太正确,你可以在那里找到更符合你需要的东西。

我认为,在java中,链接是一种方式。