Matrix multiplication: Difference between revisions

→‎{{header|AppleScript}}: Normalised argument sequences for map and reduce
m (added a ;Task: (bold) header.)
(→‎{{header|AppleScript}}: Normalised argument sequences for map and reduce)
Line 368:
{{trans|JavaScript}}
 
<lang AppleScript>-- matrixMultiply :: [[n]] -> [[n]] -> [[n]]
-- [[n]] -> [[n]] -> [[n]]
to matrixMultiply(a, b)
script mf
Line 375 ⟶ 374:
to lambdaRow(aRow)
set mf to mf of my closure's mf
map(mf's bCols, mClosure(mf's lambdaCol of mf, {aRow:aRow}), bCols of mf)
end lambdaRow
to lambdaCol(bCol)
dotProduct(aRow of my closure's aRow, bCol)
end lambdaCol
end script
map(a, mClosure(mf's lambdaRow of mf, {mf:mf}), a)
end matrixMultiply
 
 
-- TEST
 
on run
matrixMultiply({¬
{-1, 1, 4}, ¬
Line 405 ⟶ 406:
 
 
-- GENERIC LIBRARY FUNCTIONS
 
-- dotProduct :: [n] -> [n] -> Maybe n
on dotProduct(xs, ys)
script mf
on product(a, b)
a * b
end product
a + b
on add(a, b)
a + b
end add
-- nsum ->:: [n] -> n
on sum(xs)
reduce(xs, add, 0, xs)
end sum
end script
if length of xs is not length of ys then return missing value
sum(zipWith(my product of mf, xs, ys)) of mf
end dotProduct
 
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
script mf
on lambdaCol(_, iCol)
map(mClosure(my closure's xssmf's lambdaRow, ¬{iCol:iCol}), my closure's xss)
mClosure(my closure's mf's lambdaRow, {iCol:iCol}))
end lambdaCol
Line 425 ⟶ 440:
end lambdaRow
end script
map(item 1 of xss, ¬
map(mClosure(mf's lambdaCol, {xss:xss, mf:mf}), item 1 of xss)
end transpose
 
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to length of xs
Line 442 ⟶ 457:
end zipWith
 
-- map :: [a] -> (a -> b) -> [b]
on map(xsf, fxs)
set mf to mReturn(f)
set lng to length of xs
Line 453 ⟶ 468:
end map
 
-- [a]reduce :: -> (a -> b) -> b -> [a] -> [b]
on reduce(xs, f, startValue, xs)
set mf to mReturn(f)
Line 464 ⟶ 479:
return v
end reduce
 
-- n -> n -> n
on product(a, b)
a * b
end product
 
-- n -> n -> n
on add(a, b)
a + b
end add
 
-- [n] -> n
on sum(xs)
reduce(xs, add, 0)
end sum
 
 
9,659

edits