What is the best way to implement a sparse vector in Java?

What is the best way to implement a sparse vector in Java?

Of course, it would be nice to have something that can be easily manipulated (normalization, scalar product, etc.)

Thank you in advance

+1
source share
3 answers

MTJ has a Sparse Vector class. It has norm functions (1-norm, 2-norm and inf-norm) and functions of point products.

+2
source

JScience has SparseVector , which is part of the linear algorithm package.

+2
source

You can also watch la4j CompressedVector . It uses a couple of arrays: an array of values ​​and an array of their pointers. And with binary search on top of it, it just flies. Thus, this implementation guarantees O(log n) runtime for get / set operations.

Just a quick example

 Vector a = new CompressedVector(new double[]{ 1.0, 2.0, 3.0 }). // calculates L_1 norm of the vector double n = a.norm(); // calculates the sum of vectors elements double s = a.fold(Vectors.asSumAccumulator(0.0)); 
+1
source

All Articles