linear 1.16.2 → 1.16.4
raw patch · 12 files changed
+297/−19 lines, 12 files
Files
- CHANGELOG.markdown +12/−0
- linear.cabal +1/−1
- src/Linear/Matrix.hs +3/−0
- src/Linear/Plucker.hs +38/−0
- src/Linear/Projection.hs +2/−2
- src/Linear/V.hs +38/−0
- src/Linear/V0.hs +38/−0
- src/Linear/V1.hs +38/−0
- src/Linear/V2.hs +38/−0
- src/Linear/V3.hs +38/−0
- src/Linear/V4.hs +38/−0
- src/Linear/Vector.hs +13/−16
CHANGELOG.markdown view
@@ -1,3 +1,15 @@+1.16.4+------+* `ortho` and `inverseOrtho` now only require a `Fractional` constraint.+* Added missing `Floating` instances.++1.16.3+----+* Improve the performance of `fromQuaternion`, `mkTransformation`,+ `mkTransformationMat`, `basisFor`, `scaled` by using implementations+ that inline well for functions that were previously reference+ implementations.+ 1.16.2 ---- * Added `NFData` instances for the various vector types.
linear.cabal view
@@ -1,6 +1,6 @@ name: linear category: Math, Algebra-version: 1.16.2+version: 1.16.4 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE
src/Linear/Matrix.hs view
@@ -187,6 +187,7 @@ where x2 = x * x y2 = y * y z2 = z * z+{-# INLINE fromQuaternion #-} -- | Build a transformation matrix from a rotation matrix and a -- translation vector.@@ -194,11 +195,13 @@ mkTransformationMat (V3 r1 r2 r3) (V3 tx ty tz) = V4 (snoc3 r1 tx) (snoc3 r2 ty) (snoc3 r3 tz) (V4 0 0 0 1) where snoc3 (V3 x y z) = V4 x y z+{-# INLINE mkTransformationMat #-} -- |Build a transformation matrix from a rotation expressed as a -- 'Quaternion' and a translation vector. mkTransformation :: Num a => Quaternion a -> V3 a -> M44 a mkTransformation = mkTransformationMat . fromQuaternion+{-# INLINE mkTransformation #-} -- | Convert from a 4x3 matrix to a 4x4 matrix, extending it with the @[ 0 0 0 1 ]@ column vector m43_to_m44 :: Num a => M43 a -> M44 a
src/Linear/Plucker.hs view
@@ -222,6 +222,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance Floating a => Floating (Plucker a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ (**) = liftA2 (**)+ {-# INLINE (**) #-}+ logBase = liftA2 logBase+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Hashable a => Hashable (Plucker a) where hashWithSalt s (Plucker a b c d e f) = s `hashWithSalt` a `hashWithSalt` b `hashWithSalt` c `hashWithSalt` d `hashWithSalt` e `hashWithSalt` f {-# INLINE hashWithSalt #-}
src/Linear/Projection.hs view
@@ -186,7 +186,7 @@ -- | Build an orthographic perspective matrix from 6 clipping planes ortho- :: Floating a+ :: Fractional a => a -- ^ Left -> a -- ^ Right -> a -- ^ Bottom@@ -205,7 +205,7 @@ -- | Build an inverse orthographic perspective matrix from 6 clipping planes inverseOrtho- :: Floating a+ :: Fractional a => a -- ^ Left -> a -- ^ Right -> a -- ^ Bottom
src/Linear/V.hs view
@@ -207,6 +207,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance (Dim n, Floating a) => Floating (V n a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ V as ** V bs = V $ V.zipWith (**) as bs+ {-# INLINE (**) #-}+ logBase (V as) (V bs) = V $ V.zipWith logBase as bs+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Dim n => Distributive (V n) where distribute f = V $ V.generate (reflectDim (Proxy :: Proxy n)) $ \i -> fmap (\(V v) -> unsafeIndex v i) f {-# INLINE distribute #-}
src/Linear/V0.hs view
@@ -141,6 +141,44 @@ fromRational _ = V0 {-# INLINE fromRational #-} +instance Floating (V0 a) where+ pi = V0+ {-# INLINE pi #-}+ exp V0 = V0+ {-# INLINE exp #-}+ sqrt V0 = V0+ {-# INLINE sqrt #-}+ log V0 = V0+ {-# INLINE log #-}+ V0 ** V0 = V0+ {-# INLINE (**) #-}+ logBase V0 V0 = V0+ {-# INLINE logBase #-}+ sin V0 = V0+ {-# INLINE sin #-}+ tan V0 = V0+ {-# INLINE tan #-}+ cos V0 = V0+ {-# INLINE cos #-}+ asin V0 = V0+ {-# INLINE asin #-}+ atan V0 = V0+ {-# INLINE atan #-}+ acos V0 = V0+ {-# INLINE acos #-}+ sinh V0 = V0+ {-# INLINE sinh #-}+ tanh V0 = V0+ {-# INLINE tanh #-}+ cosh V0 = V0+ {-# INLINE cosh #-}+ asinh V0 = V0+ {-# INLINE asinh #-}+ atanh V0 = V0+ {-# INLINE atanh #-}+ acosh V0 = V0+ {-# INLINE acosh #-}+ instance Metric V0 where dot V0 V0 = 0 {-# INLINE dot #-}
src/Linear/V1.hs view
@@ -159,6 +159,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance Floating a => Floating (V1 a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ (**) = liftA2 (**)+ {-# INLINE (**) #-}+ logBase = liftA2 logBase+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Hashable a => Hashable (V1 a) where #if (MIN_VERSION_hashable(1,2,1)) || !(MIN_VERSION_hashable(1,2,0)) hash (V1 a) = hash a
src/Linear/V2.hs view
@@ -172,6 +172,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance Floating a => Floating (V2 a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ (**) = liftA2 (**)+ {-# INLINE (**) #-}+ logBase = liftA2 logBase+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Metric V2 where dot (V2 a b) (V2 c d) = a * c + b * d {-# INLINE dot #-}
src/Linear/V3.hs view
@@ -156,6 +156,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance Floating a => Floating (V3 a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ (**) = liftA2 (**)+ {-# INLINE (**) #-}+ logBase = liftA2 logBase+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Hashable a => Hashable (V3 a) where hashWithSalt s (V3 a b c) = s `hashWithSalt` a `hashWithSalt` b `hashWithSalt` c {-# INLINE hashWithSalt #-}
src/Linear/V4.hs view
@@ -165,6 +165,44 @@ fromRational = pure . fromRational {-# INLINE fromRational #-} +instance Floating a => Floating (V4 a) where+ pi = pure pi+ {-# INLINE pi #-}+ exp = fmap exp+ {-# INLINE exp #-}+ sqrt = fmap sqrt+ {-# INLINE sqrt #-}+ log = fmap log+ {-# INLINE log #-}+ (**) = liftA2 (**)+ {-# INLINE (**) #-}+ logBase = liftA2 logBase+ {-# INLINE logBase #-}+ sin = fmap sin+ {-# INLINE sin #-}+ tan = fmap tan+ {-# INLINE tan #-}+ cos = fmap cos+ {-# INLINE cos #-}+ asin = fmap asin+ {-# INLINE asin #-}+ atan = fmap atan+ {-# INLINE atan #-}+ acos = fmap acos+ {-# INLINE acos #-}+ sinh = fmap sinh+ {-# INLINE sinh #-}+ tanh = fmap tanh+ {-# INLINE tanh #-}+ cosh = fmap cosh+ {-# INLINE cosh #-}+ asinh = fmap asinh+ {-# INLINE asinh #-}+ atanh = fmap atanh+ {-# INLINE atanh #-}+ acosh = fmap acosh+ {-# INLINE acosh #-}+ instance Metric V4 where dot (V4 a b c d) (V4 e f g h) = a * e + b * f + c * g + d * h {-# INLINE dot #-}
src/Linear/Vector.hs view
@@ -42,7 +42,6 @@ import Data.IntMap as IntMap import Data.Map as Map import Data.Monoid (mempty)-import Data.Traversable (mapAccumL) import Data.Vector as Vector import Data.Vector.Mutable as Mutable #ifdef USE_GHC_GENERICS@@ -50,6 +49,8 @@ #endif import Linear.Instances () +{-# ANN module "HLint: ignore Redundant lambda" #-}+ -- $setup -- >>> import Linear.V2 @@ -366,14 +367,6 @@ f ^/ a = fmap (/a) f {-# INLINE (^/) #-} --- `SetOne` builds all combinations of the filler with one value from the choices list.-data SetOne a = SetOne { _filler :: !a, choices :: [a] }-instance Functor SetOne where- fmap f (SetOne a os) = SetOne (f a) (fmap f os)-instance Applicative SetOne where- pure a = SetOne a []- SetOne f fs <*> SetOne a as = SetOne (f a) (Prelude.foldr ((:) . ($ a)) (Prelude.map f as) fs)- -- | Produce a default basis for a vector space. If the dimensionality -- of the vector space is not statically known, see 'basisFor'. basis :: (Additive t, Traversable t, Num a) => [t a]@@ -382,14 +375,23 @@ -- | Produce a default basis for a vector space from which the -- argument is drawn. basisFor :: (Traversable t, Num a) => t b -> [t a]-basisFor = choices . traverse (\_ -> SetOne 0 [1])+basisFor = \t ->+ ifoldMapOf traversed ?? t $ \i _ ->+ return $+ iover traversed ?? t $ \j _ ->+ if i == j then 1 else 0+{-# INLINABLE basisFor #-} -- | Produce a diagonal (scale) matrix from a vector. -- -- >>> scaled (V2 2 3) -- V2 (V2 2 0) (V2 0 3) scaled :: (Traversable t, Num a) => t a -> t (t a)-scaled v = fillFromList (choices $ traverse (\a -> SetOne 0 [a]) v) v+scaled = \t -> iter t (\i x -> iter t (\j _ -> if i == j then x else 0))+ where+ iter :: Traversable t => t a -> (Int -> a -> b) -> t b+ iter x f = iover traversed f x+{-# INLINE scaled #-} -- | Create a unit vector. --@@ -397,11 +399,6 @@ -- V2 1 0 unit :: (Additive t, Num a) => ASetter' (t a) a -> t a unit l = set' l 1 zero--fillFromList :: Traversable t => [a] -> t b -> t a-fillFromList l = snd . mapAccumL aux l- where aux (a:as) _ = (as, a)- aux [] _ = error "too few elements in takeFromList" -- | Outer (tensor) product of two vectors outer :: (Functor f, Functor g, Num a) => f a -> g a -> f (g a)