ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# Samyual's solution

## to Complex Numbers in the Erlang Track

Published at Feb 25 2021 · 0 comments
Instructions
Test suite
Solution

A complex number is a number in the form `a + b * i` where `a` and `b` are real and `i` satisfies `i^2 = -1`.

`a` is called the real part and `b` is called the imaginary part of `z`. 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`.

Raising e to a complex exponent can be expressed as `e^(a + i * b) = e^a * e^(i * b)`, the last term of which is given by Euler's formula `e^(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.

You also need to implement a `equal/2` function. For this you can consider two numbers as equal, when the difference of each component is less than 0.005.

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### complex_numbers_tests.erl

``````%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/complex-numbers/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(complex_numbers_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_real_part_of_a_purely_real_number_test_'() ->
{"Real part of a purely real number",
?_assert(1 ==
complex_numbers:real(complex_numbers:new(1, 0)))}.

'2_real_part_of_a_purely_imaginary_number_test_'() ->
{"Real part of a purely imaginary number",
?_assert(0 ==
complex_numbers:real(complex_numbers:new(0, 1)))}.

'3_real_part_of_a_number_with_real_and_imaginary_part_test_'() ->
{"Real part of a number with real and "
"imaginary part",
?_assert(1 ==
complex_numbers:real(complex_numbers:new(1, 2)))}.

'4_imaginary_part_of_a_purely_real_number_test_'() ->
{"Imaginary part of a purely real number",
?_assert(0 ==
complex_numbers:imaginary(complex_numbers:new(1, 0)))}.

'5_imaginary_part_of_a_purely_imaginary_number_test_'() ->
{"Imaginary part of a purely imaginary "
"number",
?_assert(1 ==
complex_numbers:imaginary(complex_numbers:new(0, 1)))}.

'6_imaginary_part_of_a_number_with_real_and_imaginary_part_test_'() ->
{"Imaginary part of a number with real "
"and imaginary part",
?_assert(2 ==
complex_numbers:imaginary(complex_numbers:new(1, 2)))}.

'7_imaginary_unit_test_'() ->
{"Imaginary unit",
?_assert(complex_numbers:equal(complex_numbers:new(-1,
0),
complex_numbers:mul(complex_numbers:new(0,
1),
complex_numbers:new(0,
1))))}.

{"Add purely real numbers",
?_assert(complex_numbers:equal(complex_numbers:new(3,
0),
0),
complex_numbers:new(2,
0))))}.

{"Add purely imaginary numbers",
?_assert(complex_numbers:equal(complex_numbers:new(0,
3),
1),
complex_numbers:new(0,
2))))}.

{"Add numbers with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(4,
6),
2),
complex_numbers:new(3,
4))))}.

'11_subtract_purely_real_numbers_test_'() ->
{"Subtract purely real numbers",
?_assert(complex_numbers:equal(complex_numbers:new(-1,
0),
complex_numbers:sub(complex_numbers:new(1,
0),
complex_numbers:new(2,
0))))}.

'12_subtract_purely_imaginary_numbers_test_'() ->
{"Subtract purely imaginary numbers",
?_assert(complex_numbers:equal(complex_numbers:new(0,
-1),
complex_numbers:sub(complex_numbers:new(0,
1),
complex_numbers:new(0,
2))))}.

'13_subtract_numbers_with_real_and_imaginary_part_test_'() ->
{"Subtract numbers with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(-2,
-2),
complex_numbers:sub(complex_numbers:new(1,
2),
complex_numbers:new(3,
4))))}.

'14_multiply_purely_real_numbers_test_'() ->
{"Multiply purely real numbers",
?_assert(complex_numbers:equal(complex_numbers:new(2,
0),
complex_numbers:mul(complex_numbers:new(1,
0),
complex_numbers:new(2,
0))))}.

'15_multiply_purely_imaginary_numbers_test_'() ->
{"Multiply purely imaginary numbers",
?_assert(complex_numbers:equal(complex_numbers:new(-2,
0),
complex_numbers:mul(complex_numbers:new(0,
1),
complex_numbers:new(0,
2))))}.

'16_multiply_numbers_with_real_and_imaginary_part_test_'() ->
{"Multiply numbers with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(-5,
10),
complex_numbers:mul(complex_numbers:new(1,
2),
complex_numbers:new(3,
4))))}.

'17_divide_purely_real_numbers_test_'() ->
{"Divide purely real numbers",
?_assert(complex_numbers:equal(complex_numbers:new(5.0e-1,
0),
complex_numbers:divide(complex_numbers:new(1,
0),
complex_numbers:new(2,
0))))}.

'18_divide_purely_imaginary_numbers_test_'() ->
{"Divide purely imaginary numbers",
?_assert(complex_numbers:equal(complex_numbers:new(5.0e-1,
0),
complex_numbers:divide(complex_numbers:new(0,
1),
complex_numbers:new(0,
2))))}.

