From 86d41b5addedfc617bfe19328613596ed18a6acc Mon Sep 17 00:00:00 2001 From: stla Date: Sun, 21 Apr 2024 15:45:27 +0200 Subject: [PATCH] prettyParametricQSpray --- src/Math/Algebra/Hspray.hs | 40 +++++++++++++++++++++----------------- tests/Main.hs | 27 +++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Math/Algebra/Hspray.hs b/src/Math/Algebra/Hspray.hs index 3558ac0..6a38946 100644 --- a/src/Math/Algebra/Hspray.hs +++ b/src/Math/Algebra/Hspray.hs @@ -2999,7 +2999,8 @@ evalParametricSpray spray xs = if length xs >= numberOfVariables spray -- True canCoerceToSimpleParametricSpray :: (Eq a, AlgRing.C a) => ParametricSpray a -> Bool -canCoerceToSimpleParametricSpray spray = all isPolynomialRatioOfSprays (HM.elems spray) +canCoerceToSimpleParametricSpray spray = + all isPolynomialRatioOfSprays (HM.elems spray) -- | Coerces a parametric spray to a simple parametric spray, without -- checking this makes sense with `canCoerceToSimpleParametricSpray` @@ -3029,8 +3030,7 @@ fromOneParameterQSpray = HM.map fromRatioOfQPolynomials -- | Converts a parametric spray to a one-parameter spray, without checking -- the conversion makes sense parametricSprayToOneParameterSpray :: - forall a. (AlgRing.C a) - => ParametricSpray a -> OneParameterSpray a + forall a. (AlgRing.C a) => ParametricSpray a -> OneParameterSpray a parametricSprayToOneParameterSpray = HM.map toRatioOfPolynomials where toRatioOfPolynomials :: RatioOfSprays a -> RatioOfPolynomials a @@ -3047,8 +3047,7 @@ parametricSprayToOneParameterSpray = HM.map toRatioOfPolynomials -- | Converts a rational parametric spray to a rational one-parameter spray, -- without checking the conversion makes sense -parametricQSprayToOneParameterQSpray :: - ParametricQSpray -> OneParameterQSpray +parametricQSprayToOneParameterQSpray :: ParametricQSpray -> OneParameterQSpray parametricQSprayToOneParameterQSpray = HM.map toRatioOfQPolynomials where toRatioOfQPolynomials :: RatioOfQSprays -> RatioOfQPolynomials @@ -3139,16 +3138,19 @@ jacobiPolynomial n prettyParametricQSprayABCXYZ :: [String] -- ^ usually some letters, to denote the parameters of the spray -> [String] -- ^ usually some letters, to denote the variables of the spray - -> ParametricQSpray -- ^ a parametric rational spray + -> ParametricQSpray -- ^ a parametric rational spray -> String -prettyParametricQSprayABCXYZ abc xyz = - showSpray (prettyRatioOfQSpraysXYZ abc) ("{ ", " }") (showMonomialsXYZ xyz) +prettyParametricQSprayABCXYZ abc xyz spray = + showSpray rOSShower ("{ ", " }") (showMonomialsXYZ xyz) spray + where + rOSShower = if numberOfParameters spray <= length abc + then prettyRatioOfQSpraysXYZ abc + else prettyRatioOfQSpraysX1X2X3 (abc !! 0) -- | Pretty form of a parametric rational spray --- --- prop> prettyParametricQSpray spray == prettyParametricQSprayABCXYZ ["a"] ["X","Y","Z"] spray prettyParametricQSpray :: ParametricQSpray -> String -prettyParametricQSpray = prettyParametricQSprayABCXYZ ["a"] ["X", "Y", "Z"] +prettyParametricQSpray = + showSpray (prettyRatioOfQSpraysX1X2X3 "a") ("{ ", " }") (showMonomialsXYZ ["X", "Y", "Z"]) -- | Pretty form of a simple parametric rational spray, using some given strings (typically some -- letters) to denote the parameters and some given strings (typically some letters) to @@ -3169,16 +3171,18 @@ prettyParametricQSpray = prettyParametricQSprayABCXYZ ["a"] ["X", "Y", "Z"] -- >>> putStrLn $ prettySimpleParametricQSprayABCXYZ ["a","b"] ["X","Y","Z"] spqs -- { a + b }*X^2 + { a^2 + b^2 }*Y.Z prettySimpleParametricQSprayABCXYZ :: - [String] -- ^ usually some letters, to denote the parameters of the spray - -> [String] -- ^ usually some letters, to denote the variables of the spray + [String] -- ^ usually some letters, to denote the parameters of the spray + -> [String] -- ^ usually some letters, to denote the variables of the spray -> SimpleParametricQSpray -- ^ a parametric rational spray -> String -prettySimpleParametricQSprayABCXYZ abc xyz = - showSpray (prettyQSprayXYZ abc) ("{ ", " }") (showMonomialsXYZ xyz) +prettySimpleParametricQSprayABCXYZ abc xyz spray = + showSpray sprayShower ("{ ", " }") (showMonomialsXYZ xyz) spray + where + sprayShower = if numberOfParameters spray <= length abc + then prettyQSprayXYZ abc + else prettyQSprayX1X2X3 (abc !! 0) -- | Pretty form of a simple parametric rational spray --- --- prop> prettySimpleParametricQSpray spray == prettySimpleParametricQSprayABCXYZ ["a"] ["X","Y","Z"] spray prettySimpleParametricQSpray :: SimpleParametricQSpray -> String prettySimpleParametricQSpray = - prettySimpleParametricQSprayABCXYZ ["a"] ["X", "Y", "Z"] + showSpray (prettyQSprayX1X2X3 "a") ("{ ", " }") (showMonomialsXYZ ["X", "Y", "Z"]) diff --git a/tests/Main.hs b/tests/Main.hs index 9c8734e..4c45d1b 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -90,7 +90,8 @@ import Math.Algebra.Hspray ( Spray, substituteParameters, evalParametricSpray, asSimpleParametricSpray, - parametricSprayToOneParameterSpray + parametricSprayToOneParameterSpray, + prettyParametricQSprayABCXYZ ) import MathObj.Matrix ( fromRows ) import qualified MathObj.Matrix as MathMatrix @@ -104,13 +105,35 @@ import Test.Tasty.HUnit ( assertEqual , testCase ) +type PQS = ParametricQSpray + main :: IO () main = defaultMain $ testGroup "Testing hspray" [ - testCase "substituteParameters in Jacobi polynomial -> Legendre" $ do + testCase "prettyParametricQSprayABCXYZ" $ do + let + f :: (QSpray, QSpray) -> (PQS, PQS, PQS) -> PQS + f (a, b) (x, y, z) = + (a %:% (a ^+^ unitSpray)) *^ x^**^2 ^+^ (b %:% (a ^+^ b)) *^ (y ^*^ z) + pqs = f (lone 1, lone 2) (lone 1, lone 2, lone 3) + s1 = prettyParametricQSprayABCXYZ ["a","b"] ["X","Y","Z"] pqs + s2 = prettyParametricQSprayABCXYZ ["a"] ["X","Y","Z"] pqs + s3 = prettyParametricQSprayABCXYZ ["a","b"] ["X","Y"] pqs + assertEqual "" + [ + s1, s2, s3 + ] + [ + "{ [ a ] %//% [ a + 1 ] }*X^2 + { [ b ] %//% [ a + b ] }*Y.Z", + "{ [ a1 ] %//% [ a1 + 1 ] }*X^2 + { [ a2 ] %//% [ a1 + a2 ] }*Y.Z", + "{ [ a ] %//% [ a + 1 ] }*X1^2 + { [ b ] %//% [ a + b ] }*X2.X3" + ] + + + , testCase "substituteParameters in Jacobi polynomial -> Legendre" $ do let x = qlone 1 jacobi = jacobiPolynomial 5