From 08606a8b795a483a302500f6fd478388d17d278c Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 15:29:24 +0200 Subject: [PATCH] complex number assignment --- haskell/06-complex/README.md | 72 ++++++++++++++++++++ haskell/06-complex/complex.hs | 124 ++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 haskell/06-complex/README.md create mode 100644 haskell/06-complex/complex.hs diff --git a/haskell/06-complex/README.md b/haskell/06-complex/README.md new file mode 100644 index 0000000..652ca41 --- /dev/null +++ b/haskell/06-complex/README.md @@ -0,0 +1,72 @@ +# Complex Numbers + +## Assignment +Define a new type `Complex` that allows arithmetic calculations in an intuitive fashion. See below for the rules that shall be implemented. Incrementally make sure that all unit tests in `06-complex/complex.hs` are passed. + +## Complex Numbers + +Complex numbers are numbers of the form: $z = a + bi$ + +- **`a`**: The real part of the complex number. +- **`b`**: The imaginary part of the complex number. +- **`i`**: The imaginary unit, where $i^2 = -1$. + +Complex numbers are used in mathematics, physics, and engineering to extend the real number system and solve equations that have no real solutions, such as $x^2 + 1 = 0$. + +Complex numbers are often represented in both rectangular form $a + bi$ and polar/exponential form $r e^{i\theta}$. + + +## Rules for Computing with Complex Numbers + +### 1. **Addition and Subtraction** +- Add or subtract the real and imaginary parts separately. +- Rule: + $(a + bi) + (c + di) = (a + c) + (b + d)i$ + $(a + bi) - (c + di) = (a - c) + (b - d)i$ + +**Examples**: +1. $(2 + 3i) + (4 + 5i) = 6 + 8i$ +2. $(0 + 3i) + (2 + 0i) = 2 + 3i$ +3. $(7 + 2i) - (3 + 6i) = 4 - 4i$ +4. $(5 + 4i) - (1 + 2i) = 4 + 2i$ + +### 2. **Multiplication** +- Use the distributive property and the rule $i^2 = -1$. +- Rule: + $(a + bi)(c + di) = (ac - bd) + (ad + bc)i$ + +**Examples**: +1. $(1 + 2i)(3 + 4i) = -5 + 10i$ +2. $(2 + 3i)(1 - i) = 5 + i$ +3. $(4 + i)(2 - 3i) = 11 - 10i$ +4. $(3 + 2i)(3 + 2i) = 5 + 12i$ + +### 3. **Division** +- Multiply numerator and denominator by the conjugate of the denominator. +- Rule: + $\frac{a + bi}{c + di} = \frac{(a + bi)(c - di)}{(c + di)(c - di)}$ + Simplifies to: + $\frac{(ac + bd) + (bc - ad)i}{c^2 + d^2}$ + +**Examples**: +1. $\frac{1 + i}{1 - i} = 0 + 1i$ +2. $\frac{3 + 2i}{4 + 3i} = \frac{18 + i}{25} = 0.72 - 0.04i$ +3. $\frac{2 + i}{1 + i} = \frac{3 - i}{2} = 1.5 - 0.5i$ + +### 4. **Complex Conjugate** +- The conjugate of $a + bi$ is $a - bi$, used for simplifications and magnitude calculation. + +**Examples**: +1. Conjugate of $3 + 4i$ is $3 - 4i$. +2. Conjugate of $5 - i$ is $5 + i$. +3. Conjugate of $-2 + 3i$ is $-2 - 3i$. + +### 5. **Magnitude (Modulus)** +- The magnitude is the distance from the origin in the complex plane. +- Rule: $|a + bi| = \sqrt{a^2 + b^2}$ + +**Examples**: +1. $|3 + 4i| = \sqrt{3^2 + 4^2} = 5$ +2. $|1 - i| = \sqrt{1^2 + (-1)^2} = \sqrt{2}$ +3. $|0 + 5i| = \sqrt{0^2 + 5^2} = 5$ + diff --git a/haskell/06-complex/complex.hs b/haskell/06-complex/complex.hs new file mode 100644 index 0000000..54872cd --- /dev/null +++ b/haskell/06-complex/complex.hs @@ -0,0 +1,124 @@ +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} + +{-# HLINT ignore "Use void" #-} +import Test.HUnit + +data Complex a = TODO + +instance (Show a, Num a, Eq a) => Show (Complex a) where + -- show :: (Show a, Num a, Eq a) => Complex a -> String + -- TODO + +instance (Num a, Floating a) => Num (Complex a) where + -- (+) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a + -- (*) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a + -- TODO + -- 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 + -- fromRational r = TODO + -- recip (Complex re im) = TODO + + -- (Complex re1 im1) / (Complex re2 im2) = TODO + +-- conj :: Num a => Complex a -> Complex a +-- TODO + +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 ()