Kronecker product: Difference between revisions
Content deleted Content added
Add Swift |
→JS ES6: Updated primitives, layout. |
||
Line 1,766: | Line 1,766: | ||
'use strict'; |
'use strict'; |
||
// |
// ---------KRONECKER PRODUCT OF TWO MATRICES---------- |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
flip(compose(map, map, mul))(ys) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
const main = () => |
|||
unlines(map(compose(unlines, map(show)))([ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
|||
const compose = (...fs) => |
|||
x => fs.reduceRight((a, f) => f(a), x); |
|||
// concat :: [[a]] -> [a] |
// concat :: [[a]] -> [a] |
||
Line 1,772: | Line 1,810: | ||
// concatMap :: (a -> [b]) -> [a] -> [b] |
// concatMap :: (a -> [b]) -> [a] -> [b] |
||
const concatMap = |
const concatMap = f => |
||
xs => xs.flatMap(f); |
|||
// |
// flip :: (a -> b -> c) -> b -> a -> c |
||
const flip = f => |
|||
x => y => f(y)(x); |
|||
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) : |
|||
⚫ | |||
return go(xs.concat([].slice.apply(arguments))); |
|||
⚫ | |||
return go([].slice.call(args, 1)); |
|||
⚫ | |||
// map :: (a -> b) -> [a] -> [b] |
// map :: (a -> b) -> [a] -> [b] |
||
const map = |
const map = f => xs => xs.map(f); |
||
⚫ | |||
⚫ | |||
// show :: a -> String |
// show :: a -> String |
||
Line 1,797: | Line 1,833: | ||
const unlines = xs => xs.join('\n'); |
const unlines = xs => xs.join('\n'); |
||
// MAIN --- |
|||
console.log( |
|||
⚫ | |||
main() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// (* n) mapped over each element in a matrix |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return unlines(map(rows => unlines(map(show, rows)), [ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |