dkm icon indicating copy to clipboard operation
dkm copied to clipboard

Add dynamic matrix

Open Eleobert opened this issue 3 years ago • 4 comments

Instead of passing std::vector<std::array<T, N>> I added a new class as_matrix that allow passing data with size defined at runtime. The class is only an interface and all operations are read only.

For example, if the user has the data stored in std::vector<std::pair<float, float>>, we can easily pass it without having to copy to a new container:

auto matrix = dkm::as_matrix(reinterpret_cast<float*>(data.data()), data.size(), 2, false);
auto res = km::kmeans_lloyd(matrix, k);

Or if the data is an armadillo matrix (Eigen, etc) it is even easier:

auto matrix = dkm::as_matrix(data.memptr(), data.n_rows, data.n_cols);
auto res = km::kmeans_lloyd(matrix, k);

I think this approach is much better than the current one. For now, I only added the class and changed dkm.hpp (since I don't know if this will get merged and this is the only relevant part for me). One performance issue we have is as_matrix::row returning by value, but this can be easily fixed.

Eleobert avatar Feb 06 '21 14:02 Eleobert

Hi, thanks for your contribution!

This looks similar to an idea I had for a custom matrix data structure. I'll have a more detailed look at it over this weekend.

genbattle avatar Feb 26 '21 01:02 genbattle

Just to close this, I think mdspan is a better alternative.

Eleobert avatar Oct 08 '21 21:10 Eleobert

Hi, did this simply stall? Looks like a very useful addition.

g40 avatar May 05 '23 16:05 g40

Hi, did this simply stall? Looks like a very useful addition.

Yes, this change stalled, and I haven't had time to implement/update it myself.

genbattle avatar May 08 '23 22:05 genbattle