cyclotomic 0.4.2 → 0.4.3
raw patch · 3 files changed
+109/−38 lines, 3 filesdep ~base
Dependency ranges changed: base
Files
- cyclotomic.cabal +2/−2
- src/Data/Complex/Cyclotomic.hs +41/−25
- src/Tests.hs +66/−11
cyclotomic.cabal view
@@ -1,5 +1,5 @@ Name: cyclotomic-Version: 0.4.2+Version: 0.4.3 Synopsis: A subfield of the complex numbers for exact calculation. Description: The cyclotomic numbers are a subset of the complex numbers that are represented exactly, enabling exact@@ -22,7 +22,7 @@ Tested-with: GHC == 7.6.3 Library Exposed-modules: Data.Complex.Cyclotomic- Build-depends: base >= 4.2 && < 4.7,+ Build-depends: base >= 4.2 && < 4.8, containers >= 0.3 && < 0.6, arithmoi >= 0.4 && < 0.5 Hs-source-dirs: src
src/Data/Complex/Cyclotomic.hs view
@@ -60,31 +60,32 @@ -} module Data.Complex.Cyclotomic- (Cyclotomic- ,i- ,e- ,sqrtInteger- ,sqrtRat- ,sinDeg- ,cosDeg- ,sinRev- ,cosRev- ,gaussianRat- ,polarRat- ,polarRatDeg- ,polarRatRev- ,conj- ,real- ,imag- ,isReal- ,isRat- ,isGaussianRat- ,toComplex- ,toReal- ,toRat- ,goldenRatio- ,dft- ,dftInv+ ( Cyclotomic+ , i+ , e+ , sqrtInteger+ , sqrtRat+ , sinDeg+ , cosDeg+ , sinRev+ , cosRev+ , gaussianRat+ , polarRat+ , polarRatDeg+ , polarRatRev+ , conj+ , real+ , imag+ , isReal+ , isRat+ , isGaussianRat+ , toComplex+ , toReal+ , toRat+ , goldenRatio+ , dft+ , dftInv+ , rootsQuadEq ) where @@ -545,3 +546,18 @@ dftInv cs = [minv * sum (zipWith (*) [e m^(k*n) | n <- [0..]] cs) | k <- [0..m-1]] where m = fromIntegral $ length cs minv = fromRational (1 % m)++-- | Solutions to the quadratic equation+-- a x^2 + b x + c = 0.+-- Returns 'Nothing' if a == 0.+rootsQuadEq :: Rational -- ^ a+ -> Rational -- ^ b+ -> Rational -- ^ c+ -> Maybe (Cyclotomic,Cyclotomic) -- ^ roots+rootsQuadEq a b c+ | a == 0 = Nothing+ | otherwise = Just ((-bb + sqrtDisc)/(2*aa),(-bb - sqrtDisc)/(2*aa))+ where+ aa = fromRational a+ bb = fromRational b+ sqrtDisc = sqrtRat (b*b - 4*a*c)
src/Tests.hs view
@@ -3,13 +3,34 @@ module Main where import Data.Complex.Cyclotomic-import Test.Framework (defaultMain, testGroup)+import Test.Framework+ ( defaultMain+ , testGroup+ ) import Test.Framework.Providers.HUnit+ ( testCase+ ) import Test.Framework.Providers.QuickCheck2+ ( testProperty+ ) import qualified Test.Framework.Providers.SmallCheck as S import qualified Test.Framework.Providers.API as T+import Test.QuickCheck+ ( Gen+ , elements+ , Arbitrary(..)+ , shrinkRealFrac+ ) import Test.HUnit+ ( (@?=)+ , Assertion+ )+import Data.List+ ( nub+ ) import Data.Ratio+ ( (%)+ ) main :: IO () main = defaultMain tests@@ -24,14 +45,15 @@ ,qc_Gauss ,qc_dftInv_dft ,qc_dft_dftInv+ ,qc_sum_quadratic_roots ] rationals :: [Rational] rationals = 0 % 1 : [sign * k % j | n <- [0..], m <- [0..n-1], sign <- [1,-1] , let k = m + 1, let j = n - m, gcd k j == 1] -cyclotomics :: [Cyclotomic]-cyclotomics = 0 : undefined+rationalList :: Integer -> [Rational]+rationalList m = nub [n % d | n <- [-m..m], d <- [1..m]] test1a :: T.Test test1a = testGroup "polarRat" [polarRatTest p q | p <- [0..10], q <- [1..10]]@@ -71,6 +93,26 @@ then (-1 + sqrtInteger nn) / 2 else (-1 + i*sqrtInteger nn) / 2 +prop_dftInv_dft :: [Rational] -> Bool+prop_dftInv_dft rs = dftInv (dft cs) == cs+ where cs = map fromRational rs++prop_dft_dftInv :: [Rational] -> Bool+prop_dft_dftInv rs = dft (dftInv cs) == cs+ where cs = map fromRational rs++prop_sum_quadratic_roots :: (Rational, Rational, Rational) -> Bool+prop_sum_quadratic_roots (a, b, c)+ = case rootsQuadEq a b c of+ Nothing -> a == 0+ Just (r1,r2) -> r1 + r2 == fromRational (-b / a)++prop_sum_quadratic_roots_small :: (SmallRational, SmallRational, SmallRational) -> Bool+prop_sum_quadratic_roots_small (SmallRational a, SmallRational b, SmallRational c)+ = case rootsQuadEq a b c of+ Nothing -> a == 0+ Just (r1,r2) -> r1 + r2 == fromRational (-b / a)+ ---------------------- -- QuickCheck Tests -- ----------------------@@ -99,10 +141,6 @@ }) $ testProperty "QuickCheck prop_Gauss" prop_Gauss -prop_dftInv_dft :: [Rational] -> Bool-prop_dftInv_dft rs = dftInv (dft cs) == cs- where cs = map fromRational rs- qc_dftInv_dft :: T.Test qc_dftInv_dft = T.plusTestOptions (T.TestOptions@@ -115,10 +153,6 @@ }) $ testProperty "QuickCheck prop_dftInv_dft" prop_dftInv_dft -prop_dft_dftInv :: [Rational] -> Bool-prop_dft_dftInv rs = dft (dftInv cs) == cs- where cs = map fromRational rs- qc_dft_dftInv :: T.Test qc_dft_dftInv = T.plusTestOptions (T.TestOptions@@ -130,4 +164,25 @@ ,T.topt_timeout = Nothing }) $ testProperty "QuickCheck prop_dft_dftInv" prop_dft_dftInv++qc_sum_quadratic_roots :: T.Test+qc_sum_quadratic_roots+ = testProperty "QuickCheck prop_sum_quadratic_roots" prop_sum_quadratic_roots_small++----------------------+-- QuickCheck Stuff --+----------------------++data SmallRational = SmallRational Rational+ deriving (Show,Ord,Eq)++smallRationalList :: [SmallRational]+smallRationalList = map SmallRational (rationalList 3)++smallRationalGen :: Gen SmallRational+smallRationalGen = elements smallRationalList++instance Arbitrary SmallRational where+ arbitrary = smallRationalGen+ shrink (SmallRational r) = map SmallRational (shrinkRealFrac r)