What Java class should I use to represent a Vector? - Game Development Stack Exchange
Java matrix libraries - Stack Overflow
Performing Vector operations in Java?
Any Java library for 2D vectors (geometry)? Stable/maintained/perspective? - Stack Overflow
Factsheet
/ December 4, 2022; 3 years ago
/ December 4, 2022; 3 years ago
There is no built in math Vector class. You'll have to implement your own or use a library like JScience - its project page is here.
EDIT: As comment below suggested, JScience isn't really designed for gaming application is missing some optimizations. A more game oriented vector math API can be found in libgdx. I imagine it should not be too difficult to extract just the math classes from libgdx.
The vecmath-package from Java 3D can be used separately from the rest of the project. It allows in-place modification of vectors and has various methods that are convenient and efficient for games (such as lengthSquared, which avoids a √-operation).
vecmath.jar is 312K and works well separately from the rest of Java3D. It may be included under javax.vecmath in some Java distributions, but you can download it at the below link if not.
Downloads | Documentation
Some Java libraries for linear algebra are:
- Apache Commons Math: http://commons.apache.org/proper/commons-math/
- jeigen - a wrapper for eigen - https://github.com/hughperkins/jeigen (includes complex and rarely found feature like matrix exponential and matrix logarithm)
- jblas http://mikiobraun.github.io/jblas/ (also features more complex functions like matrix exponential, also very fast).
- Colt http://acs.lbl.gov/software/colt/
- JAMA http://math.nist.gov/javanumerics/jama/
- UJMP - http://sourceforge.net/projects/ujmp/
EDIT maybe we can extend this list whenever one comes across and you know - the world keeps moving:
- ojAlgo - http://ojalgo.org/ has promising benchmarks
- Efficient Java Matrix Library (EJML) - http://ejml.org
- ParallelColt - https://sites.google.com/site/piotrwendykier/software/parallelcolt
- la4j - http://la4j.org/
- MTJ - https://github.com/fommil/matrix-toolkits-java
- nd4j - https://nd4j.org/ lets you choose underlying native implementations like cuda or openBlas
Note: Personally: I use Apache Commons Math and Colt in my own project (http://www.finmath.net). While commons math is actively developed, I found that Colt is still faster in some tasks (like Eigenvalue decomposition). For that reason I use some kind of wrapper which allows me to quickly switch the underlying library (I only need a few things like solving systems of equations and Eigenvalue decomposition).
Try Apache Commons Math library. org.apache.commons.math3.linear package contains the functions that you want. Home page
I have an application where I need to perform vector operations on 3d vectors, and I really don't want to have to write the whole thing myself. Is there something I can use? Some library maybe? Thanks in advance
Yes, you'll have to write a class or use a library such as JScience
If you are looking to make a vector in 2d space, couldn't you just go with a simple Point2D(x,y) and let the length of your vector define magnitude?
So that Point2D a = new Point2D(1,1); has a magnitude of 1.4, and a NE direction. And a Point2D b = new Point2D(2,2); has the same direction but a magnitude of 2.8...
Addition would then just be: Point2D c = new Point2D(a.x + b.x, a.y + b.y);
In 3d space I would create my own class, or an entirely different data structure depending on you actual problem.
Edit: I hope he has found a solution in the past 3 years..