vectorz icon indicating copy to clipboard operation
vectorz copied to clipboard

Max/Min functions for matrices

Open malloc82 opened this issue 10 years ago • 6 comments

Added functions for Matrix class that has similar behavior as in Matlab's max/min when applied to matrices: http://www.mathworks.com/help/matlab/ref/max.html

malloc82 avatar Jun 24 '15 04:06 malloc82

Hi @malloc82 , very happy to consider this, though I think the implementation still needs a bit of work

Specific comments:

  • If we want to implement these functions, I think they should implemented at the class AMatrix so that they work for all matrix types. Can be overriden for Matrix etc.
  • Shouldn't these return vectors rather than matrices?
  • You may find that getRow(i).elementMin() is an effective way of computing the row minimum. This has the advantage of working for any type of AMatrix, and avoids duplicating a lot of fiddly loop code
  • Please provide tests along with new functions like this

mikera avatar Jun 24 '15 11:06 mikera

Hi Mike, I really like your work, and I found myself using it more and more with Clojure. I think I might be able to contribute some here. Probably start from small stuffs. As to your comments:

  • I think the result of these functions should reflect the dimension of the data and query type, as in, rowMax should return a column vector, colMax should return a row vector. This is also the behavior of corresponding Matlab functions. From what I see so far there is no way to distinguish between a row vector or a column vector. So I decided to return a matrix type instead.
  • I agree the duplicated loop code is not too great, but instead of using getRow(i).elementMin() I'm thinking about either modify existing one or add additional function like this:
    public static final double elementMin(double[] data, int offset, int length, int step) {
        double result = data[offset];
        for (int i=1; i<length; i+=step) {
            double v=data[offset+i];
            if (v<result) result=v;
        }
        return result;
    }

in DoubleArray class. So basically passing a parameter to let i increment by step. This way it can be utilized in old code and as well as in those column max/min functions. For safety, we can just add them as new functions for now since there is an additional parameter, which do make it a new function. What do you think about this?

  • I'll make sure I implement this on AMatrix level with test cases as well.

malloc82 avatar Jun 26 '15 05:06 malloc82

Well... in both Vectorz and core.matrix there isn't a distinction between "row vector" and "column vector". From a logical perspective this is analogous with 1D arrays in programming languages.

I've debated this with various experts and the consensus we have come to is that this is better than the approach used in MATLAB (which basically treats everything as a matrix, so your only option is to use "row vectors" and "column vectors").

There is some documentation / discussion on the topic here:

https://github.com/mikera/core.matrix/wiki/Vectors-vs.-matrices

mikera avatar Jun 26 '15 08:06 mikera

In theory I like the idea of an static elementMin with step function. It could be re-used in some other places as well, e.g. StridedVector and StridedMatrix

mikera avatar Jun 26 '15 08:06 mikera

@malloc82 Are you still working on this? Or should I close?

mikera avatar Dec 28 '15 09:12 mikera

Ah, sorry, for the delay. I'll try to push the change in the next week or so, been quite busy almost forgot about it.

malloc82 avatar Dec 28 '15 18:12 malloc82