Skip to content

Conversation

@yuzeh
Copy link

@yuzeh yuzeh commented Aug 4, 2012

Added some code for matrix {row, column} {sum, mean}.

M.sum(dim) returns the sum along dim (0 => sum rows, 1 => sum columns, default 0).
M.mean(dim) returns the mean along dim (same semantics as before).

If dim is not 0 or 1, null is returned.

The return value of M.sum and M.mean are Vector objects.

Unit tests are included.

@nihonjinrxs
Copy link

This looks great -- I'd provide some aliases, as stated in the line comments on your commit. @jcoglan, can we get this added to the mainline repo soon?

@jcoglan
Copy link
Owner

jcoglan commented Aug 12, 2013

I'm not convinced these methods belong in the library since they have little to do with vector/matrix operations. Am I missing a use case? The implementations are also sub-optimal, making internal use of public APIs rather than accessing the elements array directly, and use Array.reduce() which does not exist on some JS platforms (I need to check what support for this is like these days).

@jcoglan
Copy link
Owner

jcoglan commented Aug 12, 2013

Also, I think using numbers to indicate whether you wan rows or columns is a poor interface. The name of the method should indicate what it does rather than relying on magic numbers.

@yuzeh
Copy link
Author

yuzeh commented Sep 13, 2013

Sorry I didn't get to responding this sooner.

With respect to the two comments:

  1. The interface/implementation can obviously be changed.
  2. I wrote this pull request at a point when I was trying to do some machine learning in the browser. I've since given up on that.

One use case: In Markov models, we generally want a matrix M of transition probabilities, where entry M(i, j) is the probability of starting in state i and transitioning to state j in one timestep. Naturally, summing M(i, j) over all j should equal 1 for every i. Sometimes, then, the row sum is used to normalize probabilities.

Anyways, most of this comes from my background in MATLAB, and I have since given up on trying to do anything remotely matrix related in the browser. Let me know if you still want this pull req to go through. If you do, I'll polish it and resend it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants