Skip to content

Instantly share code, notes, and snippets.

@ShigekiKarita
Last active August 29, 2015 14:03
Show Gist options
  • Save ShigekiKarita/342f8bbdde224ead9398 to your computer and use it in GitHub Desktop.
Save ShigekiKarita/342f8bbdde224ead9398 to your computer and use it in GitHub Desktop.
JavaScriptで多次元配列の和、関数型プログラミング?
function isNumber(value)
{
return typeof value == 'number';
}
function tensorAdd(lhs, rhs)
{
return lhs.map
(
function(value, index)
{
if (isNumber(value))
{
return lhs[index] + rhs[index];
}
else
{
return tensorAdd(lhs[index], rhs[index]); // 再帰
}
}
);
}
function tensorSum() // 可変長引数
{
return [].reduce.call(arguments, tensorAdd);
}
const sigma0 = [[0.006, 0.003],
[0.003, 0.003]];
const sigma1 = [[0.001, 0.002],
[0.002, 0.005]];
const sigma = tensorSum(sigma0, sigma1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment