 # petrem's solution

## to Complex Numbers in the C Track

Published at Jun 16 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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.

## Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Submitting Incomplete Solutions

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

### test_complex_numbers.c

``````#include "vendor/unity.h"
#include "../src/complex_numbers.h"
#include <math.h>

#define PI acos(-1)
#define E exp(1)

void compare_complex(complex_t lhs, complex_t rhs)
{
TEST_ASSERT_EQUAL_FLOAT(lhs.real, rhs.real);
TEST_ASSERT_EQUAL_FLOAT(lhs.imag, rhs.imag);
}

void test_imaginary_unit(void)
{
complex_t z = {.real = 0.0,.imag = 1.0 };

complex_t expected = {.real = -1.0,.imag = 0.0 };
complex_t actual = c_mul(z, z);

compare_complex(expected, actual);
}

{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 0.0 };
complex_t z2 = {.real = 2.0,.imag = 0.0 };

complex_t expected = {.real = 3.0,.imag = 0.0 };

compare_complex(expected, actual);
}

{
TEST_IGNORE();
complex_t z1 = {.real = 0.0,.imag = 1.0 };
complex_t z2 = {.real = 0.0,.imag = 2.0 };

complex_t expected = {.real = 0.0,.imag = 3.0 };

compare_complex(expected, actual);
}

{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 2.0 };
complex_t z2 = {.real = 3.0,.imag = 4.0 };

complex_t expected = {.real = 4.0,.imag = 6.0 };

compare_complex(expected, actual);
}

void test_subtract_purely_real_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 0.0 };
complex_t z2 = {.real = 2.0,.imag = 0.0 };

complex_t expected = {.real = -1.0,.imag = 0.0 };
complex_t actual = c_sub(z1, z2);

compare_complex(expected, actual);
}

void test_subtract_purely_imaginary_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 0.0,.imag = 1.0 };
complex_t z2 = {.real = 0.0,.imag = 2.0 };

complex_t expected = {.real = 0.0,.imag = -1.0 };
complex_t actual = c_sub(z1, z2);

compare_complex(expected, actual);
}

void test_subtract_numbers_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 2.0 };
complex_t z2 = {.real = 3.0,.imag = 4.0 };

complex_t expected = {.real = -2.0,.imag = -2.0 };
complex_t actual = c_sub(z1, z2);

compare_complex(expected, actual);
}

void test_multiply_purely_real_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 0.0 };
complex_t z2 = {.real = 2.0,.imag = 0.0 };

complex_t expected = {.real = 2.0,.imag = 0.0 };
complex_t actual = c_mul(z1, z2);

compare_complex(expected, actual);
}

void test_multiply_purely_imaginary_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 0.0,.imag = 1.0 };
complex_t z2 = {.real = 0.0,.imag = 2.0 };

complex_t expected = {.real = -2.0,.imag = 0.0 };
complex_t actual = c_mul(z1, z2);

compare_complex(expected, actual);
}

void test_multiply_numbers_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 2.0 };
complex_t z2 = {.real = 3.0,.imag = 4.0 };

complex_t expected = {.real = -5.0,.imag = 10.0 };
complex_t actual = c_mul(z1, z2);

compare_complex(expected, actual);
}

void test_divide_purely_real_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 0.0 };
complex_t z2 = {.real = 2.0,.imag = 0.0 };

complex_t expected = {.real = 0.5,.imag = 0.0 };
complex_t actual = c_div(z1, z2);

compare_complex(expected, actual);
}

void test_divide_purely_imaginary_numbers(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 0.0,.imag = 1.0 };
complex_t z2 = {.real = 0.0,.imag = 2.0 };

complex_t expected = {.real = 0.5,.imag = 0.0 };
complex_t actual = c_div(z1, z2);

compare_complex(expected, actual);
}

void test_divide_numbers_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z1 = {.real = 1.0,.imag = 2.0 };
complex_t z2 = {.real = 3.0,.imag = 4.0 };

complex_t expected = {.real = 0.44,.imag = 0.08 };
complex_t actual = c_div(z1, z2);

compare_complex(expected, actual);
}

void test_abs_of_a_positive_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 5.0,.imag = 0.0 };

double expected = 5.0;
double actual = c_abs(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_abs_of_a_negative_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = -5.0,.imag = 0.0 };

double expected = 5.0;
double actual = c_abs(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_abs_of_a_purely_imaginary_number_with_positive_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = 5.0 };

double expected = 5.0;
double actual = c_abs(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_abs_of_a_purely_imaginary_number_with_negative_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = -5.0 };

double expected = 5.0;
double actual = c_abs(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_abs_of_a_number_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 3.0,.imag = 4.0 };

double expected = 5.0;
double actual = c_abs(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_complex_conjugate_of_a_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 5.0,.imag = 0.0 };

complex_t expected = {.real = 5.0,.imag = 0.0 };
complex_t actual = c_conjugate(z);

compare_complex(expected, actual);
}

void test_complex_conjugate_of_a_purely_imaginary_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = 5.0 };

complex_t expected = {.real = 0.0,.imag = -5.0 };
complex_t actual = c_conjugate(z);

compare_complex(expected, actual);
}

void test_complex_conjugate_of_a_number_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 1.0 };

complex_t expected = {.real = 1.0,.imag = -1.0 };
complex_t actual = c_conjugate(z);

compare_complex(expected, actual);
}

void test_real_part_of_a_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 0.0 };

double expected = 1.0;
double actual = c_real(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_real_part_of_a_purely_imaginary_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = 1.0 };

double expected = 0.0;
double actual = c_real(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_real_part_of_a_number_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 2.0 };

