diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-12 14:23:01 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-12 15:31:38 +0100 |
| commit | 37e52bff39a1fe4d442cb253773252030c1cab8a (patch) | |
| tree | 9942561e55e3a51947b87c6f35ac5181f3dff71c /src/equation.hs | |
| parent | dbbc2f6798ba77d2ea7d9cce91d3bd1879e467a2 (diff) | |
| download | computorv2-37e52bff39a1fe4d442cb253773252030c1cab8a.tar.gz computorv2-37e52bff39a1fe4d442cb253773252030c1cab8a.tar.bz2 computorv2-37e52bff39a1fe4d442cb253773252030c1cab8a.zip | |
Basic expression parsing
Diffstat (limited to 'src/equation.hs')
| -rw-r--r-- | src/equation.hs | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/src/equation.hs b/src/equation.hs deleted file mode 100644 index f332131..0000000 --- a/src/equation.hs +++ /dev/null @@ -1,95 +0,0 @@ -module Equation -( Equation (..) -, Polynomial -, Term (..) -, degree -, reduce -, solve -, filterNull -) where - -import Data.List - - -data Equation = Equation { left :: Polynomial, right :: Polynomial } -type Polynomial = [Term] -data Term = Term { coefficient :: Float, exponent :: Int } - -instance Eq Term where - (Term _ e1) == (Term _ e2) = e1 == e2 - -instance Ord Term where - compare (Term _ e1) (Term _ e2) = compare e1 e2 - -instance Show Term where - show (Term 0 e) = "" - show (Term c 0) = show (round c) - show (Term c e) = show (round c) ++ " * X^" ++ show e - -instance Show Equation where - show (Equation l r) = showPolynomial (filterNull l) - ++ " = " - ++ showPolynomial (filterNull r) - where showPolynomial [] = "0" - showPolynomial p = dropWhile (`elem` " +") $ foldl f "" (map show p) - where f s "" = s - f s (c:cs) - | c == '-' = s ++ " - " ++ cs - | otherwise = s ++ " + " ++ (c:cs) - - -filterNull :: Polynomial -> Polynomial -filterNull = filter (\t -> coefficient t /= 0) - -equationMap :: (Polynomial -> Polynomial) -> Equation -> Equation -equationMap f (Equation l r) = Equation (f l) (f r) - -degree :: Polynomial -> Int -degree [] = 0 -degree p = Equation.exponent (maximum p) - -reduce :: Equation -> Equation -reduce equ = Equation (merge (left stdForm) (right stdForm)) [] - where stdForm = equationMap (\a -> (reducePolynomial $ sort a)) equ - merge [] rs = rs - merge ls [] = ls - merge (l:ls) (r:rs) - | l == r = (subTerm l r) : merge ls rs - | l < r = l : merge ls (r:rs) - | r < l = r : merge (l:ls) rs - where subTerm (Term c1 e) (Term c2 _) = Term (c1 - c2) e - reducePolynomial [] = [] - reducePolynomial [t] = [t] - reducePolynomial (t1:t2:ts) - | t1 == t2 = (addTerm t1 t2) : reducePolynomial ts - | otherwise = t1 : reducePolynomial (t2:ts) - where addTerm (Term c1 e) (Term c2 _) = Term (c1 + c2) e - -solveDegree2 :: Float -> Float -> Float -> [Float] -solveDegree2 a b c - | phi < 0 = [] - | phi == 0 = [(-b) / (2.0 * a)] - | phi > 0 = [ (-b + mySqrt phi) / (2.0 * a) -- not alowed - , (-b - mySqrt phi) / (2.0 * a) - ] - where phi = b * b - 4.0 * a * c - -solveDegree1 :: Float -> Float -> Float -solveDegree1 b c = -c / b - -solve :: Polynomial -> [Float] -solve [t0] = [] -solve [t0, t1] = [solveDegree1 (coefficient t1) (coefficient t0)] -solve [t0, t1, t2] = solveDegree2 (coefficient t2) (coefficient t1) (coefficient t0) -solve _ = undefined - -mySqrt :: Float -> Float -mySqrt n - | n < 0 = undefined - | otherwise = mySqrt' (n / 2) - where mySqrt' x = if abs (x * x - n) < 0.01 - then x - else mySqrt' xn - where xn = b - (a * a) / (2 * b) - where a = (n - x * x) / (2 * x) - b = x + a |
