Skip to content

Instantly share code, notes, and snippets.

@rinx
Last active July 16, 2017 03:28
Show Gist options
  • Save rinx/82146fb7ee464c0d042d56443352a288 to your computer and use it in GitHub Desktop.
Save rinx/82146fb7ee464c0d042d56443352a288 to your computer and use it in GitHub Desktop.
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