Complex Numbers in Haskell
Implement complex numbers.
exercism fetch haskell complex-numbers
A complex number is a number in the form
a + b * i where
b are real and
i^2 = -1.
a is called the real part and
b is called the imaginary part of
The conjugate of the number
a + b * i is the number
a - b * i.
The absolute value of a complex number
z = a + b * i is a real number
|z| = sqrt(a^2 + b^2). The square of the absolute value
|z|^2 is the result of multiplication of
z by its complex conjugate.
The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately:
(a + i * b) + (c + i * d) = (a + c) + (b + d) * i,
(a + i * b) - (c + i * d) = (a - c) + (b - d) * i.
Multiplication result is by definition
(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i.
The reciprocal of a non-zero complex number is
1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i.
Dividing a complex number
a + i * b by another
c + i * d gives:
(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i.
Exponent of a complex number can be expressed as
exp(a + i * b) = exp(a) * exp(i * b),
and the last term is given by Euler's formula
exp(i * b) = cos(b) + i * sin(b).
Implement the following operations:
- addition, subtraction, multiplication and division of two complex numbers,
- conjugate, absolute value, exponent of a given complex number.
Assume the programming language you are using does not have an implementation of complex numbers.
For installation and learning resources, refer to the exercism help page.
Running the tests
To run the test suite, execute the following command:
If you get an error message like this...
No .cabal file found in directory
You are probably running an old stack version and need to upgrade it.
Otherwise, if you get an error message like this...
No compiler found, expected minor version match with... Try running "stack setup" to install the correct GHC...
Just do as it says and it will download and install the correct compiler version:
If you want to play with your solution in GHCi, just run the command:
Feedback, Issues, Pull Requests
The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!
Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.