From 103362e12ecede3f6a4a70ca01be908299e189b6 Mon Sep 17 00:00:00 2001 From: vvaucoul Date: Sun, 28 Jul 2024 02:01:51 +0200 Subject: [PATCH] [Update] - Add math complex numbers --- Hephaistos.code-workspace | 3 +- Hephaistos/include/math.h | 45 +++++++++++----- Hephaistos/include/string.h | 2 +- Hephaistos/include/time.h | 6 +-- Hephaistos/src/math/complex.c | 91 ++++++++++++++++++++++++++++++++ Hephaistos/src/{ => math}/math.c | 0 Hephaistos/src/string.c | 2 +- Hephaistos/src/time.c | 33 +++++++++--- workflows/workflows.h | 4 +- 9 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 Hephaistos/src/math/complex.c rename Hephaistos/src/{ => math}/math.c (100%) diff --git a/Hephaistos.code-workspace b/Hephaistos.code-workspace index b39e596..368bd88 100644 --- a/Hephaistos.code-workspace +++ b/Hephaistos.code-workspace @@ -27,7 +27,8 @@ "math.h": "c", "array.h": "c", "ctype.h": "c", - "limits.h": "c" + "limits.h": "c", + "stdint.h": "c" } } } \ No newline at end of file diff --git a/Hephaistos/include/math.h b/Hephaistos/include/math.h index 4bc3614..093d5db 100644 --- a/Hephaistos/include/math.h +++ b/Hephaistos/include/math.h @@ -6,67 +6,72 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 23:21:41 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 21:59:38 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 02:01:32 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef _MATH_H #define _MATH_H +/** + * @file math.h + * @brief Header file for mathematical operations. + */ + #include /** * @brief Return the absolute value of a number */ inline int abs(int nb) { - return (nb < 0 ? -nb : nb); + return (nb < 0 ? -nb : nb); } /** * @brief Return the absolute value of a number */ inline float fabs(float nb) { - return (nb < 0 ? -nb : nb); + return (nb < 0 ? -nb : nb); } /** * @brief Return the maximum of two numbers */ inline int max(int a, int b) { - return (a > b ? a : b); + return (a > b ? a : b); } /** * @brief Return the minimum of two numbers */ inline int min(int a, int b) { - return (a < b ? a : b); + return (a < b ? a : b); } /** * @brief Round a floating-point number to the nearest integer */ inline int round(float nb) { - return (nb >= 0) ? (int)(nb + 0.5) : -(int)(nb - 0.5); + return (nb >= 0) ? (int)(nb + 0.5) : -(int)(nb - 0.5); } /** * @brief Calculate the power of a number */ inline float pow(float nb, float power) { - float res = 1; + float res = 1; - for (uint32_t i = 0; i < (uint32_t)power; i++) { - res *= nb; - } - return res; + for (uint32_t i = 0; i < (uint32_t)power; i++) { + res *= nb; + } + return res; } /** * @brief Calculate the square root of a number */ inline float sqrt(float nb) { - return pow(nb, 0.5); + return pow(nb, 0.5); } extern float ln(float x); @@ -78,4 +83,20 @@ extern int clamp(int value, int min, int max); extern float hypot(float x, float y); extern int signum(float x); +/** + * @struct Complex + * @brief Represents a complex number with real and imaginary parts. + */ +typedef struct { + float real; + float imag; +} Complex; + +Complex complex_create(float real, float imag); +Complex complex_add(Complex a, Complex b); +Complex complex_sub(Complex a, Complex b); +Complex complex_mul(Complex a, Complex b); +Complex complex_div(Complex a, Complex b); +Complex complex_conj(Complex a); + #endif /* !_MATH_H */ \ No newline at end of file diff --git a/Hephaistos/include/string.h b/Hephaistos/include/string.h index a23fc7b..9bb0e62 100644 --- a/Hephaistos/include/string.h +++ b/Hephaistos/include/string.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 11:31:06 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 22:14:09 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:52:14 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/Hephaistos/include/time.h b/Hephaistos/include/time.h index 235bec0..f2a2a1b 100644 --- a/Hephaistos/include/time.h +++ b/Hephaistos/include/time.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/21 14:10:07 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 16:12:48 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:55:15 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ typedef struct s_counter { uint32_t end; } counter_t; -extern void counter_start(counter_t *counter); -extern void counter_stop(counter_t *counter); +extern int counter_start(counter_t *counter); +extern int counter_stop(counter_t *counter); extern uint32_t counter_get_start(counter_t *counter); extern uint32_t counter_get_end(counter_t *counter); extern uint32_t counter_get_time(counter_t *counter); diff --git a/Hephaistos/src/math/complex.c b/Hephaistos/src/math/complex.c new file mode 100644 index 0000000..798d734 --- /dev/null +++ b/Hephaistos/src/math/complex.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* complex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vvaucoul +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/28 01:58:36 by vvaucoul #+# #+# */ +/* Updated: 2024/07/28 02:01:33 by vvaucoul ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +/** + * @brief Create a complex number + * @param real The real part of the complex number + * @param imag The imaginary part of the complex number + * @return The created complex number + */ +Complex complex_create(float real, float imag) { + Complex c; + c.real = real; + c.imag = imag; + return c; +} + +/** + * @brief Add two complex numbers + * @param a The first complex number + * @param b The second complex number + * @return The sum of the two complex numbers + */ +Complex complex_add(Complex a, Complex b) { + Complex result; + result.real = a.real + b.real; + result.imag = a.imag + b.imag; + return result; +} + +/** + * @brief Subtract one complex number from another + * @param a The first complex number + * @param b The second complex number + * @return The difference of the two complex numbers + */ +Complex complex_sub(Complex a, Complex b) { + Complex result; + result.real = a.real - b.real; + result.imag = a.imag - b.imag; + return result; +} + +/** + * @brief Multiply two complex numbers + * @param a The first complex number + * @param b The second complex number + * @return The product of the two complex numbers + */ +Complex complex_mul(Complex a, Complex b) { + Complex result; + result.real = a.real * b.real - a.imag * b.imag; + result.imag = a.real * b.imag + a.imag * b.real; + return result; +} + +/** + * @brief Divide one complex number by another + * @param a The numerator complex number + * @param b The denominator complex number + * @return The quotient of the two complex numbers + */ +Complex complex_div(Complex a, Complex b) { + Complex result; + float denominator = b.real * b.real + b.imag * b.imag; + result.real = (a.real * b.real + a.imag * b.imag) / denominator; + result.imag = (a.imag * b.real - a.real * b.imag) / denominator; + return result; +} + +/** + * @brief Calculate the conjugate of a complex number + * @param a The complex number + * @return The conjugate of the complex number + */ +Complex complex_conj(Complex a) { + Complex result; + result.real = a.real; + result.imag = -a.imag; + return result; +} \ No newline at end of file diff --git a/Hephaistos/src/math.c b/Hephaistos/src/math/math.c similarity index 100% rename from Hephaistos/src/math.c rename to Hephaistos/src/math/math.c diff --git a/Hephaistos/src/string.c b/Hephaistos/src/string.c index 753c2f5..64f37f4 100644 --- a/Hephaistos/src/string.c +++ b/Hephaistos/src/string.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 11:55:48 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 17:40:13 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:52:10 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/Hephaistos/src/time.c b/Hephaistos/src/time.c index 8292a26..55306b3 100644 --- a/Hephaistos/src/time.c +++ b/Hephaistos/src/time.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/21 14:11:09 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 17:23:51 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:56:10 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,13 @@ * * @param counter Pointer to the counter structure to be started. */ -void counter_start(counter_t *counter) { - assert(counter != NULL); +int counter_start(counter_t *counter) { + if (counter == NULL) { + return -EINVAL; + } + counter->start = pit_get_ticks(); + return 0; } /** @@ -29,9 +33,13 @@ void counter_start(counter_t *counter) { * * @param counter Pointer to the counter structure to be stopped. */ -void counter_stop(counter_t *counter) { - assert(counter != NULL); +int counter_stop(counter_t *counter) { + if (counter == NULL) { + return -EINVAL; + } + counter->end = pit_get_ticks(); + return 0; } /** @@ -41,7 +49,10 @@ void counter_stop(counter_t *counter) { * @return The start time of the counter. */ uint32_t counter_get_start(counter_t *counter) { - assert(counter != NULL); + if (counter == NULL) { + return -EINVAL; + } + return counter->start; } @@ -52,7 +63,10 @@ uint32_t counter_get_start(counter_t *counter) { * @return The end time of the counter. */ uint32_t counter_get_end(counter_t *counter) { - assert(counter != NULL); + if (counter == NULL) { + return -EINVAL; + } + return counter->end; } @@ -63,6 +77,9 @@ uint32_t counter_get_end(counter_t *counter) { * @return The elapsed time between the start and end times of the counter. */ uint32_t counter_get_time(counter_t *counter) { - assert(counter != NULL); + if (counter == NULL) { + return -EINVAL; + } + return counter->end - counter->start; } \ No newline at end of file diff --git a/workflows/workflows.h b/workflows/workflows.h index 8773ec4..1ac0487 100644 --- a/workflows/workflows.h +++ b/workflows/workflows.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/09 14:55:33 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 19:07:58 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:49:47 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,4 @@ extern int workflow_hephaistos_a_avl(void); extern int hephaistos_workflow(void); -extern void hephaistos_off_tests(void); - #endif /* !__H_WORKFLOWS_H */ \ No newline at end of file