RosettaCodeData/Task/Matrix-exponentiation-operator/Haskell/matrix-exponentiation-operator.hs

33 lines
744 B
Haskell
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
import Data.List (transpose)
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
(<+>)
:: Num a
=> [a] -> [a] -> [a]
(<+>) = zipWith (+)
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
(<*>)
:: Num a
=> [a] -> [a] -> a
(<*>) = (sum .) . zipWith (*)
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
newtype Mat a =
Mat [[a]]
deriving (Eq, Show)
instance Num a =>
Num (Mat a) where
2013-04-10 21:29:02 -07:00
negate (Mat x) = Mat $ map (map negate) x
2017-09-23 10:01:46 +02:00
Mat x + Mat y = Mat $ zipWith (<+>) x y
Mat x * Mat y =
Mat
[ [ xs Main.<*> ys -- Main prefix to distinguish fron applicative operator
| ys <- transpose y ]
| xs <- x ]
abs = undefined
2013-04-10 21:29:02 -07:00
fromInteger _ = undefined -- don't know dimension of the desired matrix
2017-09-23 10:01:46 +02:00
signum = undefined
-- TEST ----------------------------------------------------------------------
main :: IO ()
main = print $ Mat [[1, 2], [0, 1]] ^ 4