RosettaCodeData/Task/Matrix-multiplication/AppleScript/matrix-multiplication-1.applescript

150 lines
3 KiB
AppleScript
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
-- matrixMultiply :: Num a => [[a]] -> [[a]] -> [[a]]
2016-12-05 22:15:40 +01:00
to matrixMultiply(a, b)
script rows
property xs : transpose(b)
2017-09-23 10:01:46 +02:00
on |λ|(row)
2016-12-05 22:15:40 +01:00
script columns
2017-09-23 10:01:46 +02:00
on |λ|(col)
my dotProduct(row, col)
end |λ|
2016-12-05 22:15:40 +01:00
end script
map(columns, xs)
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
map(rows, a)
end matrixMultiply
2017-09-23 10:01:46 +02:00
-- TEST -----------------------------------------------------------
2016-12-05 22:15:40 +01:00
on run
matrixMultiply({¬
{-1, 1, 4}, ¬
{6, -4, 2}, ¬
{-3, 5, 0}, ¬
{3, 7, -2} ¬
}, {¬
{-1, 1, 4, 8}, ¬
{6, 9, 10, 2}, ¬
{11, -4, 5, -3}})
--> {{51, -8, 26, -18}, {-8, -38, -6, 34},
-- {33, 42, 38, -14}, {17, 74, 72, 44}}
end run
2017-09-23 10:01:46 +02:00
-- GENERIC FUNCTIONS ----------------------------------------------
2016-12-05 22:15:40 +01:00
-- dotProduct :: [n] -> [n] -> Maybe n
on dotProduct(xs, ys)
2017-09-23 10:01:46 +02:00
script mult
on |λ|(a, b)
2016-12-05 22:15:40 +01:00
a * b
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
if length of xs is not length of ys then
missing value
else
2017-09-23 10:01:46 +02:00
sum(zipWith(mult, xs, ys))
2016-12-05 22:15:40 +01:00
end if
end dotProduct
2017-09-23 10:01:46 +02:00
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
2016-12-05 22:15:40 +01:00
tell mReturn(f)
set v to startValue
set lng to length of xs
2017-09-23 10:01:46 +02:00
repeat with i from lng to 1 by -1
set v to |λ|(v, item i of xs, i, xs)
2016-12-05 22:15:40 +01:00
end repeat
return v
end tell
2017-09-23 10:01:46 +02:00
end foldr
2016-12-05 22:15:40 +01:00
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
2017-09-23 10:01:46 +02:00
set end of lst to |λ|(item i of xs, i, xs)
2016-12-05 22:15:40 +01:00
end repeat
return lst
end tell
end map
2017-09-23 10:01:46 +02:00
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
2016-12-05 22:15:40 +01:00
else
2017-09-23 10:01:46 +02:00
x
2016-12-05 22:15:40 +01:00
end if
2017-09-23 10:01:46 +02:00
end min
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
2016-12-05 22:15:40 +01:00
on mReturn(f)
if class of f is script then
f
else
script
2017-09-23 10:01:46 +02:00
property |λ| : f
2016-12-05 22:15:40 +01:00
end script
end if
end mReturn
2017-09-23 10:01:46 +02:00
-- product :: Num a => [a] -> a
on product(xs)
script mult
on |λ|(a, b)
a * b
end |λ|
end script
foldr(mult, 1, xs)
end product
-- sum :: Num a => [a] -> a
on sum(xs)
script add
on |λ|(a, b)
a + b
end |λ|
end script
foldr(add, 0, xs)
end sum
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
script column
on |λ|(_, iCol)
script row
on |λ|(xs)
item iCol of xs
end |λ|
end script
map(row, xss)
end |λ|
end script
map(column, item 1 of xss)
end transpose
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end zipWith