Last active
July 16, 2017 03:28
-
-
Save rinx/82146fb7ee464c0d042d56443352a288 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Vector.Unboxed as VU | |
-- linear algebra | |
-- add | |
(<+>) :: Vector Double -> Vector Double -> Vector Double | |
(<+>) x y = VU.zipWith (+) x y | |
-- sub | |
(<->) :: Vector Double -> Vector Double -> Vector Double | |
(<->) x y = VU.zipWith (-) x y | |
-- dot product | |
(.*) :: Vector Double -> Vector Double -> Double | |
(.*) x y = VU.foldl1 (+) $ VU.zipWith (*) x y | |
-- cross product | |
(/*) :: Vector Double -> Vector Double -> Vector Double | |
(/*) x y = VU.zipWith (-) a' b' | |
where | |
i = fromList [1, 2, 0] | |
z = fromList [0, 0, 0] | |
a = VU.accumulate (*) x $ VU.zip i y | |
b = VU.accumulate (*) y $ VU.zip i x | |
a' = VU.accumulate (+) z $ VU.zip i a | |
b' = VU.accumulate (+) z $ VU.zip i b | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment