🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ABigFatJerk

ABigFatJerk's solution

to Complex Numbers in the Erlang Track

Published at Jan 02 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.

Source

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

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))))}.

'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]).


abs(Complex) -> R = real(Complex), I = imaginary(Complex), math:sqrt(math:pow(R,2) + math:pow(I,2)).

unpack(Complex1, Complex2) -> {real(Complex1), real(Complex2), imaginary(Complex1), imaginary(Complex2)}.

add(Complex1, Complex2) -> {R1, R2, I1, I2} = unpack(Complex1, Complex2), 
                           R = R1 + R2, 
                           I = I1 + I2, 
                           new(R, I).

sub(Complex1, Complex2) -> {R1, R2, I1, I2} = unpack(Complex1, Complex2), 
                           R = R1 - R2, 
                           I = I1 - I2, 
                           new(R, I).

mul(Complex1, Complex2) -> {R1, R2, I1, I2} = unpack(Complex1, Complex2), 
                           R = R1 * R2 - I1 * I2, 
                           I = I1 * R2 + R1 * I2, 
                           new(R, I).

divide(Complex1, Complex2) -> {R1, R2, I1, I2} = unpack(Complex1, Complex2),
                              Denom = (math:pow(R2, 2) + math:pow(I2, 2)), 
                              R = (R1 * R2 + I1 * I2)/Denom, 
                              I = (I1 * R2 - R1 * I2)/Denom,
                              new(R, I).

equal(Complex1, Complex2) -> {R1, R2, I1, I2} = unpack(Complex1, Complex2),
                             equal_enough(R1, R2) and equal_enough(I1, I2).

equal_enough(Num1, Num2) -> erlang:abs(Num1 - Num2) < 0.005.

exp(Complex) -> R1 = real(Complex), I1 = imaginary(Complex),
                Scalar = math:exp(R1),
                R = math:cos(I1),
                I = math:sin(I1),
                new(Scalar*R, Scalar*I).

conjugate(Complex) -> R = real(Complex), I = -1 * imaginary(Complex), new(R, I).

new(R, I) -> {complex, R, I}.
imaginary({complex, _, I}) -> I.
real({complex, R, _I}) -> R.

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?