Encephalon

"Dubito,
ergo cogito,
ergo sum"

Add to Technorati Favorites

todizzle91@gmail.com

Google
 


Fri Jan 11

A Work in Progress

module Math
    where

data Point = Pt {x, y :: Float}
data Line = MBLine Float Float —slope, y-intercept
      | MPLine Float Point —slope, point
      | PPLine Point Point —point, point
data Polynom = Polynom [(Float, Float)] —list of coefficients and powers
data Ration = Ration {numerat, denominat :: Polynom}

answerToLifeTheUniverseAndEverything = 42

instance Show Point where
    show (Pt x y) = “(” ++ (show x) ++ “,” ++ (show y) ++ “)”

instance Show Line where
    show line = “y = ” ++ (show (slope line)) ++ “x + ” ++ (show (y (yIntercept line)))

instance Show Polynom where
    show (Polynom monomials) = “y =” ++ (drop 2 (foldl (++) (“”) (map (\x -> ” + ” ++ (show (fst x)) ++ “x^” ++ (show (snd x))) monomials)))

instance Show Ration where
    show (Ration num den) = “y = (” ++ (drop 4 (show num)) ++ “) / (” ++ (drop 4 (show den)) ++ “)”

class Function a where
    slope :: a -> Float
    yIntercept :: a -> Point
    zeros :: a -> [Point]
    coefficients :: a -> [Float]
    degree :: a -> Float

instance Function Line where
    slope (MBLine m _) = m
    slope (MPLine m _) = m
    slope (PPLine a b) = ((y a) - (y b)) / ((x a) - (x b))
   
    yIntercept (MBLine _ b) = Pt 0 b
    yIntercept (MPLine m pt) = Pt 0 ((y pt) - (m * (x pt)))
    yIntercept (PPLine pt1 pt2) = Pt 0 ((y pt1) - ((slope (PPLine pt1 pt2)) * (x pt1)))
   
    zeros line = [Pt ((negate (y (yIntercept line))) / (slope line)) 0]
   
    coefficients line = [(slope line), (y (yIntercept line))]
   
    degree _ = 1

instance Function Polynom where
    coefficients (Polynom monomials) = map fst monomials
   
    degree (Polynom monomials) = maximum (map snd monomials)

derivative :: Polynom -> Polynom
derivative (Polynom monomials) = Polynom (map (\x -> (((fst x) * (snd x)), ((snd x) - 1))) monomials)

sndDerivative :: Polynom -> Polynom
sndDerivative curve = derivative (derivative curve)

—maximums :: Polynom -> [Point]