Pages: [1]   Go Down
Author Topic: Complex numbers library  (Read 2716 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

A playground article is in progress - http://arduino.cc/playground/Main/ComplexMath -

As allways comments and remarks are welcome

* complex0.1.01.zip (3.09 KB - downloaded 65 times.)
« Last Edit: March 11, 2012, 06:21:29 am by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 11, 2012, 07:11:03 am by robtillaart » Logged

Rob Tillaart

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

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks stimmer,

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

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just tested proposed code:
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 smiley-sad

I still have to try some variations

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

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!!

« Last Edit: March 11, 2012, 04:44:23 pm by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

update to version 0.1.03
- added minor fixes
- zip file also includes test sketch

* complex0.1.03.zip (5 KB - downloaded 20 times.)
Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

update to version 0.1.04
- added #ifdef's in complex.h file to selectively use functions.

* complex0.1.04.zip (5.23 KB - downloaded 12 times.)
Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 19 times.)
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: