Simple implementation of basic operations on complex numbers
This commit is contained in:
parent
ccbe41c269
commit
bc49570d12
4 changed files with 61 additions and 0 deletions
|
@ -19,3 +19,4 @@ add_subdirectory(./QuadTree)
|
||||||
add_subdirectory(./BinaryTree)
|
add_subdirectory(./BinaryTree)
|
||||||
add_subdirectory(./Subprocess)
|
add_subdirectory(./Subprocess)
|
||||||
add_subdirectory(./TracingHeap)
|
add_subdirectory(./TracingHeap)
|
||||||
|
add_subdirectory(./Complex)
|
||||||
|
|
1
src/Complex/CMakeLists.txt
Normal file
1
src/Complex/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
add_library(Complex STATIC cartesian.c)
|
43
src/Complex/cartesian.c
Normal file
43
src/Complex/cartesian.c
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include "cartesian.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
16
src/Complex/cartesian.h
Normal file
16
src/Complex/cartesian.h
Normal file
|
@ -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
|
Loading…
Reference in a new issue