'19_divide_numbers_with_real_and_imaginary_part_test_'() ->
{"Divide numbers with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(4.4e-1,
8.0e-2),
complex_numbers:divide(complex_numbers:new(1,
2),
complex_numbers:new(3,
4))))}.

'20_absolute_value_of_a_positive_purely_real_number_test_'() ->
{"Absolute value of a positive purely "
"real number",
?_assert(5 ==
complex_numbers:abs(complex_numbers:new(5, 0)))}.

'21_absolute_value_of_a_negative_purely_real_number_test_'() ->
{"Absolute value of a negative purely "
"real number",
?_assert(5 ==
complex_numbers:abs(complex_numbers:new(-5, 0)))}.

'22_absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part_test_'() ->
{"Absolute value of a purely imaginary "
"number with positive imaginary part",
?_assert(5 ==
complex_numbers:abs(complex_numbers:new(0, 5)))}.

'23_absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part_test_'() ->
{"Absolute value of a purely imaginary "
"number with negative imaginary part",
?_assert(5 ==
complex_numbers:abs(complex_numbers:new(0, -5)))}.

'24_absolute_value_of_a_number_with_real_and_imaginary_part_test_'() ->
{"Absolute value of a number with real "
"and imaginary part",
?_assert(5 ==
complex_numbers:abs(complex_numbers:new(3, 4)))}.

'25_conjugate_a_purely_real_number_test_'() ->
{"Conjugate a purely real number",
?_assert(complex_numbers:equal(complex_numbers:new(5,
0),
complex_numbers:conjugate(complex_numbers:new(5,
0))))}.

'26_conjugate_a_purely_imaginary_number_test_'() ->
{"Conjugate a purely imaginary number",
?_assert(complex_numbers:equal(complex_numbers:new(0,
-5),
complex_numbers:conjugate(complex_numbers:new(0,
5))))}.

'27_conjugate_a_number_with_real_and_imaginary_part_test_'() ->
{"Conjugate a number with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(1,
-1),
complex_numbers:conjugate(complex_numbers:new(1,
1))))}.

'28_eulers_identityformula_test_'() ->
{"Euler's identity/formula",
?_assert(complex_numbers:equal(complex_numbers:new(-1,
0),
complex_numbers:exp(complex_numbers:new(0,
3.14159265358979311600))))}.

'29_exponential_of_0_test_'() ->
{"Exponential of 0",
?_assert(complex_numbers:equal(complex_numbers:new(1,
0),
complex_numbers:exp(complex_numbers:new(0,
0))))}.

'30_exponential_of_a_purely_real_number_test_'() ->
{"Exponential of a purely real number",
?_assert(complex_numbers:equal(complex_numbers:new(2.71828182845904509080,
0),
complex_numbers:exp(complex_numbers:new(1,
0))))}.

'31_exponential_of_a_number_with_real_and_imaginary_part_test_'() ->
{"Exponential of a number with real and "
"imaginary part",
?_assert(complex_numbers:equal(complex_numbers:new(-2,
0),
complex_numbers:exp(complex_numbers:new(6.93147180559945286227e-1,
3.14159265358979311600))))}.``````
``````-module(complex_numbers).

-export([abs/1, add/2, conjugate/1, divide/2, equal/2, exp/1, imaginary/1, mul/2, new/2,
real/1, sub/2]).

-record(complex, {r :: number(), i :: number()}).

-spec abs(#complex{}) -> number().
abs(#complex{r = A, i = B}) -> math:sqrt(A * A + B * B).

-spec add(#complex{}, #complex{}) -> #complex{}.
add(#complex{r = A, i = B}, #complex{r = C, i = D}) -> new(A + C, B + D).

-spec conjugate(#complex{}) -> #complex{}.
conjugate(Z) -> new(Z#complex.r, -Z#complex.i).

-spec divide(#complex{}, #complex{}) -> #complex{}.
divide(#complex{r = A, i = B}, #complex{r = C, i = D}) ->
new((A * C + B * D) / (C * C + D * D), (B * C - A * D) / (C * C + D * D)).

-spec equal(#complex{}, #complex{}) -> #complex{}.
equal(#complex{r = A, i = B}, #complex{r = C, i = D}) ->
(erlang:abs(A - C) < 0.005) and (erlang:abs(B - D) < 0.005).

-spec exp(#complex{}) -> #complex{}.
exp(#complex{r = A, i = B}) -> mul(new(math:exp(A), 0),new(math:cos(B), math:sin(B))).

-spec imaginary(#complex{}) -> number().
imaginary(Z) -> Z#complex.i.

-spec mul(#complex{}, #complex{}) -> #complex{}.
mul(#complex{r = A, i = B}, #complex{r = C, i = D}) -> new(A * C - B * D, B * C + A * D).

-spec new(number(), number()) -> #complex{}.
new(R, I) -> #complex{r = R, i = I}.

-spec real(#complex{}) -> number().
real(Z) -> Z#complex.r.

-spec sub(#complex{}, #complex{}) -> #complex{}.
sub(#complex{r = A, i = B}, #complex{r = C, i = D}) -> new(A - C, B - D).``````