Skip to content

Commit

Permalink
complex: add commonly used complex number funcs
Browse files Browse the repository at this point in the history
Provide required complex.h function prototypes and commonly used
functions.

`cabs()` and `cabsf()` implementations, to calculate distance,
temporarily uses `sqrt()` instead of `hypot()` which is required
but missing now.

JIRA: RTOS-528
  • Loading branch information
gerard5 committed Jul 21, 2023
1 parent d26c40e commit 20ad323
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 1 deletion.
271 changes: 271 additions & 0 deletions include/complex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/*
* 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)
{
union {
double complex z;
struct {
double re;
double im;
} part;
} u = { .z = z };
u.part.im = -u.part.im;
return u.z;
}


static inline float complex conjf(float complex z)
{
union {
float complex z;
struct {
float re;
float im;
} part;
} u = { .z = z };
u.part.im = -u.part.im;
return u.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 */
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;
}

0 comments on commit 20ad323

Please sign in to comment.