Sha256: ff5506f9091961c6f3ffda07c9ddcebdcfac3e4255234adbc7b1344d11c6400a

Contents?: true

Size: 1.43 KB

Versions: 93

Compression:

Stored size: 1.43 KB

Contents

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

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

complex_t c_add(const complex_t a, const complex_t b)
{
   complex_t result;
   result.real = a.real + b.real;
   result.imag = a.imag + b.imag;

   return result;
}

complex_t c_sub(const complex_t a, const complex_t b)
{
   complex_t result;
   result.real = a.real - b.real;
   result.imag = a.imag - b.imag;

   return result;
}

complex_t c_mul(const complex_t a, const complex_t b)
{
   complex_t result;
   result.real = a.real * b.real - a.imag * b.imag;
   result.imag = a.imag * b.real + a.real * b.imag;

   return result;
}

complex_t c_div(const complex_t a, const complex_t b)
{
   double denominator = square(b.real) + square(b.imag);

   complex_t result;
   result.real = (a.real * b.imag + a.imag * b.real) / denominator;
   result.imag = (a.imag * b.real - a.real * b.imag) / denominator;

   return result;
}

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

complex_t c_conjugate(const complex_t x)
{
   complex_t result;
   result.real = x.real;
   result.imag = -(x.imag);

   return result;
}

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

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

complex_t c_exp(const complex_t x)
{
   double real_exp = exp(x.real);

   complex_t result;
   result.real = real_exp * cos(x.imag);
   result.imag = real_exp * sin(x.imag);

   return result;
}

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.138 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.137 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.136 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.135 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.134 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.133 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.132 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.131 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.130 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.129 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.128 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.127 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.126 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.125 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.124 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.123 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.122 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.121 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.120 tracks/c/exercises/complex-numbers/src/example.c