0

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?

sbtpr
  • 541
  • 5
  • 18

1 Answers1

2

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

Abderrahmane TAHRI JOUTI
  • 3,514
  • 2
  • 26
  • 43