Matrix multiplication: Difference between revisions
Content deleted Content added
Line 358: | Line 358: | ||
exception index error: |
exception index error: |
||
putf(stand error, $x"Exception: index error."l$) |
putf(stand error, $x"Exception: index error."l$) |
||
=={{header|AppleScript}}== |
|||
{{trans|JavaScript}} |
|||
<lang AppleScript> |
|||
-- [[n]] -> [[n]] -> [[n]] |
|||
to matrixMultiply(a, b) |
|||
script mf |
|||
property bCols : transpose(b) |
|||
to lambdaRow(aRow) |
|||
set mf to my closure's mf |
|||
{concatMap(mf's bCols, mClosure(mf's lambdaCol, {aRow:aRow}))} |
|||
end lambdaRow |
|||
to lambdaCol(bCol) |
|||
{dotProduct(my closure's aRow, bCol)} |
|||
end lambdaCol |
|||
end script |
|||
concatMap(a, mClosure(mf's lambdaRow, {mf:mf})) |
|||
end matrixMultiply |
|||
on run |
|||
matrixMultiply({¬ |
|||
{1, 2}, ¬ |
|||
{3, 4} ¬ |
|||
}, {¬ |
|||
{-3, -8, 3}, ¬ |
|||
{-2, 1, 4}}) |
|||
--> {{-7, -6, 11}, {-17, -20, 25}} |
|||
end run |
|||
-- [n] -> [n] -> Maybe n |
|||
on dotProduct(xs, ys) |
|||
if length of xs is not length of ys then return missing value |
|||
sum(zipWith(my product, xs, ys)) |
|||
end dotProduct |
|||
-- [[a]] -> [[a]] |
|||
on transpose(xss) |
|||
script mf |
|||
on lambdaCol(_, iCol) |
|||
map(my closure's xss, ¬ |
|||
mClosure(my closure's mf's lambdaRow, {iCol:iCol})) |
|||
end lambdaCol |
|||
on lambdaRow(row) |
|||
item (my closure's iCol) of row |
|||
end lambdaRow |
|||
end script |
|||
map(item 1 of xss, ¬ |
|||
mClosure(mf's lambdaCol, {xss:xss, mf:mf})) |
|||
end transpose |
|||
-- LIBRARY FUNCTIONS |
|||
-- (a -> b -> c) -> [a] -> [b] -> [c] |
|||
on zipWith(f, xs, ys) |
|||
set lng to length of xs |
|||
if lng is not length of ys then return missing value |
|||
set mf to mReturn(f) |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to mf's lambda(item i of xs, item i of ys) |
|||
end repeat |
|||
return lst |
|||
end zipWith |
|||
-- [a] -> (a -> b) -> [b] |
|||
on map(xs, f) |
|||
set mf to mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to mf's lambda(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end map |
|||
-- [a] -> (a -> b) -> b -> [b] |
|||
on reduce(xs, f, startValue) |
|||
set mf to mReturn(f) |
|||
set v to startValue |
|||
set lng to length of xs |
|||
repeat with i from 1 to lng |
|||
set v to mf's lambda(v, item i of xs, i, xs) |
|||
end repeat |
|||
return v |
|||
end reduce |
|||
on concat(a, b) |
|||
a & b |
|||
end concat |
|||
on concatMap(xs, f) |
|||
reduce(map(xs, f), concat, {}) |
|||
end concatMap |
|||
-- 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 |
|||
-- Script | Handler -> Script |
|||
on mReturn(f) |
|||
if class of f is script then |
|||
f |
|||
else |
|||
script |
|||
property lambda : f |
|||
end script |
|||
end if |
|||
end mReturn |
|||
-- Handler -> Record -> Script |
|||
on mClosure(f, recBindings) |
|||
script |
|||
property closure : recBindings |
|||
property lambda : f |
|||
end script |
|||
end mClosure</lang> |
|||
{{Out}} |
|||
<pre>{{-7, -6, 11}, {-17, -20, 25}}</pre> |
|||
=={{header|APL}}== |
=={{header|APL}}== |