Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Nov 27 2020
·
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.

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
```

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.

Wikipedia https://en.wikipedia.org/wiki/Complex_number

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

```
%% 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))))}.
'8_add_purely_real_numbers_test_'() ->
{"Add purely real numbers",
?_assert(complex_numbers:equal(complex_numbers:new(3,
0),
complex_numbers:add(complex_numbers:new(1,
0),
complex_numbers:new(2,
0))))}.
'9_add_purely_imaginary_numbers_test_'() ->
{"Add purely imaginary numbers",
?_assert(complex_numbers:equal(complex_numbers:new(0,
3),
complex_numbers:add(complex_numbers:new(0,
1),
complex_numbers:new(0,
2))))}.
'10_add_numbers_with_real_and_imaginary_part_test_'() ->
{"Add numbers with real and imaginary "
"part",
?_assert(complex_numbers:equal(complex_numbers:new(4,
6),
complex_numbers:add(complex_numbers:new(1,
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]).
-export_type([complex/0, real/0, imaginary/0]).
-opaque complex() :: {real(), imaginary()}.
-opaque real() :: number().
-opaque imaginary() :: number().
-spec abs(complex()) -> complex().
abs({R, I}) ->
trunc(math:pow(math:pow(R, 2) + math:pow(I, 2), 0.5)).
-spec add(complex(), complex()) -> complex().
add({R1, I1}, {R2, I2}) ->
{R1 + R2, I1 + I2}.
-spec conjugate(complex()) -> complex().
conjugate({R, I}) ->
{R, -I}.
-spec divide(complex(), complex()) -> complex().
divide({R1, I1}, {R2, I2}) ->
{(R1 * R2 + I1 * I2) / (math:pow(R2, 2) + math:pow(I2, 2)),
(I1 * R2 - R1 * I2) / (math:pow(R2, 2) + math:pow(I2, 2))}.
-spec equal(complex(), complex()) -> boolean().
equal({R1, I1}, {R2, I2}) ->
erlang:abs(R1 - R2) < 0.005 andalso erlang:abs(I1 - I2) < 0.005.
-spec exp(complex()) -> complex().
exp({R, I}) ->
mul({math:exp(R), 0}, {math:cos(I), math:sin(I)}).
-spec imaginary(complex()) -> imaginary().
imaginary({_R, I}) ->
I.
-spec mul(complex(), complex()) -> complex().
mul({R1, I1}, {R2, I2}) ->
{R1 * R2 - I1 * I2, I1 * R2 + R1 * I2}.
-spec new(real(), imaginary()) -> complex().
new(R, I) ->
{R, I}.
-spec real(complex()) -> real().
real({R, _I}) ->
R.
-spec sub(complex(), complex()) -> complex().
sub({R1, I1}, {R2, I2}) ->
{R1 - R2, I1 - I2}.
```

A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments