cayley-dickson 0.1.4.0 → 0.2.0.0
raw patch · 3 files changed
+27/−43 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Math.CayleyDickson: instance Data.Foldable.Foldable (Math.CayleyDickson.Nion n)
- Math.CayleyDickson: instance Data.Traversable.Traversable (Math.CayleyDickson.Nion n)
- Math.CayleyDickson: instance GHC.Base.Functor (Math.CayleyDickson.Nion n)
- Math.CayleyDickson: instance Math.CayleyDickson.Tag n => GHC.Base.Applicative (Math.CayleyDickson.Nion n)
Files
- cayley-dickson.cabal +1/−1
- src/Math/CayleyDickson.hs +25/−28
- test/test.hs +1/−14
cayley-dickson.cabal view
@@ -1,5 +1,5 @@ name: cayley-dickson-version: 0.1.4.0+version: 0.2.0.0 synopsis: Complex numbers, quaternions, octonions, sedenions, etc. description: Cayley-Dickson constructions (complex numbers, quaternions, octonions, sedenions, etc.) over general scalars without
src/Math/CayleyDickson.hs view
@@ -236,12 +236,12 @@ polarUsing sqrtMinus1 (Scalar r) = realPolar sqrtMinus1 r polarUsing sqrtMinus1 x | sqnormp == 0 = realPolar sqrtMinus1 r- | otherwise = (absx, acos (r / absx), u)+ | otherwise = (normx, acos (r / normx), u) where r = scalarPart x sqnormp = sqnorm x - r*r- u = purePart x /. (sqrt sqnormp)- absx = norm x+ u = purePart x /. sqrt sqnormp+ normx = norm x polar' :: (Tag n, Conjugable a, RealFloat a) => Proxy n -> Nion n a -> (a, a, Nion n a)@@ -313,12 +313,29 @@ basisElement1 = basisElement (1 :: Integer) ----------------------------------------------------------+-- util++-- Proper Functor and Foldable instances would have to translate+-- top-level scalars to their equivalent representation with padded+-- zeros. The machinations needed for this indirection are rather+-- cumbersome relative to the benefits of having the instances, whose+-- use would seem uncommon.++smap :: (a -> a) -> Nion n a -> Nion n a+smap f (Scalar s) = Scalar $ f s+smap f (x :@ y) = smap f x :@ smap f y++sfoldr :: (a -> b -> b) -> b -> Nion n a -> b+sfoldr f acc (Scalar x) = f x acc+sfoldr f acc (x :@ y) = sfoldr f (sfoldr f acc y) x++---------------------------------------------------------- -- accessors coords' :: (Tag n, Num a) => Proxy n -> Nion n a -> [a]-coords' n' (Scalar x) = x : replicate (fromInteger $ 2^n - 1) 0 where- n = tagVal n'-coords' _ x = foldr (:) [] x+coords' n (Scalar x) = x : genericReplicate k 0 where+ k = 2 ^ tagVal n - 1 :: Integer+coords' _ x = sfoldr (:) [] x -- | List of coordinates for this element. coords :: (Tag n, Num a) => Nion n a -> [a]@@ -390,26 +407,6 @@ x1 :@ x2 == y@(Scalar _) = x1 == y && x2 == 0 x1 :@ x2 == y1 :@ y2 = x1 == y1 && x2 == y2 -instance Functor (Nion n) where- fmap f (Scalar s) = Scalar $ f s- fmap f (x :@ y) = fmap f x :@ fmap f y--instance Tag n => Applicative (Nion n) where- pure = fill-- Scalar f <*> Scalar x = Scalar $ f x- Scalar f <*> x@(_ :@ _) = pure f <*> x- f@(_ :@ _) <*> (Scalar x) = f <*> pure x- (f1 :@ f2) <*> (x1 :@ x2) = (f1 <*> x1) :@ (f2 <*> x2)--instance Foldable (Nion n) where- foldr f acc (Scalar x) = f x acc- foldr f acc (x :@ y) = foldr f (foldr f acc y) x--instance Traversable (Nion n) where- traverse f (Scalar x) = Scalar <$> (f x)- traverse f (x :@ y) = (:@) <$> traverse f x <*> traverse f y- instance Conjugable a => Num (Nion n a) where Scalar x + Scalar y = Scalar $ x + y x@(Scalar _) + (y1 :@ y2) = (x + y1) :@ y2@@ -426,7 +423,7 @@ (x1 :@ x2) * y@(Scalar _) = (x1 * y) :@ (x2 * y) (x1 :@ x2) * (y1 :@ y2) = (x1 * y1 - conj y2 * x2) :@ (y2 * x1 + x2 * conj y1) - negate = fmap negate+ negate = smap negate fromInteger = fromScalar . fromInteger abs = doNotUse signum = doNotUse@@ -434,7 +431,7 @@ instance (Conjugable a, Fractional a) => Fractional (Nion n a) where Scalar x / Scalar y = Scalar $ x / y x@(Scalar _) / y@(_ :@ _) = x * recip y- x@(_ :@ _) / Scalar y = fmap (/ y) x+ x@(_ :@ _) / Scalar y = smap (/ y) x x@(_ :@ _) / y@(_ :@ _) = (x * conj y) /. sqnorm y recip x = conj x /. sqnorm x
test/test.hs view
@@ -241,7 +241,7 @@ assert $ x *. 10 == quaternion 10 20 30 40 assert $ (quaternion 1 2 3 4 :: Quaternion (Ratio Integer)) /. 2 ==- nion [1 % 2, 1, 3 % 2, 2] + nion [1 % 2, 1, 3 % 2, 2] forM_ [-2, -1, -0.5, 0, 0.5, 1, 2] $ \r -> do let y = fromScalar r :: Quaternion Double@@ -297,18 +297,6 @@ assert $ recip y == quaternion (1 % 30) (-1 % 15) (-1 % 10) (-2 % 15) assert $ y * recip y == 1 -checkApplicative :: IO ()-checkApplicative = do- let x = quaternion 1 2 3 4 :: Quaternion Integer- y = quaternion 5 6 7 8 :: Quaternion Integer- r = nion [3] :: Nion Tag0 Integer- s = nion [4] :: Nion Tag0 Integer- assert $ ((+) <$> x <*> y) == x + y- assert $ ((-) <$> x <*> y) == x - y- assert $ ((*) <$> 3 <*> x) == 3 * x- assert $ ((*) <$> x <*> 3) == x * 3- assert $ ((*) <$> r <*> s) == r * s- checkPower :: IO () checkPower = do let x = quaternion 1 2 3 4 :: Quaternion Integer@@ -416,7 +404,6 @@ checkOctonion checkSedenion checkBig- checkApplicative checkPower checkInverses checkMixed