Complex numbers library

Recently there was a request for complex numbers and yesterday I came across a set of complex math formula's so I decided to Arduinize them into a library.
It took some hours to google a complete set of formulas from different sites which resulted into a rather BIG lib. It definitely needs a review as I assume some parts can be optimized. Fortunately everyone can strip out only the functions they need to minimize the lib (compiler is also helpful).

.cpp and .h and the testcode is attached as zip-file

Playground article - Arduino Playground - ComplexMath - (not up to date)
Latest version - Arduino/libraries/Complex at master · RobTillaart/Arduino · GitHub -

As allways comments and remarks are welcome

complex0.1.01.zip (3.09 KB)

some test update:

  • output under IDE 0.22 and IDE 1.0 of the test sketch is identical
  • 0.22 size: 18594
  • 1.0 size: 19050

If multiplication is much slower than addition/subtraction (which it might be on an Arduino), there is a formula for complex multiplication which only uses three multiplications at the expense of needing 5 additions/subtractions:

double s=a.re * b.re;
double t=a.im * b.im;
double u=(a.re + a.im)*(b.re + b.im);

c.re = s-t;
c.im = u-s-t;

It might be worth investigating if this works any faster in practice.

Thanks stimmer,

I did no performance tests/tuning yet but this is definitely one to remember.

Just tested proposed code:

  unsigned long start = micros();
  for (int i = 0; i< 10000; i++)
  {
    c5 = c3 * c4;
  }
  unsigned long stop = micros();
  Serial.println(stop - start);
  Serial.println("\n.. Complex done");

traditional code: 667180 for 10K multiplications
proposed code: 784068 for 10K multiplications
diff: 116888 = 17.5% slower :frowning:

I still have to try some variations

  • update -
    improved the complex division by 9% :slight_smile: - new code -
Complex Complex::operator / (Complex c)
{
	double f = 1/(c.re*c.re + c.im*c.im);
	double r = re * c.re + im * c.im;
	double i = re * c.im - im * c.re;
	return Complex(r * f, -i * f);
}
  • update -
  • improved upon other divisions too,
  • other optimizations costs a lot of memory
    e.g. c_tan() = c_sin()/c_cos() which can be rewritten in 11 lines (steps) so that no new object is created => -44% but price is 358 bytes!!

update to version 0.1.03

  • added minor fixes
  • zip file also includes test sketch

complex0.1.03.zip (5 KB)

update to version 0.1.04

  • added #ifdef's in complex.h file to selectively use functions.

complex0.1.04.zip (5.23 KB)

update to version 0.1.05

  • derive complex from PRINT ==> so it can be used in Serial.print() and other print derived classes.
  • layout

// note the test program on the playground may not be compatible anymore (I need to check that someday)

Complex0.1.05.zip (5.63 KB)

update to version 0.1.07 (attachment)

  • refactored interface to decrease footprint
  • added example folder
  • added referenceOutput.txt to test sketch

As always comments and remarks are welcome

Latest version on GITHUB - Arduino/libraries/Complex at master · RobTillaart/Arduino · GitHub -

complex0.1.07.zip (5.79 KB)

update to version 0.1.08

  • refactored to decrease footprint
  • replaced divisions with multiplies (faster)

As always comments and remarks are welcome

Latest version - Arduino/libraries/Complex at master · RobTillaart/Arduino · GitHub -

update to version 0.1.09

  • added (0,0) constructor
  • added array example
  • removed unneeded ifdef constructs

As always comments and remarks are welcome

Latest version - Arduino/libraries/Complex at master · RobTillaart/Arduino · GitHub -

I have problems with the complex library when I try to use it with the DUE board.

I have checked the example program complex.ino with the UNO board and it works correctly, but when compiling it for the DUE I have the following errors.

Z: \ Arduino \ Libraries \ complex_0_1_7 \ examples \ complex \ complex.ino: In function 'void setup ()':

complex: 16: error: 'COMPLEX_LIB_VERSION' was not declared in this scope

Serial.println (COMPLEX_LIB_VERSION);

^

complex: 19: error: 'Complex' was not declared in this scope

Complex c1 (10.0, -2.0);

^

complex: 19: error: expected ';' before 'c1'

Complex c1 (10.0, -2.0);

^

complex: 20: error: expected ';' before 'c2'

Complex c2 (3.0);

^

complex: 21: error: expected ';' before 'c3'

Complex c3 (-10,4);

^

complex: 22: error: expected ';' before 'c4'

Complex c4 (-5.5);

^

complex: 23: error: expected ';' before 'c5'

Complex c5 (0, 0);

^

complex: 25: error: 'one' was not declared in this scope

Serial.println (one);

^

complex: 26: error: 'c1' was not declared in this scope

Serial.println (c1);

^

complex: 27: error: 'c2' was not declared in this scope

Serial.println (c2);

^

complex: 28: error: 'c3' was not declared in this scope

Serial.println (c3);

^

complex: 29: error: 'c4' was not declared in this scope

Serial.println (c4);

^

complex: 36: error: 'c5' was not declared in this scope

c5 = c1;

^

exit status 1
'COMPLEX_LIB_VERSION' was not declared in this scope

Could someone help me?

Thank you very much and best greetings,

@Jpllerandi
Does this problem still exists?

robtillaart:
@Jpllerandi
Does this problem still exists?

@robtillaart Yes, it still exists.

Do you have any idea how to fix it?

I would really appreciate your help telling me what to change for this library to work on Arduino DUE.

Beforehand thank you very much.

I'm investigating....

Solved, see - Complex Library Arduino Due · Issue #90 · RobTillaart/Arduino · GitHub