Mbed studio does not support exp of complex double values?

Hi how do I complex exponential function to work after using the #include <complex> library.
complex exp

where ex is a complex<double> data type
how do i resolve this issue, are there any libraries I can download, or etc ?

Hello,

The following should work:

#include "mbed.h"
#include <complex>

int main()
{
    int n = 1;
    int k = 1;
    double x[3] = {1.0, 2.0, 3.0};
    complex<double> ex(1.0, 2.0);
    complex<double> mult = complex<double>(x[n]) * exp(ex * complex<double>(n) * complex<double>(k));
    printf("mult = (%5.2f, %5.2f)\r\n", mult.real(), mult.imag());
}

Result:

mult = (-2.26,  4.94)

Hi
Thanks for the swift reply.
I have tried to cast the variables as complex variables, but the IDE still has the red squiggly lines as seen here:

It seems like the IDE cant find the complex library definition of the exp(complex<double>..) function …
help regarding this is much appreciated
thanks

Sorry I used mbed-cli (I don’t have the Mbed Studio IDE installed on the machine I’m using right now). The complex header file is located in the gcc-arm\arm-none-eabi\include\c++\9.2.1 folder on my system. Check where the complex header file is located on your system. It should contain something like:

...
  /// Return complex base e exponential of @a z.
  template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
...
  // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
  template<typename _Tp>
    inline complex<_Tp>
    __complex_exp(const complex<_Tp>& __z)
    { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }

#if _GLIBCXX_USE_C99_COMPLEX
  inline __complex__ float
  __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }

  inline __complex__ double
  __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }

  inline __complex__ long double
  __complex_exp(const __complex__ long double& __z)
  { return __builtin_cexpl(__z); }

  template<typename _Tp>
    inline complex<_Tp>
    exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
#else
  template<typename _Tp>
    inline complex<_Tp>
    exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
#endif
...

Hi
I have found the complex file, its in the C:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\libcxx folder. This file does indeed have the function definitions as you’ve mentioned
complex_exponentialbase_e_definition

How do i get the IDE to take in this file
Thanks

Rather than let the IDE “guess” what path to use:

#include <complex>

try to provide the full path:

#include "C:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\libcxx\complex"

ADVICE: You can copy&paste source code snippets here and prepend and append a line containing ``` (three back-tick characters) rather than insert a picture. The advantage is that then people can copy&paste your code into their editor and compile, test …

It worked?
Thank you

I tried to provide the full path as seen below:

#include "mbed.h"
#include <iostream>
//#include <cmath>
//#include <complex> // complex already includes cmath 
#include "C:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\libcxx\complex.h"
#include "C:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\libcxx\complex"
...
mult = complex<double>(x[n]) * exp(ex*complex<double>(n)*complex<double>(k));

but the IDE still seems to be looking for the definition of exp in the math.h header rather than the complex header

No matching function for call to 'exp'

c:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\math.h:347:23:
note: candidate function not viable: no known conversion from 'complex<double>' to 'double' for 1st argument

c:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\math.h:959:18:
note: candidate function not viable: no known conversion from 'complex<double>' to 'float' for 1st argument

c:\ProgramData\Mbed Studio\mbed-studio-tools\ac6\include\math.h:1000:24:
note: candidate function not viable: no known conversion from 'complex<double>' to 'long double' for 1st argument

can’t seem to figure out why the IDE only looks for the function definition in the math.h header only

Thanks for the pro-tip for the code snippet

It seems that the mult variable is declared as double in your program (I have got the same error when I modified the example program above accordingly).
Try to define the mult variable as complex<double>.

complex<double> mult;

If it works then most likely you don’t need to use the full path in the header file includes.

NOTE: Try to compile the example program above as it is (without any modification).

Hi
The program as it is, has variable mult is declared as complex<double> variable

typedef complex<double> cplx;
	cplx mult;
	cplx sum;
	cplx ex = { 0, ((-1.0*2.0 * PI) / (double)DFT_SIZE) };// variables of the eulers number powers
	for (int n = 0; n < DFT_SIZE; n++) {
		sum = 0;
		for (int k = 0; k < DFT_SIZE; k++) {
			mult = (1/(double)DFT_SIZE)*X[k] * exp(-1.0*ex*(double)k*(double)n);
			sum = sum + mult;

but the IDE still flags a No matching function for call to 'exp'

and not only the exp function but cout aslo doesn’t seem to work with complex variables
cout_for_complex_variable
where x_in is a complex data type as well

cplx x_in[DFT_SIZE] = { {0.0,0.0},{0.0,0.0},{0.0,0.0},{0.9,0.0},{0.0,0.0},{0.0,0.0},{0.0,0.0},{0.0,0.0} };

seems like most functions in the complex library is unavailable, except when declaring a complex<double> var1 variable

this entire code, is verified to work on visual studio, so I suppose syntactically there shouldn’t be an issue. Only IDE library visibility, can’t seem to figure out why ?
btw Thanks Zoltan Hudak for the swift reply for all these times :slight_smile: