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

雨燕双飞 提交于 2019-12-08 06:57:03

问题


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

Of course the good thing would be to have something that can be manipulated quite easily (normalization, scalar product and so on)

Thanks in advance


回答1:


MTJ has a Sparse Vector class. It has norm functions (1-norm 2-norm and ∞-norm) and dot product functions.




回答2:


JScience has a SparseVector implementation that is part of its linear algebra package.




回答3:


You can also try to look at la4j's CompressedVector implementation. It uses pair of arrays: array of values and array of their indicies. And with binary search on top of that it just flies. So, this implementation guarantees O(log n) running time for get/set operations.

Just a brief 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));


来源:https://stackoverflow.com/questions/1934254/which-is-the-best-way-to-implement-a-sparse-vector-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!