Closed wojdyr closed 6 years ago
Extending the fold
functions over matrices shouldn't be too hard.
argmax()
is a trickier case. I've painted myself into a bit of a consistency corner. maxelem(a)
is defined as a[argmax(a)]
for vectors, there's a nice symmetry between argmax
returning an int
and operator[]
taking an int
. argmax
on a matrix COULD return the index of the "greatest" column, as linalg::vec
does model LessThanComparable
, but this seems a little nonsensical and not very useful. A matrix is semantically quite a bit different from a sequence of vectors.
argmax
could return the index of the greatest element as though the matrix were a flat array in column major order. I do define a few other operations on matrices (comparison ops, etc.) as though they were a flat sequence of numbers, but I don't have any sort of function for "flat indexing" into a linalg::mat
.
I suppose argmax
could return an int2
, and I could add an overload of operator[]
which accepts an int2
. The only remaining question is whether mat[{x,y}]
should be equal to mat[x][y]
(less confusing) or mat[y][x]
which would match the sort of "row,column" subscripting you see in math textbooks.
I'll do the fold
functions right away, and I welcome your input on what would be most logical for something like argmax
.
I'm going to close this issue for now, as the obvious improvements were made and there was no further input on the argmax
semantics.
I just tried to call
argmax()
with a matrix as an argument and realized that the reductions here work only for vectors.So here is a feature request to consider: functions such as
sum()
andargmax()
for matrices.