Avatar of siebenschlaefer

siebenschlaefer's solution

to Complex Numbers in the C Track

Published at Mar 30 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.

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.

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

void test_add_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 = 3.0,.imag = 0.0 };
   complex_t actual = c_add(z1, z2);

   compare_complex(expected, actual);
}

void test_add_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 = 3.0 };
   complex_t actual = c_add(z1, z2);

   compare_complex(expected, actual);
}

void test_add_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 = 4.0,.imag = 6.0 };
   complex_t actual = c_add(z1, z2);

   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_add_purely_real_numbers);
   RUN_TEST(test_add_purely_imaginary_numbers);
   RUN_TEST(test_add_numbers_with_real_and_imaginary_part);
   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.h

#ifndef UUID_C825DD1C_9E42_49DB_92B9_5D25030156F4
#define UUID_C825DD1C_9E42_49DB_92B9_5D25030156F4

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

struct complex c_add(struct complex a, struct complex b);
struct complex c_sub(struct complex a, struct complex b);
struct complex c_mul(struct complex a, struct complex b);
struct complex c_div(struct complex a, struct complex b);
double c_abs(struct complex c);
struct complex c_conjugate(struct complex c);
double c_real(struct complex c);
double c_imag(struct complex c);
struct complex c_exp(struct complex c);

#endif //UUID_C825DD1C_9E42_49DB_92B9_5D25030156F4

src/complex_numbers.c

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

struct complex c_add(struct complex a, struct complex b)
{
    return (struct complex) {
        .real = a.real + b.real,
        .imag = a.imag + b.imag
    };
}

struct complex c_sub(struct complex a, struct complex b)
{
    return (struct complex) {
        .real = a.real - b.real,
        .imag = a.imag - b.imag
    };
}

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

struct complex c_div(struct complex a, struct complex b)
{
    double divisor = b.real * b.real + b.imag * b.imag;
    return (struct complex) {
        .real = (a.real * b.real + a.imag * b.imag) / divisor,
        .imag = (a.imag * b.real - a.real * b.imag) / divisor
    };
}

double c_abs(struct complex c)
{
    return hypot(c.real, c.imag);
}

struct complex c_conjugate(struct complex c)
{
    return (struct complex) {
        .real = c.real,
        .imag = -c.imag
    };
}

double c_real(struct complex c)
{
    return c.real;
}

double c_imag(struct complex c)
{
    return c.imag;
}

struct complex c_exp(struct complex c)
{
    double exp_real = exp(c.real);
    return (struct complex) {
        .real = exp_real * cos(c.imag),
        .imag = exp_real * sin(c.imag)
    };
}

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?