diff --git a/include/complex.h b/include/complex.h index e69de29b..9c7a3427 100644 --- a/include/complex.h +++ b/include/complex.h @@ -0,0 +1,255 @@ +/* + * Phoenix-RTOS + * + * libphoenix + * + * mathematical functions on complex numbers + * + * Copyright 2023 Phoenix Systems + * Author: Gerard Swiderski + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#ifndef _COMPLEX_H_ +#define _COMPLEX_H_ + +/* define complex numbers specifers */ +#define complex _Complex +#define _Complex_I 1.0fi +#define I _Complex_I + + +#ifdef __cplusplus +extern "C" { +#endif + + +static inline double creal(double complex z) +{ + return (__real__(z)); +} + + +static inline float crealf(float complex z) +{ + return (__real__(z)); +} + + +static inline double cimag(double complex z) +{ + return (__imag__(z)); +} + + +static inline float cimagf(float complex z) +{ + return (__imag__(z)); +} + + +static inline double complex conj(double complex z) +{ + return __builtin_complex((__real__(z)), -(__imag__(z))); +} + + +static inline float complex conjf(float complex z) +{ + return __builtin_complex((__real__(z)), -(__imag__(z))); +} + + +double cabs(double complex); + + +float cabsf(float complex); + + +double carg(double complex); + + +float cargf(float complex); + + +double complex cexp(double complex); + + +float complex cexpf(float complex); + + +/* + * TODO: functions not yet implemented + */ + +long double cabsl(long double complex); + + +double complex cacos(double complex); + + +float complex cacosf(float complex); + + +double complex cacosh(double complex); + + +float complex cacoshf(float complex); + + +long double complex cacoshl(long double complex); + + +long double complex cacosl(long double complex); + + +long double cargl(long double complex); + + +double complex casin(double complex); + + +float complex casinf(float complex); + + +double complex casinh(double complex); + + +float complex casinhf(float complex); + + +long double complex casinhl(long double complex); + + +long double complex casinl(long double complex); + + +double complex catan(double complex); + + +float complex catanf(float complex); + + +double complex catanh(double complex); + + +float complex catanhf(float complex); + + +long double complex catanhl(long double complex); + + +long double complex catanl(long double complex); + + +double complex ccos(double complex); + + +float complex ccosf(float complex); + + +double complex ccosh(double complex); + + +float complex ccoshf(float complex); + + +long double complex ccoshl(long double complex); + + +long double complex ccosl(long double complex); + + +long double complex cexpl(long double complex); + + +long double cimagl(long double complex); + + +double complex clog(double complex); + + +float complex clogf(float complex); + + +long double complex clogl(long double complex); + + +long double complex conjl(long double complex); + + +double complex cpow(double complex, double complex); + + +float complex cpowf(float complex, float complex); + + +long double complex cpowl(long double complex, long double complex); + + +double complex cproj(double complex); + + +float complex cprojf(float complex); + + +long double complex cprojl(long double complex); + + +long double creall(long double complex); + + +double complex csin(double complex); + + +float complex csinf(float complex); + + +double complex csinh(double complex); + + +float complex csinhf(float complex); + + +long double complex csinhl(long double complex); + + +long double complex csinl(long double complex); + + +double complex csqrt(double complex); + + +float complex csqrtf(float complex); + + +long double complex csqrtl(long double complex); + + +double complex ctan(double complex); + + +float complex ctanf(float complex); + + +double complex ctanh(double complex); + + +float complex ctanhf(float complex); + + +long double complex ctanhl(long double complex); + + +long double complex ctanl(long double complex); + + +#ifdef __cplusplus +} +#endif + + +#endif /* end of _COMPLEX_H_ */ diff --git a/math/Makefile b/math/Makefile index 3efe3875..f8f1a967 100644 --- a/math/Makefile +++ b/math/Makefile @@ -4,4 +4,4 @@ # Copyright 2012-2015, 2020 Phoenix Systems # -OBJS += $(addprefix $(PREFIX_O)math/, trig.o power.o exp.o common.o hyper.o) +OBJS += $(addprefix $(PREFIX_O)math/, trig.o power.o exp.o common.o complex.o hyper.o) diff --git a/math/complex.c b/math/complex.c new file mode 100644 index 00000000..c12d4e60 --- /dev/null +++ b/math/complex.c @@ -0,0 +1,61 @@ +/* + * Phoenix-RTOS + * + * libphoenix + * + * mathematical functions on complex numbers + * + * Copyright 2023 Phoenix Systems + * Author: Gerard Swiderski + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include +#include + + +double cabs(double complex z) +{ + /* FIXME: due to missing hypot() implementation, temporarily sqrt() is used */ + return sqrt((creal(z) * creal(z)) + (cimag(z) * cimag(z))); +} + + +float cabsf(float complex z) +{ + /* FIXME: due to missing hypot() implementation, temporarily sqrtf() is used */ + return sqrtf((crealf(z) * crealf(z)) + (cimagf(z) * cimagf(z))); +} + + +double carg(double complex z) +{ + return atan2(cimag(z), creal(z)); +} + + +float cargf(float complex z) +{ + return atan2f(cimagf(z), crealf(z)); +} + + +double complex cexp(double complex z) +{ + double re = creal(z); + double im = cimag(z); + double radius = exp(re); + return radius * cos(im) + radius * sin(im) * 1.0i; +} + + +float complex cexpf(float complex z) +{ + float re = crealf(z); + float im = cimagf(z); + float radius = expf(re); + return radius * cosf(im) + radius * sinf(im) * 1.0fi; +}