How to store ojalgo sparse array to file in java?

流过昼夜 提交于 2021-02-10 12:01:25

问题


I currently have a SparseStore matrix on which I perform a lot of counting and calculations. I want to store it to file, so I can later reuse it without re-doing all previous calculations.

I tried basic serialization in Java:

ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(exportFileName));
outputStream.writeObject(mySparseStoreInstance);

but it seems the class doesn't implement java.io.Serializable:

java.io.NotSerializableException: org.ojalgo.matrix.store.SparseStore

I wouldn't mind changing to Array2D or some other ojalgo sparse structure, but also can't find if any of those can be serialized.

What is recommended to store sparse arrays to file?


回答1:


There is no built-in feature for this. At this point you have to implement something yourself. Extract the index-value pairs using nonzeros() and write to a file, and then recreate using set(index, value) one by one. Adding new elements in a strictly increasing index order is not terribly inefficient.



来源:https://stackoverflow.com/questions/57985654/how-to-store-ojalgo-sparse-array-to-file-in-java

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