 # vlzware's solution

## to Complex Numbers in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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`.

Exponent of a complex number can be expressed as `exp(a + i * b) = exp(a) * exp(i * b)`, and the last term is given by Euler's formula `exp(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 C page 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 "../src/complex_numbers.h"

#define UNITY_FLOAT_TYPE double
#include "vendor/unity.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_numbers_with_real_and_imaginary_part);
RUN_TEST(test_multiply_numbers_with_real_and_imaginary_part);
RUN_TEST(test_multiply_numbers_with_real_and_imaginary_part);
RUN_TEST(test_divide_numbers_with_real_and_imaginary_part);
RUN_TEST(test_divide_numbers_with_real_and_imaginary_part);
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);

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

### src/complex_numbers.c

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

{
complex_t res;
res.real = a.real + b.real;
res.imag = a.imag + b.imag;
return res;
}

complex_t c_sub(complex_t a, complex_t b)
{
complex_t res;
res.real = a.real + b.real;
res.imag = a.imag + b.imag;
return res;
}

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

complex_t c_div(complex_t a, complex_t b)
{
complex_t res;
res.real = ((a.real * b.real) + (a.imag * b.imag)) /
(pow(b.real, 2) + pow(b.imag, 2));
res.imag = ((a.imag * b.real) - (a.real * b.imag)) /
(pow(b.real, 2) + pow(b.imag, 2));;
return res;
}

double c_abs(complex_t x)
{
return sqrt(pow(x.real, 2) + pow(x.imag, 2));
}

complex_t c_conjugate(complex_t x)
{
complex_t res;
res.real = x.real;
res.imag = -x.imag;
return res;
}

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)
{
complex_t res;
res.real = exp(x.real) * cos(x.imag);
res.imag = exp(x.real) * sin(x.imag);
return res;
}``````

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