Complex Numbers

Status
Not open for further replies.

Jake

Well-known member
I am wondering what is normally done when complex number math is required for the Teensy. There seem to be a couple of options:

1. Use Rob Tillaart's library found at http://forum.arduino.cc/index.php?topic=96080.0

2. Perhaps gcc for Teensyduino already has a complex number library

3. Since I only need to do some multiplies and divides, work with the real and imaginary parts myself without using the complex numbers.

I thought I would ask for advice before digging into it
 
Wow, it does work. I just learned something new today!

Code:
#include <complex.h>

__complex__ float x;

void setup() {
  x = 2.0 + 3.0i;
}

void loop() {
  Serial.print("real = ");
  Serial.print(crealf(x));
  Serial.print(", imag = ");
  Serial.print(cimagf(x));
  Serial.print(", magn = ");
  Serial.print(sqrtf(crealf(x) * crealf(x) + cimagf(x) * cimagf(x)));
  Serial.println();
  delay(1000);
}
 
Great News, Thanks a lot Paul, but always a couple of questions.

1. How did you get away without a
Code:
Serial.begin(9600);
command

2. Why is my program not printing?

3. Why does the conj command not work (I thought I would be fancy and used the sqrt of the number multiplied by its complex conjugate for the magnitude.

Code:
#include <complex.h>

__complex__ float x;

void setup() {
  x = 2.0 + 3.0i;
  Serial.begin(9600);
  delay(2000);
}

void loop() {
  Serial.print("real = ");
  Serial.print(crealf(x));
  Serial.print(", imag = ");
  Serial.print(cimagf(x));
  Serial.print(", mag = ");
  Serial.print(sqrtf(crealf(x) * crealf(x) + cimagf(x) * cimagf(x)));
  Serial.print(", mag using conj = ");
//  Serial.print((conj(x)));
//  Serial.print(sqrtf(x * conj(x)));
  Serial.print(", angle = ");
  Serial.print(180.0 / PI * cargf(x));
  Serial.println();
  delay(1000);
}
 
Sqrt(-1) = i ; However a complex multiplied by its complex conjugate is a real. Try FOIL on it.

Program prints fine, my mistake above.

I am curious when
Code:
Serial.begin(9600);
became optional

It seems that basic complex numbers work but the code snippets
Code:
  Serial.print(cexpf(x));
  Serial.print(conjf(x));
  Serial.print(csinf(x));
  Serial.print(csqrtf(x * conjf(x)));
don't work.

Just to test my knowledge of C I tried the following c-program

Code:
#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void) {
_Complex double x = 3 + 4j;

printf("The real part of x is: %g\n", creal(x));
printf("The imaginary part of x is: %g\n", cimag(x));
printf("The complex conjugate of x is: %g + %gj\n", creal(conj(x)), cimag(conj(x)));
printf("The magnitude of x is: %g\n", sqrt(x*conj(x)));
printf("The angle of x is: %g\n", 180/M_PI*carg(x));
printf("The sqrt of x is: %g + %gj\n", creal(csqrt(x)), cimag(csqrt(x)));
printf("The exponential of x is: %g + %gj\n", creal(cexp(x)), cimag(cexp(x)));
printf("The sin of x is: %g + %gj\n", creal(csin(x)), cimag(csin(x)));

return(1);
}

and compiled it using
Code:
gcc complexText.c -o ct -lm
and got the expected output. So my conclusion is that the complex functions for the teensy are limited.

Thanks
 
Last edited:
An imaginary number is a complex number that can be written as a real number multiplied by the imaginary unit i, which is defined by its property i^2 = −1. The square of an imaginary number bi is −b^2. For example, 5i is an imaginary number, and its square is −25.
 
In this:
Code:
Serial.print(csqrtf(x * conjf(x)));
and similar code, the result is still a __complex__ float even though we think of the result as being real (i.e. the result might be, for example, 13.2+0i instead of 13.2)and Serial.print doesn't have a method defined to handle the __complex__ type.
When you know the result is real, you would have to do this:
Code:
Serial.print(creal(csqrtf(x * conjf(x))));

Pete
 
An imaginary number is a complex number that can be written as a real number multiplied by the imaginary unit i, which is defined by its property i^2 = −1. The square of an imaginary number bi is −b^2. For example, 5i is an imaginary number, and its square is −25.

what I said, but I was joking (and I'm sure Stevech knows complex arithmetic)
 
Thank you all, with Pete's insight it is all working now. No compiler limitations at least for this test.

And thanks Frank for your clarification on serial over usb.
 
Status
Not open for further replies.
Back
Top