Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complex: add commonly used complex number funcs #272

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions include/complex.h
Original file line number Diff line number Diff line change
@@ -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_ */
2 changes: 1 addition & 1 deletion math/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
61 changes: 61 additions & 0 deletions math/complex.c
Original file line number Diff line number Diff line change
@@ -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 <math.h>
#include <complex.h>


double cabs(double complex z)
{
/* FIXME: due to missing hypot() implementation, temporarily sqrt() is used */
gerard5 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading