forked from steger/pr3-sose2026
Implemented the complex-number exercise
parent
ad48ab3983
commit
5dff437064
|
|
@ -3,30 +3,49 @@
|
||||||
{-# HLINT ignore "Use void" #-}
|
{-# HLINT ignore "Use void" #-}
|
||||||
import Test.HUnit
|
import Test.HUnit
|
||||||
|
|
||||||
data Complex a = TODO
|
data Complex a = Complex a a deriving (Eq)
|
||||||
|
|
||||||
instance (Show a, Num a, Eq a) => Show (Complex a) where
|
instance (Show a, Num a, Eq a, Ord a) => Show (Complex a) where
|
||||||
-- show :: (Show a, Num a, Eq a) => Complex a -> String
|
show (Complex re im)
|
||||||
-- TODO
|
| re == 0 && im == 0 = "0"
|
||||||
|
| re == 0 && im == 1 = "i"
|
||||||
|
| re == 0 && im == -1 = "-i"
|
||||||
|
| re == 0 = show im ++ "i"
|
||||||
|
| im == 0 = show re
|
||||||
|
| im == 1 = show re ++ "+i"
|
||||||
|
| im == -1 = show re ++ "-i"
|
||||||
|
| im > 0 = show re ++ "+" ++ show im ++ "i"
|
||||||
|
| otherwise = show re ++ show im ++ "i"
|
||||||
|
|
||||||
instance (Num a, Floating a) => Num (Complex a) where
|
instance (Num a, Floating a, Eq a) => Num (Complex a) where
|
||||||
-- (+) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
|
(Complex re1 im1) + (Complex re2 im2) = Complex (re1 + re2) (im1 + im2)
|
||||||
-- (*) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
|
(Complex re1 im1) - (Complex re2 im2) = Complex (re1 - re2) (im1 - im2)
|
||||||
-- TODO
|
(Complex re1 im1) * (Complex re2 im2) = Complex (re1 * re2 - im1 * im2) (re1 * im2 + im1 * re2)
|
||||||
-- abs (Complex re im) = TODO
|
|
||||||
-- signum (Complex re im) = TODO
|
|
||||||
-- fromInteger n = TODO
|
|
||||||
-- negate :: (Num a, Floating a) => Complex a -> Complex a
|
|
||||||
-- negate (Complex re im) = Complex (negate re) (negate im)
|
|
||||||
|
|
||||||
instance (Fractional a, Floating a) => Fractional (Complex a) where
|
abs (Complex re im) = Complex (sqrt (re * re + im * im)) 0
|
||||||
-- fromRational r = TODO
|
signum (Complex re im)
|
||||||
-- recip (Complex re im) = TODO
|
| re == 0 && im == 0 = Complex 0 0
|
||||||
|
| otherwise = let mag = sqrt (re * re + im * im) in Complex (re / mag) (im / mag)
|
||||||
-- (Complex re1 im1) / (Complex re2 im2) = TODO
|
|
||||||
|
|
||||||
-- conj :: Num a => Complex a -> Complex a
|
fromInteger n = Complex (fromInteger n) 0
|
||||||
-- TODO
|
negate (Complex re im) = Complex (negate re) (negate im)
|
||||||
|
|
||||||
|
instance (Fractional a, Floating a, Eq a) => Fractional (Complex a) where
|
||||||
|
fromRational r = Complex (fromRational r) 0
|
||||||
|
recip (Complex re im) =
|
||||||
|
let denom = re * re + im * im
|
||||||
|
in Complex (re / denom) (negate im / denom)
|
||||||
|
|
||||||
|
(Complex re1 im1) / (Complex re2 im2) =
|
||||||
|
let denom = re2 * re2 + im2 * im2
|
||||||
|
in Complex ((re1 * re2 + im1 * im2) / denom) ((im1 * re2 - re1 * im2) / denom)
|
||||||
|
|
||||||
|
conj :: (Num a) => Complex a -> Complex a
|
||||||
|
conj (Complex re im) = Complex re (negate im)
|
||||||
|
|
||||||
|
-- Imaginary unit
|
||||||
|
i :: (Num a) => Complex a
|
||||||
|
i = Complex 0 1
|
||||||
|
|
||||||
tests :: Test
|
tests :: Test
|
||||||
tests =
|
tests =
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue