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.180 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.179 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.178 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.177 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.176 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.175 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.174 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.173 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.172 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.171 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.170 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.169 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.167 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.166 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.165 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.164 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.163 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.162 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.161 tracks/c/exercises/complex-numbers/src/example.c
trackler-2.2.1.160 tracks/c/exercises/complex-numbers/src/example.c