From c8645c9791d9af1724714f897782a0ca3183160b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justus=20Sagem=C3=BCller?= Date: Thu, 22 May 2025 13:51:08 +0200 Subject: [PATCH 1/3] Remove the unnecessary `LV` argument to the `FreeLinearSpace` macro. This argument formerly distinguished between different constructors of the LinearMaps over the V0...V4 spaces, but this is now using the same constructor (in a nested fashion) of the space itself, with the consequence that all macro invocations passed the same `LinearMap` wrapper-constructor. This can as well be inlined in the macro. --- Math/LinearMap/Category/Instances.hs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Math/LinearMap/Category/Instances.hs b/Math/LinearMap/Category/Instances.hs index 4aacbf5..a52d3bf 100644 --- a/Math/LinearMap/Category/Instances.hs +++ b/Math/LinearMap/Category/Instances.hs @@ -194,7 +194,7 @@ tensorUnsafeWriteArrayWithOffsetViaList v2l ar i (Tensor t) toInterior = pure; fromInterior = id; translateP = Tagged (^+^); #endif -#define FreeLinearSpace( V, d, LV, tp \ +#define FreeLinearSpace( V, d, tp \ , tenspl, tenid, dspan \ , contraction, contraaction \ , frls, tols ) \ @@ -246,7 +246,7 @@ instance ∀ s . (Num' s, Eq s) => LinearSpace (V s) where { \ type DualVector (V s) = V s; \ dualSpaceWitness = case closedScalarWitness :: ClosedScalarWitness s of \ {ClosedScalarWitness -> DualSpaceWitness}; \ - linearId = LV Mat.identity; \ + linearId = LinearMap Mat.identity; \ idTensor = Tensor Mat.identity; \ tensorId = ti dualSpaceWitness where \ { ti :: ∀ w . (LinearSpace w, Scalar w ~ s) => DualSpaceWitness w -> (V s⊗w)+>(V s⊗w) \ @@ -262,7 +262,7 @@ instance ∀ s . (Num' s, Eq s) => LinearSpace (V s) where { \ \(Tensor wv) dw -> fmap (arr $ applyDualVector $ dw) wv; -} \ contractLinearMapAgainst = bilinearFunction $ getLinearMap >>> (contraaction); \ applyDualVector = bilinearFunction Mat.dot; \ - applyLinear = bilinearFunction $ \(LV m) \ + applyLinear = bilinearFunction $ \(LinearMap m) \ -> foldl' (^+^) zeroV . liftA2 (^*) m; \ applyTensorFunctional = bilinearFunction $ \(LinearMap f) (Tensor t) \ -> sum $ liftA2 (<.>^) f t; \ @@ -273,7 +273,6 @@ instance ∀ s . (Num' s, Eq s) => LinearSpace (V s) where { \ \f (LinearMap g) -> LinearMap $ fmap ((applyLinear-+$>f)-+$>) g; \ useTupleLinearSpaceComponents _ = usingNonTupleTypeAsTupleError } FreeLinearSpace( V0, 0 - , LinearMap , \(Tensor V0) -> zeroV , \_ -> LinearMap V0 , V0 @@ -284,7 +283,6 @@ FreeLinearSpace( V0, 0 , \V0 -> [] ) FreeLinearSpace( V1, 1 - , LinearMap , \(Tensor (V1 w₀)) -> w₀⊗V1 1 , \w -> LinearMap $ V1 (Tensor $ V1 w) , V1 V1 @@ -295,7 +293,6 @@ FreeLinearSpace( V1, 1 , \(V1 x) -> [x] ) FreeLinearSpace( V2, 2 - , LinearMap , \(Tensor (V2 w₀ w₁)) -> w₀⊗V2 1 0 ^+^ w₁⊗V2 0 1 , \w -> LinearMap $ V2 (Tensor $ V2 w zeroV) @@ -310,7 +307,6 @@ FreeLinearSpace( V2, 2 , \(V2 x y) -> (x:y:[]) ) FreeLinearSpace( V3, 3 - , LinearMap , \(Tensor (V3 w₀ w₁ w₂)) -> w₀⊗V3 1 0 0 ^+^ w₁⊗V3 0 1 0 ^+^ w₂⊗V3 0 0 1 @@ -331,7 +327,6 @@ FreeLinearSpace( V3, 3 , \(V3 x y z) -> x:y:z:[] ) FreeLinearSpace( V4, 4 - , LinearMap , \(Tensor (V4 w₀ w₁ w₂ w₃)) -> w₀⊗V4 1 0 0 0 ^+^ w₁⊗V4 0 1 0 0 ^+^ w₂⊗V4 0 0 1 0 From 256c4af9a03f003f24c3cb8b3efd9aa19f282264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justus=20Sagem=C3=BCller?= Date: Thu, 22 May 2025 13:53:55 +0200 Subject: [PATCH 2/3] Avoid uppercase argument names in CPP macros. As per https://github.com/leftaroundabout/linearmap-family/issues/7, such arguments appear to be not correctly substituted on some platforms. --- Math/LinearMap/Category/Instances.hs | 38 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Math/LinearMap/Category/Instances.hs b/Math/LinearMap/Category/Instances.hs index a52d3bf..94bd87c 100644 --- a/Math/LinearMap/Category/Instances.hs +++ b/Math/LinearMap/Category/Instances.hs @@ -113,10 +113,10 @@ autoLinearManifoldWitness = LinearManifoldWitness BoundarylessWitness #endif -#define LinearScalarSpace(S) \ -instance Num' (S) where {closedScalarWitness = ClosedScalarWitness}; \ -instance TensorSpace (S) where { \ - type TensorProduct (S) w = w; \ +#define LinearScalarSpace(scl) \ +instance Num' (scl) where {closedScalarWitness = ClosedScalarWitness}; \ +instance TensorSpace (scl) where { \ + type TensorProduct (scl) w = w; \ scalarSpaceWitness = ScalarSpaceWitness; \ linearManifoldWitness = autoLinearManifoldWitness; \ zeroTensor = Tensor zeroV; \ @@ -138,8 +138,8 @@ instance TensorSpace (S) where { \ = unsafeWriteArrayWithOffset ar i v; \ coerceFmapTensorProduct _ VSCCoercion = Coercion; \ wellDefinedTensor (Tensor w) = Tensor <$> wellDefinedVector w }; \ -instance LinearSpace (S) where { \ - type DualVector (S) = (S); \ +instance LinearSpace (scl) where { \ + type DualVector (scl) = (scl); \ dualSpaceWitness = DualSpaceWitness; \ linearId = LinearMap 1; \ tensorId = uncurryLinearMap $ LinearMap $ fmap (LinearFunction $ follow Tensor) -+$> id; \ @@ -194,27 +194,27 @@ tensorUnsafeWriteArrayWithOffsetViaList v2l ar i (Tensor t) toInterior = pure; fromInterior = id; translateP = Tagged (^+^); #endif -#define FreeLinearSpace( V, d, tp \ +#define FreeLinearSpace( vecCstr, d, tp \ , tenspl, tenid, dspan \ , contraction, contraaction \ , frls, tols ) \ -instance Num s => Semimanifold (V s) where { \ - type Needle (V s) = V s; \ +instance Num s => Semimanifold (vecCstr s) where { \ + type Needle (vecCstr s) = vecCstr s; \ FreeLinSpaceInteriorDecls \ (.+~^) = (^+^) }; \ -instance Num s => PseudoAffine (V s) where { \ +instance Num s => PseudoAffine (vecCstr s) where { \ v.-~.w = pure (v^-^w); (.-~!) = (^-^) }; \ -instance ∀ s . (Num' s, Eq s) => DimensionAware (V s) where { \ - type StaticDimension (V s) = 'Just (d); \ +instance ∀ s . (Num' s, Eq s) => DimensionAware (vecCstr s) where { \ + type StaticDimension (vecCstr s) = 'Just (d); \ dimensionalityWitness = IsStaticDimensional }; \ -instance ∀ s . (Num' s, Eq s) => (d)`Dimensional`V (s) where { \ +instance ∀ s . (Num' s, Eq s) => (d)`Dimensional`vecCstr (s) where { \ unsafeFromArrayWithOffset \ = unsafeFromArrayWithOffsetViaList (frls); \ unsafeWriteArrayWithOffset \ = unsafeWriteArrayWithOffsetViaList (tols) \ }; \ -instance ∀ s . (Num' s, Eq s) => TensorSpace (V s) where { \ - type TensorProduct (V s) w = V w; \ +instance ∀ s . (Num' s, Eq s) => TensorSpace (vecCstr s) where { \ + type TensorProduct (vecCstr s) w = vecCstr w; \ scalarSpaceWitness = case closedScalarWitness :: ClosedScalarWitness s of{ \ ClosedScalarWitness -> ScalarSpaceWitness}; \ linearManifoldWitness = autoLinearManifoldWitness; \ @@ -242,17 +242,17 @@ instance ∀ s . (Num' s, Eq s) => TensorSpace (V s) where { coerceFmapTensorProduct _ VSCCoercion = Coercion; \ wellDefinedTensor = getTensorProduct >>> Hask.traverse wellDefinedVector \ >>> fmap Tensor }; \ -instance ∀ s . (Num' s, Eq s) => LinearSpace (V s) where { \ - type DualVector (V s) = V s; \ +instance ∀ s . (Num' s, Eq s) => LinearSpace (vecCstr s) where { \ + type DualVector (vecCstr s) = vecCstr s; \ dualSpaceWitness = case closedScalarWitness :: ClosedScalarWitness s of \ {ClosedScalarWitness -> DualSpaceWitness}; \ linearId = LinearMap Mat.identity; \ idTensor = Tensor Mat.identity; \ tensorId = ti dualSpaceWitness where \ - { ti :: ∀ w . (LinearSpace w, Scalar w ~ s) => DualSpaceWitness w -> (V s⊗w)+>(V s⊗w) \ + { ti :: ∀ w . (LinearSpace w, Scalar w ~ s) => DualSpaceWitness w -> (vecCstr s⊗w)+>(vecCstr s⊗w) \ ; ti DualSpaceWitness = LinearMap $ \ fmap (\f -> fmap (LinearFunction $ Tensor . f)-+$>asTensor $ id) \ - (tenid :: V (w -> V w)) }; \ + (tenid :: vecCstr (w -> vecCstr w)) }; \ coerceDoubleDual = VSCCoercion; \ fromLinearForm = case closedScalarWitness :: ClosedScalarWitness s of{ \ ClosedScalarWitness -> LinearFunction $ flout LinearMap}; \ From 459c225a6691dae24af0c925b3998d434e286a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justus=20Sagem=C3=BCller?= Date: Thu, 22 May 2025 16:37:16 +0200 Subject: [PATCH 3/3] Completely avoid uppercase characters in CPP-macro argument names. This might fix issues when building on Mac OS. --- Math/LinearMap/Category/Instances.hs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Math/LinearMap/Category/Instances.hs b/Math/LinearMap/Category/Instances.hs index 94bd87c..b9b86c7 100644 --- a/Math/LinearMap/Category/Instances.hs +++ b/Math/LinearMap/Category/Instances.hs @@ -194,27 +194,27 @@ tensorUnsafeWriteArrayWithOffsetViaList v2l ar i (Tensor t) toInterior = pure; fromInterior = id; translateP = Tagged (^+^); #endif -#define FreeLinearSpace( vecCstr, d, tp \ +#define FreeLinearSpace( vec_cstr, d, tp \ , tenspl, tenid, dspan \ , contraction, contraaction \ , frls, tols ) \ -instance Num s => Semimanifold (vecCstr s) where { \ - type Needle (vecCstr s) = vecCstr s; \ +instance Num s => Semimanifold (vec_cstr s) where { \ + type Needle (vec_cstr s) = vec_cstr s; \ FreeLinSpaceInteriorDecls \ (.+~^) = (^+^) }; \ -instance Num s => PseudoAffine (vecCstr s) where { \ +instance Num s => PseudoAffine (vec_cstr s) where { \ v.-~.w = pure (v^-^w); (.-~!) = (^-^) }; \ -instance ∀ s . (Num' s, Eq s) => DimensionAware (vecCstr s) where { \ - type StaticDimension (vecCstr s) = 'Just (d); \ +instance ∀ s . (Num' s, Eq s) => DimensionAware (vec_cstr s) where { \ + type StaticDimension (vec_cstr s) = 'Just (d); \ dimensionalityWitness = IsStaticDimensional }; \ -instance ∀ s . (Num' s, Eq s) => (d)`Dimensional`vecCstr (s) where { \ +instance ∀ s . (Num' s, Eq s) => (d)`Dimensional`vec_cstr (s) where { \ unsafeFromArrayWithOffset \ = unsafeFromArrayWithOffsetViaList (frls); \ unsafeWriteArrayWithOffset \ = unsafeWriteArrayWithOffsetViaList (tols) \ }; \ -instance ∀ s . (Num' s, Eq s) => TensorSpace (vecCstr s) where { \ - type TensorProduct (vecCstr s) w = vecCstr w; \ +instance ∀ s . (Num' s, Eq s) => TensorSpace (vec_cstr s) where { \ + type TensorProduct (vec_cstr s) w = vec_cstr w; \ scalarSpaceWitness = case closedScalarWitness :: ClosedScalarWitness s of{ \ ClosedScalarWitness -> ScalarSpaceWitness}; \ linearManifoldWitness = autoLinearManifoldWitness; \ @@ -242,17 +242,17 @@ instance ∀ s . (Num' s, Eq s) => TensorSpace (vecCstr s) where { coerceFmapTensorProduct _ VSCCoercion = Coercion; \ wellDefinedTensor = getTensorProduct >>> Hask.traverse wellDefinedVector \ >>> fmap Tensor }; \ -instance ∀ s . (Num' s, Eq s) => LinearSpace (vecCstr s) where { \ - type DualVector (vecCstr s) = vecCstr s; \ +instance ∀ s . (Num' s, Eq s) => LinearSpace (vec_cstr s) where { \ + type DualVector (vec_cstr s) = vec_cstr s; \ dualSpaceWitness = case closedScalarWitness :: ClosedScalarWitness s of \ {ClosedScalarWitness -> DualSpaceWitness}; \ linearId = LinearMap Mat.identity; \ idTensor = Tensor Mat.identity; \ tensorId = ti dualSpaceWitness where \ - { ti :: ∀ w . (LinearSpace w, Scalar w ~ s) => DualSpaceWitness w -> (vecCstr s⊗w)+>(vecCstr s⊗w) \ + { ti :: ∀ w . (LinearSpace w, Scalar w ~ s) => DualSpaceWitness w -> (vec_cstr s⊗w)+>(vec_cstr s⊗w) \ ; ti DualSpaceWitness = LinearMap $ \ fmap (\f -> fmap (LinearFunction $ Tensor . f)-+$>asTensor $ id) \ - (tenid :: vecCstr (w -> vecCstr w)) }; \ + (tenid :: vec_cstr (w -> vec_cstr w)) }; \ coerceDoubleDual = VSCCoercion; \ fromLinearForm = case closedScalarWitness :: ClosedScalarWitness s of{ \ ClosedScalarWitness -> LinearFunction $ flout LinearMap}; \