Jump to content

Matrix multiplication: Difference between revisions

→‎JS ES6: Updated primitives.
(→‎JS ES6: Updated primitives.)
Line 2,511:
 
// matrixMultiply :: Num a => [[a]] -> [[a]] -> [[a]]
const matrixMultiply = (a, => b) => {
const bColsbt = transpose(b);
return a.map(aRow => bCols.map(bCol => dotProduct(aRow, bCol)));
[11, -4, 5, -3]compose(
}
flip(map)(bt),
dotProduct
[ )
],)(a)
};
 
// dotProduct :: Num a => [[a]] -> [[a]] -> [[a]]
const dotProduct = (xs, ys) => sum(zipWith(product, xs, ys));
compose(sum, zipWith(mul)(xs));
 
// ----------------------- TEST ------------------------
const main = () =>
return JSON.stringify(matrixMultiply(
] [
// [33, 42, 38, -14], [17-1, 741, 724], 44]]
[6, -4, 2],
[-3, 5, 0],
[3, 7, -2]
])([
[-1, 1, 4, 8],
[6, 9, 10, 2],
[11, -4, 5, -3]
]));
 
// ---------------------- GENERIC ----------------------
 
// zipWithcompose (<<<) :: (a -> b -> c) -> [(a] -> [b]) -> a -> [c]
const zipWithcompose = (f, xs, ys...fs) =>
xsfs.length === ys.length ? reduce(
xs.map((xf, ig) => x => f(g(x, ys[i])),
) : undefined; x => x
// TEST );
 
// transposeflip :: [[(a]] -> [[b -> c) -> b -> a]] -> c
const transposeflip = xsf =>
xs[0].map((_, iCol)x => xs.map(rowy => row[iCol]f(y)(x);
 
// length :: [a] -> Int
const length = xs =>
// Returns Infinity over objects without finite
// length. This enables zip and zipWith to choose
// the shorter argument when one is non-finite,
// like cycle, repeat etc
(Array.isArray(xs) || 'string' === typeof xs) ? (
xs.length
) : Infinity;
 
// map :: (a -> b) -> [a] -> [b]
const map = f =>
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => xs.map(f);
 
// mul :: Num a => a -> a -> a
const mul = a =>
b => a * b;
 
// sum :: (Num a) => [a] -> a
Line 2,536 ⟶ 2,577:
xs.reduce((a, x) => a + x, 0);
 
// producttake :: Num a => aInt -> [a] -> [a]
const// producttake =:: (a,Int b) =-> aString *-> b;String
const take = n =>
// The first n elements of a list,
// string of characters, or stream.
xs => xs.slice(0, n);
 
// transpose :: [[a]] -> [[a]]
const transpose = rows =>
// The columns of the input transposed
// into new rows.
// Assumes input rows of even length.
0 < rows.length ? rows[0].map(
(x, i) => rows.flatMap(
x => x[i]
)
) : [];
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
// TEST
const zipWith = f =>
return matrixMultiply(
// A list constructed by zipping with a
[
// custom function, rather [-1,than 1,with 4],the
// default tuple [6, -4, 2],constructor.
xs => ys => [-3, 5, 0],{
[3, 7, -2]const
lng = Math.min(length(xs), length(ys)),
],
vs = take(lng)(ys);
 
[ return take(lng)(xs)
[-1, 1, 4 .map((x, 8i) => f(x)(vs[i],));
[6, 9, 10, 2],};
[11, -4, 5, -3]
]
);
 
// --> [[51,MAIN -8, 26, -18], [-8, -38, -6, 34],
return main();
// [33, 42, 38, -14], [17, 74, 72, 44]]
}))();</lang>
{{Out}}
<lang JavaScriptpre>[[51, -8, 26, -18], [-8, -38, -6, 34], [33,42,38,-14],[17,74,72,44]]</pre>
[33, 42, 38, -14], [17, 74, 72, 44]]</lang>
 
=={{header|jq}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.