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

Published at Dec 23 2019
·
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.

```
%% Based on canonical data version 1.3.0
%% https://github.com/exercism/problem-specifications/raw/master/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'() ->
?assert(1 ==
complex_numbers:real(complex_numbers:new(1, 0))).
'2_real_part_of_a_purely_imaginary_number_test'() ->
?assert(0 ==
complex_numbers:real(complex_numbers:new(0, 1))).
'3_real_part_of_a_number_with_real_and_imaginary_part_test'() ->
?assert(1 ==
complex_numbers:real(complex_numbers:new(1, 2))).
'4_imaginary_part_of_a_purely_real_number_test'() ->
?assert(0 ==
complex_numbers:imaginary(complex_numbers:new(1, 0))).
'5_imaginary_part_of_a_purely_imaginary_number_test'() ->
?assert(1 ==
complex_numbers:imaginary(complex_numbers:new(0, 1))).
'6_imaginary_part_of_a_number_with_real_and_imaginary_part_test'() ->
?assert(2 ==
complex_numbers:imaginary(complex_numbers:new(1, 2))).
'7_imaginary_unit_test'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?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'() ->
?assert(5 ==
complex_numbers:abs(complex_numbers:new(5, 0))).
'21_absolute_value_of_a_negative_purely_real_number_test'() ->
?assert(5 ==
complex_numbers:abs(complex_numbers:new(-5, 0))).
'22_absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part_test'() ->
?assert(5 ==
complex_numbers:abs(complex_numbers:new(0, 5))).
'23_absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part_test'() ->
?assert(5 ==
complex_numbers:abs(complex_numbers:new(0, -5))).
'24_absolute_value_of_a_number_with_real_and_imaginary_part_test'() ->
?assert(5 ==
complex_numbers:abs(complex_numbers:new(3, 4))).
'25_conjugate_a_purely_real_number_test'() ->
?assert(complex_numbers:equal(complex_numbers:new(5, 0),
complex_numbers:conjugate(complex_numbers:new(5,
0)))).
'26_conjugate_a_purely_imaginary_number_test'() ->
?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'() ->
?assert(complex_numbers:equal(complex_numbers:new(1,
-1),
complex_numbers:conjugate(complex_numbers:new(1,
1)))).
'28_eulers_identityformula_test'() ->
?assert(complex_numbers:equal(complex_numbers:new(-1,
0),
complex_numbers:exp(complex_numbers:new(0,
3.14159265358979311600)))).
'29_exponential_of_0_test'() ->
?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'() ->
?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'() ->
?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]).
-define(TRESHOLD, 0.005).
abs({R, I}) -> math:sqrt(math:pow(R, 2) + math:pow(I, 2)).
add({R1, I1}, {R2, I2}) -> {R1 + R2, I1 + I2}.
conjugate({R, I}) -> {R, -1 * I}.
divide({R1, I1}, {R2, I2}) ->
Divisor = math:pow(R2, 2) + math:pow(I2, 2),
{(R1 * R2 + I1 * I2) / Divisor, (I1 * R2 - R1 * I2) / Divisor}.
equal({R1, I1}, {R2, I2}) -> (erlang:abs(R1 - R2) < ?TRESHOLD) andalso (erlang:abs(I1 - I2) < ?TRESHOLD).
exp({R, I}) -> {math:exp(R) * math:cos(I), math:exp(R) * math:sin(I)}.
imaginary({_, I}) -> I.
mul({R1, I1}, {R2, I2}) -> {R1 * R2 - I1 * I2, R2 * I1 + R1 * I2}.
new(R, I) -> {R, I}.
real({R, _}) -> R.
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