Last active
August 29, 2015 14:03
-
-
Save ShigekiKarita/342f8bbdde224ead9398 to your computer and use it in GitHub Desktop.
JavaScriptで多次元配列の和、関数型プログラミング?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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