linear 0.4.1 → 0.4.2.1
raw patch · 2 files changed
+64/−4 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Linear.Matrix: (!!*) :: (Functor m, Functor r, Num a) => m (r a) -> a -> m (r a)
+ Linear.Matrix: det22 :: Num a => V2 (V2 a) -> a
+ Linear.Matrix: det33 :: Num a => V3 (V3 a) -> a
+ Linear.Matrix: inv22 :: (Epsilon a, Floating a) => M22 a -> Maybe (M22 a)
+ Linear.Matrix: inv33 :: (Epsilon a, Floating a) => M33 a -> Maybe (M33 a)
+ Linear.Matrix: type M22 a = V2 (V2 a)
Files
- linear.cabal +1/−1
- src/Linear/Matrix.hs +63/−3
linear.cabal view
@@ -1,6 +1,6 @@ name: linear category: Math, Algebra-version: 0.4.1+version: 0.4.2.1 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE
src/Linear/Matrix.hs view
@@ -1,7 +1,8 @@ module Linear.Matrix- ( (!*!), (!*) , (*!)+ ( (!*!), (!*) , (*!), (!!*) , adjoint- , M33, M44, M43, m33_to_m44, m43_to_m44+ , M22, M33, M44, M43, m33_to_m44, m43_to_m44+ , det22, det33, inv22, inv33 , eye3, eye4 , trace , translation@@ -13,10 +14,13 @@ import Control.Lens import Data.Distributive import Data.Foldable as Foldable+import Linear.Epsilon+import Linear.Metric import Linear.Quaternion+import Linear.V2 import Linear.V3 import Linear.V4-import Linear.Metric+import Linear.Vector ((*^)) import Linear.Conjugate infixl 7 !*!@@ -35,6 +39,19 @@ (*!) :: (Metric r, Distributive n, Num a) => r a -> r (n a) -> n a f *! g = dot f <$> distribute g +infixl 7 *!!+-- |Scalar-matrix product.+(*!!) :: (Functor m, Functor r, Num a) => a -> m (r a) -> m (r a)+s *!! m = fmap (s *^) m+{-# INLINE (*!!) #-}++infixl 7 !!*+-- |Matrix-scalar product.+(!!*) :: (Functor m, Functor r, Num a) => m (r a) -> a -> m (r a)+(!!*) = flip (*!!)+{-# INLINE (!!*) #-}++ -- | hermitian conjugate or conjugate transpose adjoint :: (Functor m, Distributive n, Conjugate a) => m (n a) -> n (m a) adjoint = collect (fmap conjugate)@@ -46,6 +63,7 @@ {-# INLINE trace #-} -- | Matrices use a row-major representation.+type M22 a = V2 (V2 a) type M33 a = V3 (V3 a) type M44 a = V4 (V4 a) type M43 a = V4 (V3 a)@@ -96,3 +114,45 @@ -- column) from a 3x4 or 4x4 matrix translation :: (R3 t, R4 v, Functor f, Functor t) => (V3 a -> f (V3 a)) -> t (v a) -> f (t a) translation = (. fmap (^._w)) . _xyz++-- |2x2 matrix determinant.+det22 :: Num a => V2 (V2 a) -> a+det22 (V2 (V2 a b) (V2 c d)) = a * d - b * c+{-# INLINE det22 #-}++-- |3x3 matrix determinant.+det33 :: Num a => V3 (V3 a) -> a+det33 (V3 (V3 a b c)+ (V3 d e f)+ (V3 g h i)) = a * (e*i-f*h) - d * (b*i-c*h) + g * (b*f-c*e)+{-# INLINE det33 #-}++-- |2x2 matrix inverse.+inv22 :: (Epsilon a, Floating a) => M22 a -> Maybe (M22 a)+inv22 m@(V2 (V2 a b) (V2 c d))+ | nearZero det = Nothing+ | otherwise = Just $ (1 / det) *!! (V2 (V2 d (-b)) (V2 (-c) a))+ where det = det22 m+{-# INLINE inv22 #-}++-- |3x3 matrix inverse.+inv33 :: (Epsilon a, Floating a) => M33 a -> Maybe (M33 a)+inv33 m@(V3 (V3 a b c)+ (V3 d e f)+ (V3 g h i))+ | nearZero det = Nothing+ | otherwise = Just $ (1 / det) *!! (V3 (V3 a' b' c')+ (V3 d' e' f')+ (V3 g' h' i'))+ where a' = cofactor (e,f,h,i)+ b' = cofactor (c,b,i,h)+ c' = cofactor (b,c,e,f)+ d' = cofactor (f,d,i,g)+ e' = cofactor (a,c,g,i)+ f' = cofactor (c,a,f,d)+ g' = cofactor (d,e,g,h)+ h' = cofactor (b,a,h,g)+ i' = cofactor (a,b,d,e)+ cofactor (q,r,s,t) = det22 (V2 (V2 q r) (V2 s t))+ det = det33 m+{-# INLINE inv33 #-}