numbers / numbers.js

Advanced Mathematics Library for Node.js and JavaScript
Apache License 2.0
1.77k stars 167 forks source link

fix #140, add matrix.map #147

Closed Dakkers closed 9 years ago

Dakkers commented 9 years ago

140 wasn't broken, per se, but an in-place operation is deceiving. as mentioned in that issue, it would be nice to have a separate function for an in-place operation, in case it's needed. perhaps a suffix of IP would be suitable.

matrix.map takes a matrix (or vector) and applies a function to every element to a copy of the matrix (not in-place!). the purpose behind this function is to imitate how some of Numpy's functions operate element-by-element on Numpy arrays. so instead of writing matrix.sin, matrix.cos... etc. the user can just pass any function to matrix.map.

the catch: the function passed can only take one parameter, so it would be nice (and pretty simple, methinks) to create something that would behave like so:

f = function(x, a, b) {
  return a*x + b;
}
var m = [[1,2,3],[4,5,6]];

var m2 = matrix.mapNEW(m, f, a, b);

so that any additional parameters passed to matrix.mapNEW (name is a work in progress) are passed to f too.

philipithomas commented 9 years ago

Shouldn't this be called a "map" function?

Dakkers commented 9 years ago

Yes you're correct. I was struggling to come up with a name. Will fix in a bit On Jun 6, 2015 5:51 PM, "Philip I. Thomas" notifications@github.com wrote:

Shouldn't this be called a "map" function?

— Reply to this email directly or view it on GitHub https://github.com/numbers/numbers.js/pull/147#issuecomment-109653311.

Dakkers commented 9 years ago

@KartikTalwar mind reviewing this? should only take a minute :hamburger:

KartikTalwar commented 9 years ago

Mostly lgtm :hamburger:

Dakkers commented 9 years ago

looking at some benchmarks, doing a direct call is faster. I think doing this for a single argument is okay, but if we want to generalize it we can use apply.

KartikTalwar commented 9 years ago

Cool, feel free to merge

On Sunday, June 7, 2015, Dakota St. Laurent notifications@github.com wrote:

looking at some benchmarks https://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/6, doing a direct call is faster. I think doing this for a single argument is okay, but if we want to generalize it we can use apply.

— Reply to this email directly or view it on GitHub https://github.com/numbers/numbers.js/pull/147#issuecomment-109809159.

Dakkers commented 9 years ago

anyone have a suggestion for the name of the second version of matrix.map?