mikera / core.matrix

core.matrix : Multi-dimensional array programming API for Clojure
Other
701 stars 113 forks source link

index-max #332

Closed Hendekagon closed 6 years ago

Hendekagon commented 6 years ago

I have a need for a fast index-max, which returns the index of the maximum value in a vector, or a sequence of indices for a matrix. The way I'm currently doing it is linear time.

claj commented 6 years ago

Is there any implementation in any current matrix library which implements something faster than linear time for the general case?

I would assume the only way to increase speed in lookup is to store the maximum value in some indexing when creating/updating the matrix, which is probably not a good feature in the general case.

Could you either memoize the function (which core.memoize for instance) or implement som indexing that added indexes for such questions when the matrix was stable and to be used for analysis?

mikera commented 6 years ago

Isn't this always going to be linear time?

We could create a protocol to get index-max for a vector, and let people map it over rows of the matrix for the matrix case. That way implementations could provide an optimised way to scan for a max index (or potentially any other criteria)

Hendekagon commented 6 years ago

I had thought that an implementation using something like a priority map might make this constant or log time, but you're right that in the general case memoizing is easier