double expected = 1.0;
double actual = c_real(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_imaginary_part_of_a_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 0.0 };

double expected = 0.0;
double actual = c_imag(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_imaginary_part_of_a_purely_imaginary_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = 1.0 };

double expected = 1.0;
double actual = c_imag(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_imaginary_part_of_a_number_with_real_and_imaginary_part(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 2.0 };

double expected = 2.0;
double actual = c_imag(z);

TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}

void test_eulers_identity(void)
{
TEST_IGNORE();
complex_t z = {.real = 0.0,.imag = PI };

complex_t expected = {.real = -1.0,.imag = 0.0 };
complex_t actual = c_exp(z);

TEST_ASSERT_FLOAT_WITHIN(1e-10, expected.real, actual.real);
TEST_ASSERT_FLOAT_WITHIN(1e-10, expected.imag, actual.imag);
}

void test_exponential_of_zero(void)
{
TEST_IGNORE();
complex_t zero = {.real = 0.0,.imag = 0.0 };

complex_t expected = {.real = 1.0,.imag = 0.0 };
complex_t actual = c_exp(zero);

compare_complex(expected, actual);
}

void test_exponential_of_a_purely_real_number(void)
{
TEST_IGNORE();
complex_t z = {.real = 1.0,.imag = 0.0 };

complex_t expected = {.real = E,.imag = 0.0 };
complex_t actual = c_exp(z);

compare_complex(expected, actual);
}

int main(void)
{
UnityBegin("test/test_complex_numbers.c");

RUN_TEST(test_imaginary_unit);
RUN_TEST(test_subtract_purely_real_numbers);
RUN_TEST(test_subtract_purely_imaginary_numbers);
RUN_TEST(test_subtract_numbers_with_real_and_imaginary_part);
RUN_TEST(test_multiply_purely_real_numbers);
RUN_TEST(test_multiply_purely_imaginary_numbers);
RUN_TEST(test_multiply_numbers_with_real_and_imaginary_part);
RUN_TEST(test_divide_purely_real_numbers);
RUN_TEST(test_divide_purely_imaginary_numbers);
RUN_TEST(test_divide_numbers_with_real_and_imaginary_part);
RUN_TEST(test_abs_of_a_positive_purely_real_number);
RUN_TEST(test_abs_of_a_negative_purely_real_number);
RUN_TEST(test_abs_of_a_purely_imaginary_number_with_positive_imaginary_part);
RUN_TEST(test_abs_of_a_purely_imaginary_number_with_negative_imaginary_part);
RUN_TEST(test_abs_of_a_number_with_real_and_imaginary_part);
RUN_TEST(test_complex_conjugate_of_a_purely_real_number);
RUN_TEST(test_complex_conjugate_of_a_purely_imaginary_number);
RUN_TEST(test_complex_conjugate_of_a_number_with_real_and_imaginary_part);
RUN_TEST(test_real_part_of_a_purely_real_number);
RUN_TEST(test_real_part_of_a_purely_imaginary_number);
RUN_TEST(test_real_part_of_a_number_with_real_and_imaginary_part);
RUN_TEST(test_imaginary_part_of_a_purely_real_number);
RUN_TEST(test_imaginary_part_of_a_purely_imaginary_number);
RUN_TEST(test_imaginary_part_of_a_number_with_real_and_imaginary_part);
RUN_TEST(test_eulers_identity);
RUN_TEST(test_exponential_of_zero);
RUN_TEST(test_exponential_of_a_purely_real_number);

return UnityEnd();
}``````

### src/complex_numbers.c

``````#include "complex_numbers.h"
#include <math.h>

static inline complex_t make_complex(double real, double imag) {
complex_t result  = {.real = real, .imag = imag};
return result;
}

static inline double square(double x) {
return x * x;
}

static inline complex_t c_mul_scalar(complex_t x, double m) {
return make_complex(x.real * m, x.imag * m);
}

{
return make_complex(a.real + b.real, a.imag + b.imag);
}

complex_t c_sub(complex_t a, complex_t b)
{
return make_complex(a.real - b.real, a.imag - b.imag);
}

complex_t c_mul(complex_t a, complex_t b)
{
double real, imag;
real = a.real * b.real - a.imag * b.imag;
imag = a.imag * b.real + a.real * b.imag;
return make_complex(real, imag);
}

complex_t c_div(complex_t a, complex_t b)
{
double real, imag, denominator;
denominator = square(b.real) + square(b.imag);
real = (a.real * b.real + a.imag * b.imag) / denominator;
imag = (a.imag * b.real - a.real * b.imag) / denominator;
return make_complex(real, imag);
}

double c_abs(complex_t x)
{
return sqrt(square(x.real) + square(x.imag));
}

complex_t c_conjugate(complex_t x)
{
return make_complex(x.real, -x.imag);
}

double c_real(complex_t x)
{
return x.real;
}

double c_imag(complex_t x)
{
return x.imag;
}

complex_t c_exp(complex_t x)
{
return c_mul_scalar(make_complex(cos(x.imag), sin(x.imag)), exp(x.real));
}``````

### src/complex_numbers.h

``````#ifndef _COMPLEX_NUMBERS_H_
#define _COMPLEX_NUMBERS_H_

typedef struct {
double real;
double imag;
} complex_t;

complex_t c_sub(complex_t a, complex_t b);
complex_t c_mul(complex_t a, complex_t b);
complex_t c_div(complex_t a, complex_t b);
double c_abs(complex_t x);
complex_t c_conjugate(complex_t x);
double c_real(complex_t x);
double c_imag(complex_t x);
complex_t c_exp(complex_t x);

#endif``````