factory 0.2.0.3 → 0.2.0.4
raw patch · 29 files changed
+224/−75 lines, 29 files
Files
- changelog +4/−1
- factory.cabal +1/−1
- src/Factory/Data/Interval.hs +8/−3
- src/Factory/Data/MonicPolynomial.hs +17/−2
- src/Factory/Data/Monomial.hs +1/−1
- src/Factory/Data/Polynomial.hs +24/−14
- src/Factory/Data/Ring.hs +6/−1
- src/Factory/Math/Factorial.hs +1/−1
- src/Factory/Math/Hyperoperation.hs +4/−4
- src/Factory/Math/Implementations/Factorial.hs +3/−3
- src/Factory/Math/Implementations/Primality.hs +8/−3
- src/Factory/Math/Implementations/PrimeFactorisation.hs +6/−1
- src/Factory/Math/MultiplicativeOrder.hs +1/−1
- src/Factory/Math/Power.hs +1/−1
- src/Factory/Math/Primality.hs +14/−4
- src/Factory/Math/PrimeFactorisation.hs +6/−1
- src/Factory/Math/Primes.hs +6/−1
- src/Factory/Math/Probability.hs +19/−7
- src/Factory/Math/Radix.hs +25/−4
- src/Factory/Math/SquareRoot.hs +3/−3
- src/Factory/Math/Statistics.hs +33/−7
- src/Factory/Test/Performance/Factorial.hs +6/−1
- src/Factory/Test/Performance/Hyperoperation.hs +2/−2
- src/Factory/Test/Performance/Primality.hs +1/−1
- src/Factory/Test/Performance/Primes.hs +6/−1
- src/Factory/Test/Performance/SquareRoot.hs +7/−2
- src/Factory/Test/Performance/Statistics.hs +6/−1
- src/Factory/Test/QuickCheck/MonicPolynomial.hs +4/−2
- src/Factory/Test/QuickCheck/Primes.hs +1/−1
changelog view
@@ -51,4 +51,7 @@ * Added 'Factory.Data.Interval.mkBounded'. * Generalised "Factory.Math.Statistics" to accept any 'Data.Foldable.Foldable' 'Functor', rather than merely lists. 0.2.0.3- * Added "class Show" to several contexts, for migration to 'ghc-7.4'.+ * Added class 'Show' to some contexts in "Factory.Math.Radix", for migration to 'ghc-7.4'.+0.2.0.4+ * Added classes 'Eq' and 'Show' to many contexts, for migration to 'ghc-7.4'.+ * Minor re-formatting.
factory.cabal view
@@ -1,6 +1,6 @@ --Package-properties Name: factory-Version: 0.2.0.3+Version: 0.2.0.4 Cabal-Version: >= 1.6 Copyright: (C) 2011 Dr. Alistair Ward License: GPL
src/Factory/Data/Interval.hs view
@@ -125,7 +125,12 @@ | otherwise = b -- | Bisect the /interval/ at the specified /end-point/; which should be between the two existing /end-points/.-splitAt' :: (Enum endPoint, Num endPoint, Ord endPoint) => endPoint -> Interval endPoint -> (Interval endPoint, Interval endPoint)+splitAt' :: (+ Enum endPoint,+ Num endPoint,+ Ord endPoint,+ Show endPoint+ ) => endPoint -> Interval endPoint -> (Interval endPoint, Interval endPoint) splitAt' i interval@(l, r) | any ($ i) [(< l), (>= r)] = error $ "Factory.Data.Interval.splitAt':\tunsuitable index=" ++ show i ++ " for interval=" ++ show interval ++ "." | otherwise = ((l, i), (succ i, r))@@ -168,7 +173,7 @@ (each leaf of which contains a list of up to 'minLength' integers, and each node of which contains an associative binary operator), and then collapsed to a scalar, by application of the operators. -}-divideAndConquer :: (Integral i, Data.Monoid.Monoid monoid)+divideAndConquer :: (Data.Monoid.Monoid monoid, Integral i, Show i) => (i -> monoid) -- ^ The monoid's constructor. -> Data.Ratio.Ratio i -- ^ The ratio of the original span, at which to bisect the 'Interval'. -> i -- ^ For efficiency, the /interval/ will not be bisected, when it's length has been reduced to this value.@@ -200,7 +205,7 @@ * Since the result can be large, 'divideAndConquer' is used to form operands of a similar order of magnitude, thus improving the efficiency of the big-number multiplication. -}-product' :: Integral i+product' :: (Integral i, Show i) => Data.Ratio.Ratio i -- ^ The ratio at which to bisect the 'Interval'. -> i -- ^ For efficiency, the /interval/ will not be bisected, when it's length has been reduced to this value. -> Interval i
src/Factory/Data/MonicPolynomial.hs view
@@ -47,7 +47,13 @@ } deriving (Eq, Show) -- | Smart constructor. Constructs an arbitrary /monic polynomial/.-mkMonicPolynomial :: (Num c, Ord e, Show e) => Data.Polynomial.Polynomial c e -> MonicPolynomial c e+mkMonicPolynomial :: (+ Eq c,+ Num c,+ Ord e,+ Show c,+ Show e+ ) => Data.Polynomial.Polynomial c e -> MonicPolynomial c e mkMonicPolynomial polynomial | not $ Data.Polynomial.isMonic polynomial = error $ "Factory.Data.MonicPolynomial.mkMonicPolynomial:\tnot monic; " ++ show polynomial | otherwise = MkMonicPolynomial polynomial@@ -58,9 +64,11 @@ * CAVEAT: it's not strictly an instance of this class, since the result of some methods isn't /monic/. -} instance (+ Eq c, Num c, Num e, Ord e,+ Show c, Show e ) => Data.Ring.Ring (MonicPolynomial c e) where MkMonicPolynomial l =*= MkMonicPolynomial r = MkMonicPolynomial $ l =*= r@@ -71,7 +79,14 @@ additiveIdentity = MkMonicPolynomial Data.Ring.additiveIdentity --CAVEAT: not monic ! -- Since the /leading term/ of the /denominator/ is one, the /coefficient/ isn't required to implement 'Fractional'.-instance (Num c, Num e, Ord e) => Data.QuotientRing.QuotientRing (MonicPolynomial c e) where+instance (+ Eq c,+ Num c,+ Num e,+ Ord e,+ Show c,+ Show e+ ) => Data.QuotientRing.QuotientRing (MonicPolynomial c e) where MkMonicPolynomial polynomialN `quotRem'` MkMonicPolynomial polynomialD = ToolShed.Data.Pair.mirror MkMonicPolynomial $ longDivide polynomialN where -- longDivide :: (Num c, Num e, Ord e) => Polynomial c e -> (Polynomial c e, Polynomial c e) longDivide numerator
src/Factory/Data/Monomial.hs view
@@ -95,7 +95,7 @@ (cL, eL) <*> (cR, eR) = (cL * cR, eL + eR) -- | Divide the two specified 'Monomial's.-(</>) :: (Fractional c, Num e)+(</>) :: (Eq c, Fractional c, Num e) => Monomial c e -- ^ Numerator. -> Monomial c e -- ^ Denominator. -> Monomial c e
src/Factory/Data/Polynomial.hs view
@@ -99,7 +99,12 @@ } deriving (Eq, Show) -- | Makes /Polynomial/ a 'Data.Ring.Ring', over the /field/ composed from all possible /coefficients/; <http://en.wikipedia.org/wiki/Polynomial_ring>.-instance (Num c, Num e, Ord e) => Data.Ring.Ring (Polynomial c e) where+instance (+ Eq c,+ Num c,+ Num e,+ Ord e+ ) => Data.Ring.Ring (Polynomial c e) where MkPolynomial [] =*= _ = zero _ =*= MkPolynomial [] = zero polynomialL =*= polynomialR@@ -148,7 +153,12 @@ ) $ map MkPolynomial . init {-remove terminal null-} . Data.List.tails . tail &&& map Data.Monomial.double $ getMonomialList p -- | Defines the ability to divide /polynomials/.-instance (Fractional c, Num e, Ord e) => Data.QuotientRing.QuotientRing (Polynomial c e) where+instance (+ Eq c,+ Fractional c,+ Num e,+ Ord e+ ) => Data.QuotientRing.QuotientRing (Polynomial c e) where {- Uses /Euclidian division/. <http://en.wikipedia.org/wiki/Polynomial_long_division>.@@ -185,12 +195,12 @@ getLeadingTerm (MkPolynomial (m : _)) = m -- | Removes terms with a /coefficient/ of zero.-pruneCoefficients :: Num c => Polynomial c e -> Polynomial c e+pruneCoefficients :: (Eq c, Num c) => Polynomial c e -> Polynomial c e pruneCoefficients (MkPolynomial []) = zero pruneCoefficients p = filter ((/= 0) . Data.Monomial.getCoefficient) `lift` p -- | Sorts into /descending order/ of exponents, groups /like/ exponents, and calls 'pruneCoefficients'.-normalise :: (Num c, Ord e) => Polynomial c e -> Polynomial c e+normalise :: (Eq c, Num c, Ord e) => Polynomial c e -> Polynomial c e normalise = pruneCoefficients . lift ( map ( foldr ((+) . Data.Monomial.getCoefficient) 0 &&& Data.Monomial.getExponent . head@@ -198,19 +208,19 @@ ) -- | Constructs an arbitrary /zeroeth-degree polynomial/, ie. independent of the /indeterminate/.-mkConstant :: (Num c, Num e) => c -> Polynomial c e+mkConstant :: (Eq c, Num c, Num e) => c -> Polynomial c e mkConstant 0 = zero mkConstant c = MkPolynomial [(c, 0)] -- | Constructs an arbitrary /first-degree polynomial/.-mkLinear :: (Num c, Num e)+mkLinear :: (Eq c, Num c, Num e) => c -- ^ Gradient. -> c -- ^ Constant. -> Polynomial c e mkLinear m c = pruneCoefficients $ MkPolynomial [(m, 1), (c, 0)] -- | Smart constructor. Constructs an arbitrary /polynomial/.-mkPolynomial :: (Num c, Ord e) => MonomialList c e -> Polynomial c e+mkPolynomial :: (Eq c, Num c, Ord e) => MonomialList c e -> Polynomial c e mkPolynomial [] = zero mkPolynomial l = normalise $ MkPolynomial l @@ -219,7 +229,7 @@ zero = MkPolynomial [] -- | Constructs a constant /monomial/, independent of the /indeterminate/.-one :: (Num c, Num e) => Polynomial c e+one :: (Eq c, Num c, Num e) => Polynomial c e one = mkConstant 1 -- | True if all /exponents/ are in the order defined by the specified comparator.@@ -237,11 +247,11 @@ inDescendingOrder = inOrder (>=) -- | True if no term has a /coefficient/ of zero.-isReduced :: Num c => Polynomial c e -> Bool+isReduced :: (Eq c, Num c) => Polynomial c e -> Bool isReduced = all ((/= 0) . Data.Monomial.getCoefficient) . getMonomialList -- | True if no term has a /coefficient/ of zero and the /exponents/ of successive terms are in /descending/ order.-isNormalised :: (Num c, Ord e) => Polynomial c e -> Bool+isNormalised :: (Eq c, Num c, Ord e) => Polynomial c e -> Bool isNormalised polynomial = all ($ polynomial) [isReduced, inDescendingOrder] {- |@@ -249,7 +259,7 @@ * <http://en.wikipedia.org/wiki/Monic_polynomial#Classifications>. -}-isMonic :: Num c => Polynomial c e -> Bool+isMonic :: (Eq c, Num c) => Polynomial c e -> Bool isMonic (MkPolynomial []) = False --All coefficients are zero, and have therefore been removed. isMonic p = (== 1) . Data.Monomial.getCoefficient $ getLeadingTerm p @@ -299,7 +309,7 @@ * <http://en.wikipedia.org/wiki/Scalar_multiplication>. -}-(*=) :: (Num c, Num e) => Polynomial c e -> Data.Monomial.Monomial c e -> Polynomial c e+(*=) :: (Eq c, Num c, Num e) => Polynomial c e -> Data.Monomial.Monomial c e -> Polynomial c e polynomial *= monomial | Data.Monomial.getCoefficient monomial == 1 = map (`Data.Monomial.shiftExponent` Data.Monomial.getExponent monomial) `lift` polynomial | otherwise = map (monomial <*>) `lift` polynomial@@ -309,7 +319,7 @@ * Whilst one could naively implement this as @(x Data.Ring.=^ n) `mod` m@, this will result in arithmetic operatons on unnecessarily big integers. -}-raiseModulo :: (Integral c, Integral power, Num e, Ord e)+raiseModulo :: (Integral c, Integral power, Num e, Ord e, Show power) => Polynomial c e -- ^ The base. -> power -- ^ The exponent to which the base should be raised. -> c -- ^ The modulus.@@ -345,7 +355,7 @@ * If the /polynomial/ is very sparse, this may be inefficient, since it /memoizes/ the complete sequence of powers up to the polynomial's /degree/. -}-evaluate :: (Num n, Integral e)+evaluate :: (Num n, Integral e, Show e) => n -- ^ The /indeterminate/. -> Polynomial n e -> n -- ^ The Result.
src/Factory/Data/Ring.hs view
@@ -72,7 +72,12 @@ * Exponentiation is implemented as a sequence of either squares of, or multiplications by, the /ring/-member; <http://en.wikipedia.org/wiki/Exponentiation_by_squaring>. -}-(=^) :: (Ring r, Eq r, Integral power) => r -> power -> r+(=^) :: (+ Eq r,+ Integral power,+ Ring r,+ Show power+ ) => r -> power -> r _ =^ 0 = multiplicativeIdentity ring =^ power | power < 0 = error $ "Factory.Data.Ring.(=^):\tthe result isn't guaranteed to be a ring-member, for power=" ++ show power
src/Factory/Math/Factorial.hs view
@@ -33,5 +33,5 @@ -- | Defines the methods expected of a /factorial/-algorithm. class Algorithmic algorithm where- factorial :: Integral i => algorithm -> i -> i+ factorial :: (Integral i, Show i) => algorithm -> i -> i
src/Factory/Math/Hyperoperation.hs view
@@ -67,7 +67,7 @@ <http://en.wikipedia.org/wiki/Tetration>, <http://www.tetration.org/Fractals/Atlas/index.html>. -}-powerTower :: (Integral base, Integral hyperExponent) => base -> hyperExponent -> base+powerTower :: (Integral base, Integral hyperExponent, Show base) => base -> hyperExponent -> base powerTower 0 hyperExponent | even hyperExponent = 1 | otherwise = 0@@ -77,7 +77,7 @@ | otherwise = Data.List.genericIndex (iterate (base ^) 1) hyperExponent -- | The /hyperoperation/-sequence; <http://en.wikipedia.org/wiki/Hyperoperation>.-hyperoperation :: Integral rank => rank -> Base -> HyperExponent -> Base+hyperoperation :: (Integral rank, Show rank) => rank -> Base -> HyperExponent -> Base hyperoperation rank base hyperExponent | rank < fromIntegral succession = error $ "Factory.Math.Hyperoperation.hyperoperation:\tundefined for rank; " ++ show rank | hyperExponent < 0 = error $ "Factory.Math.Hyperoperation.hyperoperation:\tundefined for hyper-exponent; " ++ show hyperExponent@@ -101,11 +101,11 @@ e' = {-fromIntegral $-} r ^# pred e -- | The /Ackermann-Peter/-function; <http://en.wikipedia.org/wiki/Ackermann_function#Ackermann_numbers>.-ackermannPeter :: Integral rank => rank -> HyperExponent -> Base+ackermannPeter :: (Integral rank, Show rank) => rank -> HyperExponent -> Base ackermannPeter rank = (+ negate 3) . hyperoperation rank 2 {-base-} . (+ 3) -- | True if @hyperoperation base hyperExponent@ has the same value for each specified 'rank'.-areCoincidental :: Integral rank => Base -> HyperExponent -> [rank] -> Bool+areCoincidental :: (Integral rank, Show rank) => Base -> HyperExponent -> [rank] -> Bool areCoincidental _ _ [] = True areCoincidental _ _ [_] = True areCoincidental base hyperExponent ranks = all (== h) hs where
src/Factory/Math/Implementations/Factorial.hs view
@@ -98,7 +98,7 @@ primeMultiplicity prime = sum . takeWhile (> 0) . tail . iterate (`div` prime) -- | Returns the /rising factorial/; <http://mathworld.wolfram.com/RisingFactorial.html>-risingFactorial :: Integral i+risingFactorial :: (Integral i, Show i) => i -- ^ The lower bound of the integer-range, whose product is returned. -> i -- ^ The number of integers in the range above. -> i -- ^ The result.@@ -107,7 +107,7 @@ risingFactorial x n = Data.Interval.product' (recip 2) 64 $ Data.Interval.normalise (x, pred $ x + n) -- | Returns the /falling factorial/; <http://mathworld.wolfram.com/FallingFactorial.html>-fallingFactorial :: Integral i+fallingFactorial :: (Integral i, Show i) => i -- ^ The upper bound of the integer-range, whose product is returned. -> i -- ^ The number of integers in the range beneath. -> i -- ^ The result.@@ -125,7 +125,7 @@ then manipulate them using the module "Data.PrimeFactors", and evaluate it using by /Data.PrimeFactors.product'/. -}-(!/!) :: (Integral i, Fractional f)+(!/!) :: (Integral i, Fractional f, Show i) => i -- ^ The /numerator/. -> i -- ^ The /denominator/. -> f -- ^ The resulting fraction.
src/Factory/Math/Implementations/Primality.hs view
@@ -107,7 +107,12 @@ [@Vibhor Bhatt and G. K. Patra@] <http://www.cmmacs.ernet.in/cmmacs/Publications/resch_rep/rrcm0307.pdf>, -}-isPrimeByAKS :: (Math.PrimeFactorisation.Algorithmic factorisationAlgorithm, Control.DeepSeq.NFData i, Integral i) => factorisationAlgorithm -> i -> Bool+isPrimeByAKS :: (+ Control.DeepSeq.NFData i,+ Integral i,+ Math.PrimeFactorisation.Algorithmic factorisationAlgorithm,+ Show i+ ) => factorisationAlgorithm -> i -> Bool isPrimeByAKS factorisationAlgorithm n = and [ not $ Math.PerfectPower.isPerfectPower n, --Step 1. Math.Primality.areCoprime n `all` filter (/= n) [2 .. r], --Step 3.@@ -155,7 +160,7 @@ the remainder belong to the subset of /liars/. In consequence, many false results must be accumulated for different bases, to convincingly identify a prime. -}-witnessesCompositeness :: Integral i+witnessesCompositeness :: (Integral i, Show i) => i -- ^ Candidate integer. -> i -> Int@@ -186,7 +191,7 @@ * <http://oeis.org/A014233>, <http://oeis.org/A006945>. -}-isPrimeByMillerRabin :: Integral i => i -> Bool+isPrimeByMillerRabin :: (Integral i, Show i) => i -> Bool isPrimeByMillerRabin primeCandidate = not $ witnessesCompositeness primeCandidate ( fst $ last binaryFactors --Odd-remainder. ) (
src/Factory/Math/Implementations/PrimeFactorisation.hs view
@@ -98,7 +98,12 @@ * This algorithm works best when there's a factor close to the /square-root/. -}-factoriseByFermatsMethod :: (Control.DeepSeq.NFData base, Integral base, Control.DeepSeq.NFData exponent, Num exponent) => base -> Data.PrimeFactors.Factors base exponent+factoriseByFermatsMethod :: (+ Control.DeepSeq.NFData base,+ Control.DeepSeq.NFData exponent,+ Integral base,+ Num exponent+ ) => base -> Data.PrimeFactors.Factors base exponent factoriseByFermatsMethod i | i <= 3 = [Data.Exponential.rightIdentity i] | even i = Data.Exponential.rightIdentity 2 : factoriseByFermatsMethod (i `div` 2) {-recurse-}
src/Factory/Math/MultiplicativeOrder.hs view
@@ -42,7 +42,7 @@ * <http://mathworld.wolfram.com/MultiplicativeOrder.html>. -}-multiplicativeOrder :: (Math.PrimeFactorisation.Algorithmic primeFactorisationAlgorithm, Control.DeepSeq.NFData i, Integral i)+multiplicativeOrder :: (Math.PrimeFactorisation.Algorithmic primeFactorisationAlgorithm, Control.DeepSeq.NFData i, Integral i, Show i) => primeFactorisationAlgorithm -> i -- ^ Base. -> i -- ^ Modulus.
src/Factory/Math/Power.hs view
@@ -62,7 +62,7 @@ * <http://en.wikipedia.org/wiki/Modular_exponentiation>. -}-raiseModulo :: (Integral i, Integral power)+raiseModulo :: (Integral i, Integral power, Show power) => i -- ^ Base. -> power -> i -- ^ Modulus.
src/Factory/Math/Primality.hs view
@@ -40,7 +40,7 @@ -- | Defines the methods expected of a primality-testing algorithm. class Algorithmic algorithm where- isPrime :: (Control.DeepSeq.NFData i, Integral i) => algorithm -> i -> Bool+ isPrime :: (Control.DeepSeq.NFData i, Integral i, Show i) => algorithm -> i -> Bool {- | 'True' if the two specified integers are /relatively prime/,@@ -68,7 +68,7 @@ * TODO: confirm that all values must be tested. -}-isFermatWitness :: Integral i => i -> Bool+isFermatWitness :: (Integral i, Show i) => i -> Bool isFermatWitness i = not . all isFermatPseudoPrime $ filter (areCoprime i) [2 .. pred i] where isFermatPseudoPrime base = Math.Power.raiseModulo base (pred i) i == 1 --CAVEAT: a /Fermat Pseudo-prime/ must also be a /composite/ number. @@ -79,7 +79,12 @@ * <http://mathworld.wolfram.com/CarmichaelNumber.html>. -}-isCarmichaelNumber :: (Algorithmic algorithm, Control.DeepSeq.NFData i, Integral i) => algorithm -> i -> Bool+isCarmichaelNumber :: (+ Algorithmic algorithm,+ Control.DeepSeq.NFData i,+ Integral i,+ Show i+ ) => algorithm -> i -> Bool isCarmichaelNumber algorithm i = not $ or [ i <= 2, even i,@@ -88,5 +93,10 @@ ] -- | An ordered list of the /Carmichael/ numbers; <http://en.wikipedia.org/wiki/Carmichael_number>.-carmichaelNumbers :: (Algorithmic algorithm, Control.DeepSeq.NFData i, Integral i) => algorithm -> [i]+carmichaelNumbers :: (+ Algorithmic algorithm,+ Control.DeepSeq.NFData i,+ Integral i,+ Show i+ ) => algorithm -> [i] carmichaelNumbers algorithm = isCarmichaelNumber algorithm `filter` [3, 5 ..]
src/Factory/Math/PrimeFactorisation.hs view
@@ -118,7 +118,12 @@ * AKA /EulerPhi/. -}-eulersTotient :: (Algorithmic algorithm, Control.DeepSeq.NFData i, Integral i) => algorithm -> i -> i+eulersTotient :: (+ Algorithmic algorithm,+ Control.DeepSeq.NFData i,+ Integral i,+ Show i+ ) => algorithm -> i -> i eulersTotient _ 1 = 1 eulersTotient algorithm i | i <= 0 = error $ "Factory.Math.PrimeFactorisation.eulersTotient:\tundefined for; " ++ show i
src/Factory/Math/Primes.hs view
@@ -41,5 +41,10 @@ * <http://mathworld.wolfram.com/Primorial.html>. -}-primorial :: (Algorithmic algorithm, Control.DeepSeq.NFData i, Data.Array.IArray.Ix i, Integral i) => algorithm -> [i]+primorial :: (+ Algorithmic algorithm,+ Control.DeepSeq.NFData i,+ Data.Array.IArray.Ix i,+ Integral i+ ) => algorithm -> [i] primorial = scanl (*) 1 . primes
src/Factory/Math/Probability.hs view
@@ -49,7 +49,7 @@ | NormalDistribution f f -- ^ Defines a /Normal/-distribution with a particular /mean/ and /variance/; <http://en.wikipedia.org/wiki/Normal_distribution>. deriving (Eq, Read, Show) -instance (Num a, Ord a) => ToolShed.SelfValidate.SelfValidator (ContinuousDistribution a) where+instance (Num a, Ord a, Show a) => ToolShed.SelfValidate.SelfValidator (ContinuousDistribution a) where getErrors distribution = ToolShed.SelfValidate.extractErrors $ case distribution of UniformDistribution interval -> [(Data.Interval.isReversed interval, "Reversed interval='" ++ show interval ++ "'.")] NormalDistribution _ v -> [(v < 0, "Negative variance=" ++ show v ++ ".")]@@ -57,7 +57,7 @@ -- | Describes a /discrete probability-distribution/; <http://en.wikipedia.org/wiki/List_of_probability_distributions#Discrete_distributions>. data DiscreteDistribution f = PoissonDistribution f deriving (Eq, Read, Show) -instance (Num f, Ord f) => ToolShed.SelfValidate.SelfValidator (DiscreteDistribution f) where+instance (Num f, Ord f, Show f) => ToolShed.SelfValidate.SelfValidator (DiscreteDistribution f) where getErrors (PoissonDistribution lambda) = ToolShed.SelfValidate.extractErrors [(lambda < 0, "Negative lambda=" ++ show lambda ++ ".")] {- |@@ -66,7 +66,7 @@ * <http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>. -}-boxMullerTransform :: (Floating f, Ord f)+boxMullerTransform :: (Floating f, Ord f, Show f) => (f, f) -- ^ Independent, /uniformly distributed/ random numbers, which must be within the /semi-closed unit interval/, /(0, 1]/. -> (f, f) -- ^ Independent, /normally distributed/ random numbers, with standardized /mean/=0 and /variance/=1. boxMullerTransform cartesian@@ -93,7 +93,12 @@ * <http://en.wikipedia.org/wiki/Normal_distribution>, <http://mathworld.wolfram.com/NormalDistribution.html>. -}-generateStandardizedNormalDistribution :: (System.Random.RandomGen randomGen, RealFloat f, System.Random.Random f) => randomGen -> [f]+generateStandardizedNormalDistribution :: (+ RealFloat f,+ Show f,+ System.Random.Random f,+ System.Random.RandomGen randomGen+ ) => randomGen -> [f] generateStandardizedNormalDistribution = ToolShed.Data.List.linearise . uncurry (zipWith $ curry boxMullerTransform) . ToolShed.Data.Pair.mirror ( System.Random.randomRs (minPositiveFloat undefined, 1) ) . System.Random.split@@ -109,7 +114,12 @@ the generated population will only tend towards the requested /mean/ and /variance/ of, as the sample-size tends towards infinity. Whilst one could arrange for these criteria to be precisely met for any sample-size, the sample would lose a degree of randomness as a result. -}-generateContinuousPopulation :: (RealFloat f, System.Random.Random f, System.Random.RandomGen randomGen)+generateContinuousPopulation :: (+ RealFloat f,+ Show f,+ System.Random.Random f,+ System.Random.RandomGen randomGen+ ) => Int -- ^ number of items. -> ContinuousDistribution f -> randomGen -- ^ A generator of /uniformly distributed/ random numbers.@@ -136,10 +146,11 @@ so for large /lambda/, this implementation returns the appropriate 'NormalDistribution', which is similar for large /lambda/. -} generatePoissonDistribution :: (+ Integral events, RealFloat lambda,+ Show lambda, System.Random.Random lambda,- System.Random.RandomGen randomGen,- Integral events+ System.Random.RandomGen randomGen ) => lambda -- ^ Defines the required approximate value of both /mean/ and /variance/. -> randomGen@@ -163,6 +174,7 @@ generateDiscretePopulation :: ( Ord f, RealFloat f,+ Show f, System.Random.Random f, System.Random.RandomGen randomGen, Integral events
src/Factory/Math/Radix.hs view
@@ -58,7 +58,13 @@ * The conversion to 'Char' can only succeed where printable and intelligible characters exist to represent all digits in the chosen base; which in practice means @(-36 <= base <= 36)@. -}-toBase :: (Data.Array.IArray.Ix decimal, Integral base, Integral decimal, Show decimal, Show base) => base -> decimal -> String+toBase :: (+ Data.Array.IArray.Ix decimal,+ Integral base,+ Integral decimal,+ Show base,+ Show decimal+ ) => base -> decimal -> String toBase 10 decimal = show decimal --Base unchanged. toBase _ 0 = "0" --Zero has the same representation in any base. toBase base decimal@@ -88,7 +94,12 @@ * Both negative numbers and negative bases are permissible. -}-fromBase :: (Integral base, Integral decimal, Read decimal, Show base) => base -> String -> decimal+fromBase :: (+ Integral base,+ Integral decimal,+ Read decimal,+ Show base+ ) => base -> String -> decimal fromBase 10 s = read s --Base unchanged. fromBase _ "0" = 0 --Zero has the same representation in any base. fromBase base s@@ -108,11 +119,21 @@ * <http://en.wikipedia.org/wiki/Digit_sum>. -}-digitSum :: (Data.Array.IArray.Ix decimal, Integral base, Integral decimal, Show decimal) => base -> decimal -> decimal+digitSum :: (+ Data.Array.IArray.Ix decimal,+ Integral base,+ Integral decimal,+ Show base,+ Show decimal+ ) => base -> decimal -> decimal digitSum 10 = fromIntegral . foldr ((+) . Data.Char.digitToInt) 0 . show digitSum base = sum . Data.Maybe.mapMaybe (`lookup` decodes) . toBase base -- | <http://en.wikipedia.org/wiki/Digital_root>.-digitalRoot :: (Data.Array.IArray.Ix decimal, Integral decimal) => decimal -> decimal+digitalRoot :: (+ Data.Array.IArray.Ix decimal,+ Integral decimal,+ Show decimal+ ) => decimal -> decimal digitalRoot = until (<= 9) (digitSum (10 :: Int))
src/Factory/Math/SquareRoot.hs view
@@ -53,14 +53,14 @@ -- | Defines the methods expected of a /square-root/ algorithm. class Algorithmic algorithm where- squareRootFrom :: Real operand+ squareRootFrom :: (Real operand, Show operand) => algorithm -> Estimate -- ^ An initial estimate from which to start. -> Math.Precision.DecimalDigits -- ^ The required precision. -> operand -- ^ The value for which to find the /square-root/. -> Result -- ^ Returns an improved estimate of the /square-root/, found using the specified algorithm, accurate to at least the required number of decimal digits. - squareRoot :: Real operand+ squareRoot :: (Real operand, Show operand) => algorithm -> Math.Precision.DecimalDigits -- ^ The required precision. -> operand -- ^ The value for which to find the /square-root/.@@ -82,7 +82,7 @@ rSqrt = sqrt . realToFrac -- | Uses 'Double'-precision floating-point arithmetic, to obtain an initial estimate for the /square-root/, and its accuracy.-getEstimate :: Real operand => operand -> Estimate+getEstimate :: (Real operand, Show operand) => operand -> Estimate getEstimate y | y < 0 = error $ "Factory.Math.SquareRoot.getEstimate:\tthere's no real square-root of " ++ show y | otherwise = (Math.Precision.simplify decimalDigits {-doubles performance by roughly length of the Rational representation-} . toRational $ rSqrt y, decimalDigits)
src/Factory/Math/Statistics.hs view
@@ -58,7 +58,12 @@ * Should the caller define the result-type as 'Data.Ratio.Rational', then it will be free from rounding-errors. -}-getDispersionFromMean :: (Data.Foldable.Foldable f, Functor f, Real r, Fractional result) => (Data.Ratio.Rational -> Data.Ratio.Rational) -> f r -> result+getDispersionFromMean :: (+ Data.Foldable.Foldable f,+ Fractional result,+ Functor f,+ Real r+ ) => (Data.Ratio.Rational -> Data.Ratio.Rational) -> f r -> result getDispersionFromMean weight x = getMean $ fmap (weight . (+ negate mean) . realToFrac) x where mean :: Data.Ratio.Rational mean = getMean x@@ -68,11 +73,21 @@ * Should the caller define the result-type as 'Data.Ratio.Rational', then it will be free from rounding-errors. -}-getVariance :: (Data.Foldable.Foldable f, Functor f, Real r, Fractional variance) => f r -> variance+getVariance :: (+ Data.Foldable.Foldable f,+ Fractional variance,+ Functor f,+ Real r+ ) => f r -> variance getVariance = getDispersionFromMean Math.Power.square -- | Determines the /standard-deviation/ of the specified numbers; <http://en.wikipedia.org/wiki/Standard_deviation>.-getStandardDeviation :: (Data.Foldable.Foldable f, Functor f, Real r, Floating result) => f r -> result+getStandardDeviation :: (+ Data.Foldable.Foldable f,+ Floating result,+ Functor f,+ Real r+ ) => f r -> result getStandardDeviation = sqrt . getVariance {- |@@ -80,11 +95,22 @@ * Should the caller define the result-type as 'Data.Ratio.Rational', then it will be free from rounding-errors. -}-getAverageAbsoluteDeviation :: (Data.Foldable.Foldable f, Functor f, Real r, Fractional result) => f r -> result+getAverageAbsoluteDeviation :: (+ Data.Foldable.Foldable f,+ Fractional result,+ Functor f,+ Real r+ ) => f r -> result getAverageAbsoluteDeviation = getDispersionFromMean abs -- | Determines the /coefficient-of-variance/ of the specified numbers; <http://en.wikipedia.org/wiki/Coefficient_of_variation>.-getCoefficientOfVariance :: (Data.Foldable.Foldable f, Functor f, Real r, Floating result) => f r -> result+getCoefficientOfVariance :: (+ Data.Foldable.Foldable f,+ Eq result,+ Floating result,+ Functor f,+ Real r+ ) => f r -> result getCoefficientOfVariance l | mean == 0 = error "Factory.Math.Statistics.getCoefficientOfVariance:\tundefined if mean is zero." | otherwise = getStandardDeviation l / abs mean@@ -92,7 +118,7 @@ mean = getMean l -- | The number of unordered /combinations/ of /r/ objects taken from /n/; <http://en.wikipedia.org/wiki/Combination>.-nCr :: (Math.Factorial.Algorithmic factorialAlgorithm, Integral i)+nCr :: (Math.Factorial.Algorithmic factorialAlgorithm, Integral i, Show i) => factorialAlgorithm -> i -- ^ The total number of items from which to select. -> i -- ^ The number of items in a sample.@@ -110,7 +136,7 @@ denominator = Math.Factorial.factorial factorialAlgorithm smaller -- | The number of /permutations/ of /r/ objects taken from /n/; <http://en.wikipedia.org/wiki/Permutations>.-nPr :: Integral i+nPr :: (Integral i, Show i) => i -- ^ The total number of items from which to select. -> i -- ^ The number of items in a sample. -> i -- ^ The number of permutations.
src/Factory/Test/Performance/Factorial.hs view
@@ -34,7 +34,12 @@ import qualified ToolShed.System.TimePure -- | Measures the CPU-time required by 'Math.Factorial.factorial'.-factorialPerformance :: (Math.Factorial.Algorithmic algorithm, Control.DeepSeq.NFData i, Integral i) => algorithm -> i -> IO (Double, i)+factorialPerformance :: (+ Control.DeepSeq.NFData i,+ Integral i,+ Math.Factorial.Algorithmic algorithm,+ Show i+ ) => algorithm -> i -> IO (Double, i) factorialPerformance algorithm = ToolShed.System.TimePure.getCPUSeconds . Math.Factorial.factorial algorithm -- | Measures the CPU-time required by a naive implementation.
src/Factory/Test/Performance/Hyperoperation.hs view
@@ -31,7 +31,7 @@ import qualified ToolShed.System.TimePure -- | Measures the CPU-time required by 'Math.Hyperoperation.hyperoperation'.-hyperoperationPerformance :: Integral rank => rank -> Math.Hyperoperation.Base -> Math.Hyperoperation.HyperExponent -> IO (Double, Integer)+hyperoperationPerformance :: (Integral rank, Show rank) => rank -> Math.Hyperoperation.Base -> Math.Hyperoperation.HyperExponent -> IO (Double, Integer) hyperoperationPerformance rank base = ToolShed.System.TimePure.getCPUSeconds . Math.Hyperoperation.hyperoperation rank base {- |@@ -57,7 +57,7 @@ * CAVEAT: nothing is returned, since the result is printed ... and it never terminates. -}-hyperoperationPerformanceGraphExponent :: Integral rank+hyperoperationPerformanceGraphExponent :: (Integral rank, Show rank) => Bool -- ^ Verbose. -> rank -> Math.Hyperoperation.Base
src/Factory/Test/Performance/Primality.hs view
@@ -39,7 +39,7 @@ | otherwise = ToolShed.System.TimePure.getCPUSeconds . take i $ Math.Primality.carmichaelNumbers primalityAlgorithm -- | Measures the CPU-time required to determine whether the specified integer is prime, which is returned together with the Boolean result.-isPrimePerformance :: (Control.DeepSeq.NFData i, Integral i) => Math.Primality.Algorithmic primalityAlgorithm => primalityAlgorithm -> i -> IO (Double, Bool)+isPrimePerformance :: (Control.DeepSeq.NFData i, Integral i, Show i) => Math.Primality.Algorithmic primalityAlgorithm => primalityAlgorithm -> i -> IO (Double, Bool) isPrimePerformance primalityAlgorithm = ToolShed.System.TimePure.getCPUSeconds . Math.Primality.isPrime primalityAlgorithm {- |
src/Factory/Test/Performance/Primes.hs view
@@ -31,5 +31,10 @@ import qualified ToolShed.System.TimePure -- | Measures the CPU-time required by 'Math.Primes.primes', to find the specified prime.-primesPerformance :: (Math.Primes.Algorithmic algorithm, Control.DeepSeq.NFData i, Data.Array.IArray.Ix i, Integral i) => algorithm -> Int -> IO (Double, i)+primesPerformance :: (+ Control.DeepSeq.NFData i,+ Data.Array.IArray.Ix i,+ Math.Primes.Algorithmic algorithm,+ Integral i+ ) => algorithm -> Int -> IO (Double, i) primesPerformance algorithm = ToolShed.System.TimePure.getCPUSeconds . (Math.Primes.primes algorithm !!)
src/Factory/Test/Performance/SquareRoot.hs view
@@ -32,7 +32,11 @@ import qualified ToolShed.System.TimePure -- | Measures the CPU-time required by 'Math.SquareRoot.squareRootFrom', which is returned together with the approximate rational result.-squareRootPerformance :: (Math.SquareRoot.Algorithmic algorithm, Real operand) => algorithm -> operand -> Math.Precision.DecimalDigits -> IO (Double, Math.SquareRoot.Result)+squareRootPerformance :: (+ Math.SquareRoot.Algorithmic algorithm,+ Real operand,+ Show operand+ ) => algorithm -> operand -> Math.Precision.DecimalDigits -> IO (Double, Math.SquareRoot.Result) squareRootPerformance algorithm operand requiredDecimalDigits = ToolShed.System.TimePure.getCPUSeconds $ Math.SquareRoot.squareRoot algorithm requiredDecimalDigits operand {- |@@ -44,8 +48,9 @@ squareRootPerformanceGraph :: ( Math.SquareRoot.Algorithmic algorithm, Math.SquareRoot.Iterator algorithm,+ Real operand, Show algorithm,- Real operand+ Show operand ) => algorithm -> operand -> IO () squareRootPerformanceGraph algorithm operand = mapM_ ( \requiredDecimalDigits -> putStrLn . (
src/Factory/Test/Performance/Statistics.hs view
@@ -31,7 +31,12 @@ import qualified ToolShed.System.TimePure -- | Measures the CPU-time required by 'Math.Statistics.nCr'.-nCrPerformance :: (Math.Factorial.Algorithmic factorialAlgorithm, Control.DeepSeq.NFData i, Integral i)+nCrPerformance :: (+ Control.DeepSeq.NFData i,+ Integral i,+ Math.Factorial.Algorithmic factorialAlgorithm,+ Show i+ ) => factorialAlgorithm -> i -- ^ The total number from which to select. -> i -- ^ The number of items in a sample.
src/Factory/Test/QuickCheck/MonicPolynomial.hs view
@@ -39,10 +39,12 @@ import qualified Test.QuickCheck instance (- Test.QuickCheck.Arbitrary c, Integral c,+ Integral e,+ Test.QuickCheck.Arbitrary c, Test.QuickCheck.Arbitrary e,- Integral e+ Show c,+ Show e ) => Test.QuickCheck.Arbitrary (Data.MonicPolynomial.MonicPolynomial c e) where arbitrary = do polynomial <- Test.QuickCheck.arbitrary
src/Factory/Test/QuickCheck/Primes.hs view
@@ -54,7 +54,7 @@ coarbitrary = undefined --CAVEAT: stops warnings from ghc. #endif -isPrime :: (Control.DeepSeq.NFData i, Integral i) => i -> Bool+isPrime :: (Control.DeepSeq.NFData i, Integral i, Show i) => i -> Bool isPrime = Math.Primality.isPrime primalityAlgorithm where primalityAlgorithm :: Math.Implementations.Primality.Algorithm Math.Implementations.PrimeFactorisation.Algorithm primalityAlgorithm = ToolShed.Defaultable.defaultValue