Go Down

### Topic: Complex numbers library (Read 9495 times)previous topic - next topic

#### robtillaart

##### Mar 11, 2012, 12:12 pmLast Edit: Oct 18, 2015, 12:15 pm by robtillaart
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 - http://arduino.cc/playground/Main/ComplexMath - (not up to date)

As allways comments and remarks are welcome
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#1
##### Mar 11, 2012, 01:06 pmLast Edit: Mar 11, 2012, 01:11 pm by robtillaart Reason: 1
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
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### stimmer

#2
##### Mar 11, 2012, 03:05 pm
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:

Code: [Select]
`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.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

#### robtillaart

#3
##### Mar 11, 2012, 07:03 pm

Thanks stimmer,

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#4
##### Mar 11, 2012, 08:20 pmLast Edit: Mar 11, 2012, 10:44 pm by robtillaart Reason: 1
Just tested proposed code:
Code: [Select]
`  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

I still have to try some variations

- update -
improved the complex division by 9%  - new code -

Code: [Select]
`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!!

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#5
##### May 19, 2012, 10:40 am
update to version 0.1.03
- zip file also includes test sketch
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#6
##### Sep 23, 2013, 01:45 pm
update to version 0.1.04
- added #ifdef's in complex.h file to selectively use functions.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#7
##### Oct 05, 2013, 07:20 pm
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)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#8
##### Mar 06, 2015, 10:25 amLast Edit: Mar 06, 2015, 10:29 am by robtillaart
update to version 0.1.07 (attachment)
+ refactored interface to decrease footprint
+ added referenceOutput.txt to test sketch

As always comments and remarks are welcome

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#9
##### Mar 07, 2015, 09:10 pm
update to version 0.1.08
+ refactored to decrease footprint
+ replaced divisions with multiplies (faster)

As always comments and remarks are welcome

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#10
##### Oct 18, 2015, 12:14 pm
update to version 0.1.09
+ removed unneeded ifdef constructs

As always comments and remarks are welcome

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Jpllerandi

#11
##### Oct 01, 2017, 08:48 pm
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,

#### robtillaart

#12
##### Dec 09, 2017, 08:46 pm
@Jpllerandi
Does this problem still exists?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### alcazar_juanc

#13
##### Jan 25, 2018, 08:44 am
@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.

#### robtillaart

#14
##### Jan 25, 2018, 09:06 am
I'm investigating....
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up