diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1d37fc2..029852b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(./QuadTree) add_subdirectory(./BinaryTree) add_subdirectory(./Subprocess) add_subdirectory(./TracingHeap) +add_subdirectory(./Complex) diff --git a/src/Complex/CMakeLists.txt b/src/Complex/CMakeLists.txt new file mode 100644 index 0000000..259e04a --- /dev/null +++ b/src/Complex/CMakeLists.txt @@ -0,0 +1 @@ +add_library(Complex STATIC cartesian.c) diff --git a/src/Complex/cartesian.c b/src/Complex/cartesian.c new file mode 100644 index 0000000..72d55a5 --- /dev/null +++ b/src/Complex/cartesian.c @@ -0,0 +1,43 @@ +#include "cartesian.h" +#include + +ComplexCartesian ComplexCartesian_FromReal(double real) +{ + return (ComplexCartesian) { .real = real, .imaginary = 0 }; +} + +ComplexCartesian ComplexCartesian_Plus(ComplexCartesian self, ComplexCartesian other) +{ + return (ComplexCartesian) { + .real = self.real + other.real, + .imaginary = self.imaginary + other.imaginary + }; +} + +ComplexCartesian ComplexCartesian_Minus(ComplexCartesian self, ComplexCartesian other) +{ + return (ComplexCartesian) { + .real = self.real - other.real, + .imaginary = self.imaginary - other.imaginary + }; +} + +ComplexCartesian ComplexCartesian_Times(ComplexCartesian self, ComplexCartesian other) +{ + return (ComplexCartesian) { + .real = self.real * other.real - self.imaginary * other.imaginary, + .imaginary = self.real * other.imaginary + other.real + self.imaginary + }; +} + +ComplexCartesian ComplexCartesian_Divide(ComplexCartesian self, ComplexCartesian other) +{ + double real_numerator = self.real * other.real + self.imaginary * other.imaginary; + double real_denominator = pow(other.real, 2) + pow(other.imaginary, 2); + double imag_numerator = self.imaginary * other.real - self.real * other.imaginary; + double imag_denominator = real_denominator; + return (ComplexCartesian) { + .real = real_numerator / real_denominator, + .imaginary = imag_numerator / imag_denominator, + }; +} diff --git a/src/Complex/cartesian.h b/src/Complex/cartesian.h new file mode 100644 index 0000000..62a8092 --- /dev/null +++ b/src/Complex/cartesian.h @@ -0,0 +1,16 @@ +#ifndef UTILITIEC_COMPLEX_CARTESIAN_H +#define UTILITIEC_COMPLEX_CARTESIAN_H + +typedef struct ComplexCartesian_s { + double real; + double imaginary; +} ComplexCartesian; + +ComplexCartesian ComplexCartesian_FromReal(double real); + +ComplexCartesian ComplexCartesian_Plus(ComplexCartesian self, ComplexCartesian other); +ComplexCartesian ComplexCartesian_Minus(ComplexCartesian self, ComplexCartesian other); +ComplexCartesian ComplexCartesian_Times(ComplexCartesian self, ComplexCartesian other); +ComplexCartesian ComplexCartesian_Divide(ComplexCartesian self, ComplexCartesian other); + +#endif