【问题标题】:How can I apply a custom scalar function elementwise to a matrix in math.js?如何将自定义标量函数元素应用于 math.js 中的矩阵?
【发布时间】:2017-12-25 09:35:19
【问题描述】:

考虑矩阵m:

let m = [ [ 1 , 2 ] , [ 3 , 4 ] ]

对 m 应用幂函数:

let mexp = math.exp(m)

现在JSON.stringify(mexp) 输出:

"[[2.718281828459045,7.38905609893065],[20.085536923187668,54.598150033144236]]"

因此,内置的求幂函数按元素应用于矩阵,结果是一个矩阵。

假设我有一个自定义标量函数sigmoid

let sigmoid = x => 1 / ( 1 + Math.exp(-x) )

现在我想将 sigmoid elementwise 应用于矩阵,就好像它是内置函数的 math.js 一样:

math.sigmoid(m)

我该如何实现?

【问题讨论】:

    标签: javascript function matrix elementwise-operations mathjs


    【解决方案1】:

    您可以简单地使用 math.map,并自定义 sigmoid 以使用 map

    math.map(m, sigmoid)
    

    更多http://mathjs.org/docs/reference/functions/map.html

    【讨论】:

    • 非常感谢。我使用了 math.map(m,sigmoid) 并且成功了。
    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 2021-06-20
    • 2018-10-09
    • 2022-12-12
    • 1970-01-01
    • 2017-03-11
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多