1
0
Fork 0
pr3-sose2026-fork/haskell/06-complex/complex.hs

143 lines
4.7 KiB
Haskell

{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use void" #-}
import Test.HUnit
data Complex a = Complex a a deriving (Eq)
instance (Show a, Num a, Eq a, Ord a) => Show (Complex a) where
show (Complex re im)
| 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, Eq a) => Num (Complex a) where
(Complex re1 im1) + (Complex re2 im2) = Complex (re1 + re2) (im1 + im2)
(Complex re1 im1) - (Complex re2 im2) = Complex (re1 - re2) (im1 - im2)
(Complex re1 im1) * (Complex re2 im2) = Complex (re1 * re2 - im1 * im2) (re1 * im2 + im1 * re2)
abs (Complex re im) = Complex (sqrt (re * re + im * im)) 0
signum (Complex re im)
| re == 0 && im == 0 = Complex 0 0
| otherwise = let mag = sqrt (re * re + im * im) in Complex (re / mag) (im / mag)
fromInteger n = Complex (fromInteger n) 0
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 =
TestList
[ Test.HUnit.TestCase (assertEqual "Show 1+2i" "1+2i" (show $ Complex 1 2)),
Test.HUnit.TestCase (assertEqual "Show 1" "1" (show $ Complex 1 0)),
Test.HUnit.TestCase (assertEqual "Show i" "i" (show i)),
Test.HUnit.TestCase (assertEqual "Show 5i" "5i" (show $ Complex 0 5)),
Test.HUnit.TestCase (assertEqual "Show 0" "0" (show $ Complex 0 0)),
Test.HUnit.TestCase (assertEqual "Compare Equal" True (Complex 2 3 == Complex 2 3)),
Test.HUnit.TestCase (assertEqual "Compare Real Not Equal" False (Complex 1 3 == Complex 2 3)),
Test.HUnit.TestCase
(assertEqual "Compare Imag Not Equal" False (Complex 2 4 == Complex 2 3)),
Test.HUnit.TestCase
(assertEqual "Compare Not Equal" False (Complex 2 3 /= Complex 2 3)),
Test.HUnit.TestCase
( assertEqual
"Addition 1"
(Complex 6.0 8.0)
(Complex 2.0 3.0 + Complex 4.0 5.0)
),
Test.HUnit.TestCase
( assertEqual
"Addition 2"
(Complex 2.0 3.0)
(Complex 0.0 3.0 + Complex 2.0 0.0)
),
Test.HUnit.TestCase
( assertEqual
"Subtraction 1"
(Complex 4.0 (-4.0))
(Complex 7.0 2.0 - Complex 3.0 6.0)
),
Test.HUnit.TestCase
( assertEqual
"Subtraction 2"
(Complex 4.0 2.0)
(Complex 5.0 4.0 - Complex 1.0 2.0)
),
Test.HUnit.TestCase
( assertEqual
"Negation"
(Complex (-7.0) (-2.0))
(negate (Complex 7.0 2.0))
),
Test.HUnit.TestCase
( assertEqual
"Multiplication 1"
(Complex (-5.0) 10.0)
(Complex 1.0 2.0 * Complex 3.0 4.0)
),
Test.HUnit.TestCase
( assertEqual
"Multiplication 2"
(Complex 5.0 1.0)
(Complex 2.0 3.0 * Complex 1.0 (-1.0))
),
Test.HUnit.TestCase
( assertEqual
"Multiplication 3"
(Complex 11.0 (-10.0))
(Complex 4.0 1.0 * Complex 2.0 (-3.0))
),
Test.HUnit.TestCase
( assertEqual
"Multiplication 4"
(Complex 5.0 12.0)
(Complex 3.0 2.0 * Complex 3.0 2.0)
),
Test.HUnit.TestCase
( assertEqual
"Magnitude 1"
(Complex 5.0 0.0)
(abs (Complex 3.0 4.0))
),
Test.HUnit.TestCase
( assertEqual
"Magnitude 2"
(Complex (sqrt 2.0) 0.0)
(abs (Complex 1.0 (-1.0)))
),
Test.HUnit.TestCase
( assertEqual
"Magnitude 3"
(Complex 5.0 0.0)
(abs (Complex 0.0 5.0))
),
TestCase (assertEqual "Conjugate of 3+4i" (Complex 3 (-4)) (conj (Complex 3 4))),
TestCase (assertEqual "Conjugate of 5-i" (Complex 5 1) (conj (Complex 5 (-1)))),
TestCase (assertEqual "Conjugate of -2+3i" (Complex (-2) (-3)) (conj (Complex (-2) 3)))
]
main :: IO ()
main = runTestTT tests >> return ()