linearmap-category 0.4.1.0 → 0.4.2.0
raw patch · 8 files changed
+108/−69 lines, 8 filesdep ~free-vector-spacesdep ~template-haskell
Dependency ranges changed: free-vector-spaces, template-haskell
Files
- Math/LinearMap/Asserted.hs +1/−0
- Math/LinearMap/Category/Class.hs +12/−0
- Math/LinearMap/Category/Instances.hs +10/−2
- Math/LinearMap/Category/Instances/Deriving.hs +35/−61
- Math/VectorSpace/Docile.hs +43/−0
- Math/VectorSpace/Dual.hs +2/−2
- linearmap-category.cabal +3/−3
- test/tasty/test.hs +2/−1
Math/LinearMap/Asserted.hs view
@@ -103,6 +103,7 @@ (.+~^) = (^+^) instance VectorSpace w => PseudoAffine (LinearFunction s v w) where f.-~.g = return $ f^-^g+ f.-~!g = f^-^g instance Functor (LinearFunction s v) Coercion Coercion where fmap Coercion = Coercion
Math/LinearMap/Category/Class.hs view
@@ -282,6 +282,7 @@ applyTensorFunctional = biConst0 applyTensorLinMap = biConst0 composeLinear = biConst0+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError -- | The tensor product between one space's dual space and another space is the@@ -773,6 +774,7 @@ >>> \f -> LinearFunction $ \g -> (applyTensorLinMap-+$>f) . arr (asTensor . hasteLinearMap) -+$> g+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError instance ∀ s u v . (TensorSpace u, TensorSpace v, Scalar u ~ s, Scalar v ~ s) => TensorSpace (Tensor s u v) where@@ -865,6 +867,7 @@ ScalarSpaceWitness -> contractTensorMap . fmap transposeTensor . contractMapTensor . fmap (arr (curryLinearMap . hasteLinearMap) . transposeTensor) . arr rassocTensor+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError @@ -1066,6 +1069,7 @@ -+$> coCurryLinearMap $ sampleLinearFunctionFn -+$> exposeLinearFn . curryLinearMap $ f )+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError instance (TensorSpace u, TensorSpace v, s~Scalar u, s~Scalar v)@@ -1096,6 +1100,9 @@ genericTensorspaceError :: a genericTensorspaceError = error "GHC.Generics types can not be used as tensor spaces." +usingNonTupleTypeAsTupleError :: a+usingNonTupleTypeAsTupleError = error "This is not a tuple type, the method should not be callable."+ instance ∀ v s . TensorSpace v => TensorSpace (Gnrx.Rec0 v s) where type TensorProduct (Gnrx.Rec0 v s) w = TensorProduct v w wellDefinedVector = fmap Gnrx.K1 . wellDefinedVector . Gnrx.unK1@@ -1298,6 +1305,7 @@ (applyTensorFunctional-+$>LinearMap f)-+$>pseudoFmapTensorLHS Gnrx.unK1 $ t applyTensorLinMap = bilinearFunction $ \(LinearMap f) t -> (applyTensorLinMap-+$>LinearMap f)-+$>pseudoFmapTensorLHS Gnrx.unK1 $ t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError instance (LinearSpace (f p), Num (Scalar (f p))) => LinearSpace (Gnrx.M1 i c f p) where type DualVector (Gnrx.M1 i c f p) = DualVector (f p)@@ -1313,6 +1321,7 @@ (applyTensorFunctional-+$>LinearMap f)-+$>pseudoFmapTensorLHS Gnrx.unM1 $ t applyTensorLinMap = bilinearFunction $ \(LinearMap f) t -> (applyTensorLinMap-+$>LinearMap f)-+$>pseudoFmapTensorLHS Gnrx.unM1 $ t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError data GenericTupleDual f g p = GenericTupleDual !(DualVector (f p)) !(DualVector (g p)) deriving (Generic)@@ -1492,6 +1501,7 @@ \(LinearMap (fu,fv)) (Tensor (tu,tv)) -> ((applyTensorLinMap -+$> uncurryLinearMap . fmap fromTensor $ fu)-+$>tu) ^+^ ((applyTensorLinMap -+$> uncurryLinearMap . fmap fromTensor $ fv)-+$>tv)+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError newtype GenericNeedle' m@@ -1608,6 +1618,7 @@ applyTensorLinMap = bilinearFunction $ \(LinearMap f) t -> (applyTensorLinMap-+$>LinearMap f) -+$>pseudoFmapTensorLHS getGenericNeedle $ t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError instance ∀ s m . ( Num' s , Semimanifold m@@ -1641,3 +1652,4 @@ DualSpaceWitness -> bilinearFunction $ \(LinearMap f) t -> (applyTensorLinMap-+$>LinearMap f) -+$>pseudoFmapTensorLHS getGenericNeedle' $ t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError
Math/LinearMap/Category/Instances.hs view
@@ -76,6 +76,7 @@ LinearManifoldWitness -> SemimanifoldWitness instance LinearSpace v => PseudoAffine (EmptyMfd v) where p .-~. _ = case p of {}+ p .-~! _ = case p of {} #endif @@ -133,7 +134,8 @@ -> let LinearMap fuw = curryLinearMap $ fℝuw \ in (applyLinear-+$>fuw) -+$> u; \ composeLinear = bilinearFunction $ \f (LinearMap g) \- -> LinearMap $ (applyLinear-+$>f)-+$>g }+ -> LinearMap $ (applyLinear-+$>f)-+$>g; \+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError } LinearScalarSpace(ℝ) LinearScalarSpace(Float)@@ -207,7 +209,8 @@ -> foldl' (^+^) zeroV $ liftA2 (arr fromTensor >>> \ getLinearFunction . getLinearFunction applyLinear) f t; \ composeLinear = bilinearFunction $ \- \f (LinearMap g) -> LinearMap $ fmap ((applyLinear-+$>f)-+$>) g }+ \f (LinearMap g) -> LinearMap $ fmap ((applyLinear-+$>f)-+$>) g; \+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError } FreeLinearSpace( V0 , LinearMap , \(Tensor V0) -> zeroV@@ -399,6 +402,7 @@ applyTensorLinMap = bilinearFunction $ arr curryLinearMap >>> \(LinearMap m) (Tensor t) -> sumV $ zipWith (getLinearFunction . getLinearFunction applyLinear) m t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError instance (Num' n, UArr.Unbox n) => LinearSpace (FinSuppSeq n) where type DualVector (FinSuppSeq n) = Sequence n dualSpaceWitness = case closedScalarWitness :: ClosedScalarWitness n of@@ -421,6 +425,7 @@ applyTensorLinMap = bilinearFunction $ arr curryLinearMap >>> \(LinearMap m) (Tensor t) -> sumV $ zipWith (getLinearFunction . getLinearFunction applyLinear) m t+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError @@ -464,6 +469,7 @@ #endif instance (TensorSpace v, Scalar v ~ s) => PseudoAffine (SymmetricTensor s v) where (.-~!) = (^-^)+ p.-~.q = pure (p^-^q) instance (Num' s, TensorSpace v, Scalar v ~ s) => TensorSpace (SymmetricTensor s v) where type TensorProduct (SymmetricTensor s v) x = Tensor s v (Tensor s v x) wellDefinedVector (SymTensor t) = SymTensor <$> wellDefinedVector t@@ -522,6 +528,7 @@ $ uncurryLinearMap . fmap (uncurryLinearMap . fromTensor . fmap fromTensor) $ LinearMap f) t + useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError @@ -600,6 +607,7 @@ , Monoidal f (LinearFunction (Scalar y)) (LinearFunction (Scalar y)) ) => PseudoAffine (LinearApplicativeSpace f y) where (.-~!) = (.-.)+ p.-~.q = pure (p.-.q)
Math/LinearMap/Category/Instances/Deriving.hs view
@@ -21,7 +21,7 @@ {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE DerivingStrategies #-}-{-# LANGUAGE GeneralisedNewtypeDeriving #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE CPP #-} {-# LANGUAGE TupleSections #-}@@ -109,60 +109,34 @@ return (pure cxt', pure v') exts <- extsEnabled- if not $ TypeFamilies`elem`exts && ScopedTypeVariables`elem`exts- then reportError "This macro requires -XTypeFamilies and -XScopedTypeVariables."+ if not $ all (`elem`exts) [TypeFamilies, ScopedTypeVariables, TypeApplications]+ then reportError "This macro requires -XTypeFamilies, -XScopedTypeVariables and -XTypeApplications." else pure () sequence- [ InstanceD Nothing <$> cxt <*> [t|Semimanifold $v|] <*> do- tySyns <- sequence [-#if MIN_VERSION_template_haskell(2,15,0)- error "The TH type of TySynInstD has changed"-#else- TySynInstD ''Needle <$> do- TySynEqn . (:[]) <$> v <*> v- , TySynInstD ''Interior <$> do- TySynEqn . (:[]) <$> v <*> v-#endif- ]- methods <- [d|+ [ InstanceD Nothing <$> cxt <*> [t|Semimanifold $v|] <*> [d|+ type instance Needle $v = $v+#if !MIN_VERSION_manifolds_core(0,6,0)+ type instance Interior $v = $v $(varP 'toInterior) = pure $(varP 'fromInterior) = id $(varP 'translateP) = Tagged (^+^)- $(varP '(.+~^)) = (^+^) $(varP 'semimanifoldWitness) = SemimanifoldWitness BoundarylessWitness+#endif+ $(varP '(.+~^)) = (^+^) |]- return $ tySyns ++ methods , InstanceD Nothing <$> cxt <*> [t|PseudoAffine $v|] <*> do [d| $(varP '(.-~!)) = (^-^)+ $(varP '(.-~.)) = \p q -> pure (p^-^q) |]- , InstanceD Nothing <$> cxt <*> [t|AffineSpace $v|] <*> do- tySyns <- sequence [-#if MIN_VERSION_template_haskell(2,15,0)- error "The TH type of TySynInstD has changed"-#else- TySynInstD ''Diff <$> do- TySynEqn . (:[]) <$> v <*> v-#endif- ]- methods <- [d|+ , InstanceD Nothing <$> cxt <*> [t|AffineSpace $v|] <*> [d|+ type instance Diff $v = $v $(varP '(.+^)) = (^+^) $(varP '(.-.)) = (^-^) |]- return $ tySyns ++ methods- , InstanceD Nothing <$> cxt <*> [t|TensorSpace $v|] <*> do- tySyns <- sequence [-#if MIN_VERSION_template_haskell(2,15,0)- error "The TH type of TySynInstD has changed"-#else- TySynInstD ''TensorProduct <$> do- wType <- VarT <$> newName "w" :: Q Type- TySynEqn . (:[wType]) <$> v- <*> [t| Basis $v :->: $(pure wType) |]-#endif- ]- methods <- [d|+ , InstanceD Nothing <$> cxt <*> [t|TensorSpace $v|] <*> [d|+ type instance TensorProduct $v w = Basis $v :->: w $(varP 'wellDefinedVector) = \v -> if v==v then Just v else Nothing $(varP 'wellDefinedTensor) = \(Tensor v)@@ -172,7 +146,10 @@ $(varP 'fromFlatTensor) = LinearFunction $ \(Tensor t) -> recompose $ enumerate t $(varP 'scalarSpaceWitness) = ScalarSpaceWitness- $(varP 'linearManifoldWitness) = LinearManifoldWitness BoundarylessWitness+ $(varP 'linearManifoldWitness) = LinearManifoldWitness+#if !MIN_VERSION_manifolds_core(0,6,0)+ BoundarylessWitness+#endif $(varP 'addTensors) = \(Tensor v) (Tensor w) -> Tensor $ (^+^) <$> v <*> w $(varP 'subtractTensors) = \(Tensor v) (Tensor w)@@ -191,24 +168,12 @@ $(varP 'coerceFmapTensorProduct) = \_ Coercion -> error "Cannot yet coerce tensors defined from a `HasBasis` instance. This would require `RoleAnnotations` on `:->:`. Cf. https://gitlab.haskell.org/ghc/ghc/-/issues/8177" |]- return $ tySyns ++ methods , InstanceD Nothing <$> cxt <*> [t|BasisGeneratedSpace $v|] <*> do [d| $(varP 'proveTensorProductIsTrie) = \φ -> φ |]- , InstanceD Nothing <$> cxt <*> [t|LinearSpace $v|] <*> do- tySyns <- sequence [-#if MIN_VERSION_template_haskell(2,15,0)- error "The TH type of TySynInstD has changed"-#else- TySynInstD ''DualVector <$> do- TySynEqn . (:[]) <$> v- <*> [t| DualVectorFromBasis $v |]-#endif- ]- methods <- [d|- - + , InstanceD Nothing <$> cxt <*> [t|LinearSpace $v|] <*> [d|+ type instance DualVector $v = DualVectorFromBasis $v $(varP 'dualSpaceWitness) = case closedScalarWitness @(Scalar $v) of ClosedScalarWitness -> DualSpaceWitness $(varP 'linearId) = LinearMap . trie $ basisValue@@ -249,10 +214,9 @@ -> sumV [ (applyLinear-+$>(LinearMap fi :: u+>w)) -+$> untrie t i | (i, Tensor fi) <- enumerate f ]- $(varP 'useTupleLinearSpaceComponents) = error "Not a tuple type"+ $(varP 'useTupleLinearSpaceComponents) = \_ -> usingNonTupleTypeAsTupleError |]- return $ tySyns ++ methods ] data FiniteDimensionalFromBasisDerivationConfig@@ -285,7 +249,11 @@ tySyns <- sequence [ #if MIN_VERSION_template_haskell(2,15,0)- error "The TH type of TySynInstD has changed"+ DataInstD [] Nothing+ <$> (AppT (ConT ''SubBasis) <$> v)+ <*> pure Nothing+ <*> pure [NormalC subBasisCstr []]+ <*> pure [] #else DataInstD [] ''SubBasis <$> ((:[]) <$> v)@@ -386,12 +354,14 @@ instance AdditiveGroup v => Semimanifold (DualVectorFromBasis v) where type Needle (DualVectorFromBasis v) = DualVectorFromBasis v+#if !MIN_VERSION_manifolds_core(0,6,0) type Interior (DualVectorFromBasis v) = DualVectorFromBasis v toInterior = pure fromInterior = id translateP = Tagged (^+^)- (.+~^) = (^+^) semimanifoldWitness = SemimanifoldWitness BoundarylessWitness+#endif+ (.+~^) = (^+^) instance AdditiveGroup v => AffineSpace (DualVectorFromBasis v) where type Diff (DualVectorFromBasis v) = DualVectorFromBasis v@@ -400,6 +370,7 @@ instance AdditiveGroup v => PseudoAffine (DualVectorFromBasis v) where (.-~!) = (^-^)+ p.-~.q = pure (p^-^q) instance ∀ v . ( HasBasis v, Num' (Scalar v) , Scalar (Scalar v) ~ Scalar v@@ -417,7 +388,10 @@ fromFlatTensor = LinearFunction $ \(Tensor t) -> recompose $ enumerate t scalarSpaceWitness = ScalarSpaceWitness- linearManifoldWitness = LinearManifoldWitness BoundarylessWitness+ linearManifoldWitness = LinearManifoldWitness+#if !MIN_VERSION_manifolds_core(0,6,0)+ BoundarylessWitness+#endif addTensors (Tensor v) (Tensor w) = Tensor $ (^+^) <$> v <*> w subtractTensors (Tensor v) (Tensor w) = Tensor $ (^-^) <$> v <*> w tensorProduct = bilinearFunction@@ -498,7 +472,7 @@ -+$> untrie t i | (i, Tensor fi) <- enumerate f ] )- useTupleLinearSpaceComponents = error "Not a tuple type"+ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError zipWith' :: (a -> b -> c) -> [a] -> [b] -> ([c], [b])
Math/VectorSpace/Docile.hs view
@@ -26,6 +26,7 @@ {-# LANGUAGE EmptyCase #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE TypeApplications #-}+{-# LANGUAGE DataKinds #-} {-# LANGUAGE DefaultSignatures #-} module Math.VectorSpace.Docile where@@ -1212,18 +1213,36 @@ rieszDecomposition _ = [] instance RieszDecomposable (V1 ℝ) where rieszDecomposition m = [(ex, sRiesz $ fmap (LinearFunction (^._x)) $ m)]+#if MIN_VERSION_free_vector_spaces(0,2,0)+ where ex = e @0+#endif instance RieszDecomposable (V2 ℝ) where rieszDecomposition m = [ (ex, sRiesz $ fmap (LinearFunction (^._x)) $ m) , (ey, sRiesz $ fmap (LinearFunction (^._y)) $ m) ]+#if MIN_VERSION_free_vector_spaces(0,2,0)+ where ex = e @0+ ey = e @1+#endif instance RieszDecomposable (V3 ℝ) where rieszDecomposition m = [ (ex, sRiesz $ fmap (LinearFunction (^._x)) $ m) , (ey, sRiesz $ fmap (LinearFunction (^._y)) $ m) , (ez, sRiesz $ fmap (LinearFunction (^._z)) $ m) ]+#if MIN_VERSION_free_vector_spaces(0,2,0)+ where ex = e @0+ ey = e @1+ ez = e @2+#endif instance RieszDecomposable (V4 ℝ) where rieszDecomposition m = [ (ex, sRiesz $ fmap (LinearFunction (^._x)) $ m) , (ey, sRiesz $ fmap (LinearFunction (^._y)) $ m) , (ez, sRiesz $ fmap (LinearFunction (^._z)) $ m) , (ew, sRiesz $ fmap (LinearFunction (^._w)) $ m) ]+#if MIN_VERSION_free_vector_spaces(0,2,0)+ where ex = e @0+ ey = e @1+ ez = e @2+ ew = e @3+#endif infixl 7 .< @@ -1322,19 +1341,43 @@ showsPrecBasis _ = absurd instance TensorDecomposable (V0 ℝ) where tensorDecomposition _ = []+#if MIN_VERSION_free_vector_spaces(0,2,0)+ showsPrecBasis = showsPrec+#else showsPrecBasis _ (Mat.E q) = (V0^.q ++)+#endif instance TensorDecomposable (V1 ℝ) where+#if MIN_VERSION_free_vector_spaces(0,2,0)+ tensorDecomposition (Tensor (V1 w)) = [(e @0, w)]+ showsPrecBasis = showsPrec+#else tensorDecomposition (Tensor (V1 w)) = [(ex, w)] showsPrecBasis _ (Mat.E q) = (V1"ex"^.q ++)+#endif instance TensorDecomposable (V2 ℝ) where+#if MIN_VERSION_free_vector_spaces(0,2,0)+ tensorDecomposition (Tensor (V2 x y)) = [ (e @0, x), (e @1, y) ]+ showsPrecBasis = showsPrec+#else tensorDecomposition (Tensor (V2 x y)) = [ (ex, x), (ey, y) ] showsPrecBasis _ (Mat.E q) = (V2"ex""ey"^.q ++)+#endif instance TensorDecomposable (V3 ℝ) where+#if MIN_VERSION_free_vector_spaces(0,2,0)+ tensorDecomposition (Tensor (V3 x y z)) = [ (e @0, x), (e @1, y), (e @2, z) ]+ showsPrecBasis = showsPrec+#else tensorDecomposition (Tensor (V3 x y z)) = [ (ex, x), (ey, y), (ez, z) ] showsPrecBasis _ (Mat.E q) = (V3"ex""ey""ez"^.q ++)+#endif instance TensorDecomposable (V4 ℝ) where+#if MIN_VERSION_free_vector_spaces(0,2,0)+ tensorDecomposition (Tensor (V4 x y z w)) = [(e @0,x), (e @1,y), (e @2,z), (e @3,w)]+ showsPrecBasis = showsPrec+#else tensorDecomposition (Tensor (V4 x y z w)) = [ (ex, x), (ey, y), (ez, z), (ew, w) ] showsPrecBasis _ (Mat.E q) = (V4"ex""ey""ez""ew"^.q ++)+#endif tensorDecomposeShowsPrec :: ∀ u v s . ( TensorDecomposable u, FiniteDimensional v, Show v, Scalar u ~ s, Scalar v ~ s )
Math/VectorSpace/Dual.hs view
@@ -20,8 +20,8 @@ module Math.VectorSpace.Dual ( Dualness(..), Dual, DualityWitness(..), ValidDualness(..)- , usingAnyDualness- ) where+ , usingAnyDualness, DualnessSingletons(..)+ ) where import Math.LinearMap.Category.Class import Data.Kind (Type)
linearmap-category.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: linearmap-category-version: 0.4.1.0+version: 0.4.2.0 synopsis: Native, complete, matrix-free linear algebra. description: The term /numerical linear algebra/ is often used almost synonymous with /matrix modifications/. However, what's interesting@@ -55,13 +55,13 @@ constrained-categories >=0.3 && <0.5, containers, vector, tagged,- free-vector-spaces >= 0.1.4 && < 0.2,+ free-vector-spaces >= 0.1.4 && < 0.3, linear, lens, transformers, manifolds-core >= 0.5.1.0 && < 0.7, semigroups, hashable, data-default-class, ieee754 >= 0.7 && < 0.9,- call-stack, template-haskell >=2.13 && <2.15,+ call-stack, template-haskell >=2.12 && <2.18, QuickCheck >=2.11 && <2.15 -- hs-source-dirs: default-language: Haskell2010
test/tasty/test.hs view
@@ -13,10 +13,11 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeApplications #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE GeneralisedNewtypeDeriving #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE DerivingStrategies #-} import qualified Prelude as Hask