From cc93d648089344338030a9b79cd7bea7c6e8c997 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 21 Jul 2013 14:25:30 -0400 Subject: [PATCH] Fix a bunch of hlint warnings. --- src/BigFloat.hs | 2 +- src/Integration/Simpson.hs | 2 +- src/Integration/Trapezoid.hs | 5 +-- src/Linear/Matrix.hs | 2 +- src/Linear/System.hs | 4 +-- src/Linear/Vector.hs | 9 +++-- src/Misc.hs | 3 +- src/Roots/Fast.hs | 64 ++++++++++++------------------------ src/Roots/Simple.hs | 8 ++--- 9 files changed, 42 insertions(+), 57 deletions(-) diff --git a/src/BigFloat.hs b/src/BigFloat.hs index 4c57aad..cc228be 100644 --- a/src/BigFloat.hs +++ b/src/BigFloat.hs @@ -22,7 +22,7 @@ type R = BigFloat Prec50 instance Epsilon e => Additive.C (BigFloat e) where (+) = (P.+) - zero = (P.fromInteger 0) + zero = 0 negate = (P.negate) instance Epsilon e => Ring.C (BigFloat e) where diff --git a/src/Integration/Simpson.hs b/src/Integration/Simpson.hs index c9ad414..6bfe258 100644 --- a/src/Integration/Simpson.hs +++ b/src/Integration/Simpson.hs @@ -45,7 +45,7 @@ simpson_1 :: (RealField.C a, ToRational.C a, RealField.C b) simpson_1 f a b = coefficient * ((f a) + 4*(f midpoint) + (f b)) where - coefficient = (fromRational' $ toRational (b - a)) / 6 + coefficient = fromRational' $ (toRational (b - a)) / 6 midpoint = (a + b) / 2 diff --git a/src/Integration/Trapezoid.hs b/src/Integration/Trapezoid.hs index 444d92d..06350fc 100644 --- a/src/Integration/Trapezoid.hs +++ b/src/Integration/Trapezoid.hs @@ -38,8 +38,9 @@ trapezoid_1 :: (Field.C a, ToRational.C a, Field.C b) -> a -- ^ The \"right\" endpoint, @b@ -> b trapezoid_1 f a b = - (((f a) + (f b)) / 2) * (fromRational' $ toRational (b - a)) - + (((f a) + (f b)) / 2) * coerced_interval_length + where + coerced_interval_length = fromRational' $ toRational (b - a) -- | Use the composite trapezoid rule to numerically integrate @f@ -- over @n@ subintervals of [@a@, @b@]. diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 20769b4..105acef 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -431,7 +431,7 @@ instance (Algebraic.C a, -- 5.0 -- norm_p p (Mat rows) = - (root p') $ sum [(fromRational' $ toRational x)^p' | x <- xs] + (root p') $ sum [fromRational' (toRational x)^p' | x <- xs] where p' = toInteger p xs = concat $ V.toList $ V.map V.toList rows diff --git a/src/Linear/System.hs b/src/Linear/System.hs index f49c652..85a1dd4 100644 --- a/src/Linear/System.hs +++ b/src/Linear/System.hs @@ -80,8 +80,8 @@ backward_substitute :: (Field.C a, Arity m) => Mat m m a -> Mat m N1 a -> Mat m N1 a -backward_substitute m b = - forward_substitute (transpose m) b +backward_substitute m = + forward_substitute (transpose m) -- | Solve the linear system m*x = b where m is positive definite. diff --git a/src/Linear/Vector.hs b/src/Linear/Vector.hs index 42d47a3..e4f6225 100644 --- a/src/Linear/Vector.hs +++ b/src/Linear/Vector.hs @@ -55,9 +55,14 @@ type Vec5 = Vec N5 -- >>> delete b 1 :: Vec2 Int -- fromList [1,3] -- -delete :: (Vector v a, Vector w a, Dim v ~ S (Dim w)) => v a -> Int -> w a +delete :: (Vector v a, + Vector w a, + Dim v ~ S (Dim w)) + => v a + -> Int + -> w a delete v1 idx = - fromList $ (lhalf ++ rhalf') + fromList (lhalf ++ rhalf') where (lhalf, rhalf) = splitAt idx (toList v1) rhalf' = tail rhalf diff --git a/src/Misc.hs b/src/Misc.hs index ddaad6e..bf8414d 100644 --- a/src/Misc.hs +++ b/src/Misc.hs @@ -36,7 +36,8 @@ partition n a b let xi = a + k'*h, let xj = a + (k'+1)*h ] where - h = (b-a)/(fromIntegral $ toInteger n) + coerced_n = fromIntegral $ toInteger n + h = (b-a)/coerced_n -- | Compute the unit roundoff (machine epsilon) for this machine. We diff --git a/src/Roots/Fast.hs b/src/Roots/Fast.hs index f7cde8a..8b69786 100644 --- a/src/Roots/Fast.hs +++ b/src/Roots/Fast.hs @@ -9,6 +9,7 @@ module Roots.Fast where import Data.List (find) +import Data.Maybe (fromMaybe) import Normed @@ -30,33 +31,20 @@ has_root :: (RealField.C a, -> Maybe b -- ^ Precoumpted f(a) -> Maybe b -- ^ Precoumpted f(b) -> Bool -has_root f a b epsilon f_of_a f_of_b = - if not ((signum (f_of_a')) * (signum (f_of_b')) == 1) then - -- We don't care about epsilon here, there's definitely a root! - True - else - if (b - a) <= epsilon' then - -- Give up, return false. - False - else - -- If either [a,c] or [c,b] have roots, we do too. +has_root f a b epsilon f_of_a f_of_b + | (signum (f_of_a')) * (signum (f_of_b')) /= 1 = True + | (b - a) <= epsilon' = False + | otherwise = (has_root f a c (Just epsilon') (Just f_of_a') Nothing) || (has_root f c b (Just epsilon') Nothing (Just f_of_b')) where -- If the size of the smallest subinterval is not specified, -- assume we just want to check once on all of [a,b]. - epsilon' = case epsilon of - Nothing -> (b-a) - Just eps -> eps + epsilon' = fromMaybe (b-a) epsilon -- Compute f(a) and f(b) only if needed. - f_of_a' = case f_of_a of - Nothing -> f a - Just v -> v - - f_of_b' = case f_of_b of - Nothing -> f b - Just v -> v + f_of_a' = fromMaybe (f a) f_of_a + f_of_b' = fromMaybe (f b) f_of_b c = (a + b)/2 @@ -87,13 +75,8 @@ bisect f a b epsilon f_of_a f_of_b else bisect f c b epsilon (Just f_of_c') (Just f_of_b') where -- Compute f(a) and f(b) only if needed. - f_of_a' = case f_of_a of - Nothing -> f a - Just v -> v - - f_of_b' = case f_of_b of - Nothing -> f b - Just v -> v + f_of_a' = fromMaybe (f a) f_of_a + f_of_b' = fromMaybe (f b) f_of_b c = (a + b) / 2 @@ -118,26 +101,21 @@ trisect f a b epsilon f_of_a f_of_b | f_of_b' == 0 = Just b | otherwise = -- Use a 'prime' just for consistency. - let (a', b', fa', fb') = - if (has_root f d b (Just epsilon) (Just f_of_d') (Just f_of_b')) - then (d, b, f_of_d', f_of_b') - else - if (has_root f c d (Just epsilon) (Just f_of_c') (Just f_of_d')) - then (c, d, f_of_c', f_of_d') - else (a, c, f_of_a', f_of_c') + let (a', b', fa', fb') + | has_root f d b (Just epsilon) (Just f_of_d') (Just f_of_b') = + (d, b, f_of_d', f_of_b') + | has_root f c d (Just epsilon) (Just f_of_c') (Just f_of_d') = + (c, d, f_of_c', f_of_d') + | otherwise = + (a, c, f_of_a', f_of_c') in if (b-a) < 2*epsilon then Just ((b+a)/2) else trisect f a' b' epsilon (Just fa') (Just fb') where -- Compute f(a) and f(b) only if needed. - f_of_a' = case f_of_a of - Nothing -> f a - Just v -> v - - f_of_b' = case f_of_b of - Nothing -> f b - Just v -> v + f_of_a' = fromMaybe (f a) f_of_a + f_of_b' = fromMaybe (f b) f_of_b c = (2*a + b) / 3 @@ -153,8 +131,8 @@ trisect f a b epsilon f_of_a f_of_b fixed_point_iterations :: (a -> a) -- ^ The function @f@ to iterate. -> a -- ^ The initial value @x0@. -> [a] -- ^ The resulting sequence of x_{n}. -fixed_point_iterations f x0 = - iterate f x0 +fixed_point_iterations = + iterate -- | Find a fixed point of the function @f@ with the search starting diff --git a/src/Roots/Simple.hs b/src/Roots/Simple.hs index a6aa09e..03b39ae 100644 --- a/src/Roots/Simple.hs +++ b/src/Roots/Simple.hs @@ -195,8 +195,8 @@ newton_iterations :: (Field.C a) -> (a -> a) -- ^ The derivative of @f@ -> a -- ^ Initial guess, x-naught -> [a] -newton_iterations f f' x0 = - iterate next x0 +newton_iterations f f' = + iterate next where next xn = xn - ( (f xn) / (f' xn) ) @@ -280,8 +280,8 @@ secant_iterations :: (Field.C a) -> a -- ^ Initial guess, x-naught -> a -- ^ Second initial guess, x-one -> [a] -secant_iterations f x0 x1 = - iterate2 g x0 x1 +secant_iterations f = + iterate2 g where g prev2 prev1 = let x_change = prev1 - prev2 -- 2.43.2