Q:

How can I apply a custom scalar function elementwise to a matrix in math.js?

Consider the matrix m:
let m = [ [ 1 , 2 ] , [ 3 , 4 ] ]

Apply the exponentiation function to m:
let mexp = math.exp(m)

Now JSON.stringify(mexp) outputs:
"[[2.718281828459045,7.38905609893065],[20.085536923187668,54.598150033144236]]"

So the built in exponentiation function was applied elementwise to the matrix and the result is a matrix.
Let's say I have a custom scalar function sigmoid:
let sigmoid = x => 1 / ( 1 + Math.exp(-x) )

Now I would like to apply sigmoid elementwise to the matrix as if it was a math.js built in function:
math.sigmoid(m)

How can I implement this?

A:

you could simply use math.map, and customize sigmoid to work with map
math.map(m, sigmoid)

more here http://mathjs.org/docs/reference/functions/map.html

