+ version 0.1.07 (attachment)

+ refactored to decrease footprint
+ added example folder
+ added referenceOutput.txt to the example sketches
This commit is contained in:
rob tillaart 2015-03-06 10:41:34 +01:00
parent bb3198d47d
commit 35b19c9fe7
6 changed files with 254 additions and 116 deletions

View File

@ -1,80 +1,17 @@
//
// FILE: Complex.cpp
// AUTHOR: Rob Tillaart
// VERSION: see COMPLEX_LIB_VERSION
// VERSION: 0.1.07
// PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath
//
// Released to the public domain
//
// 0.1.07 - refactor interfaces
#include "complex.h"
Complex::Complex(double real, double imag)
{
re = real;
im = imag;
}
Complex::Complex(const Complex &c)
{
re = c.re;
im = c.im;
}
Complex::Complex(double d)
{
re = d;
im = 0;
}
void Complex::set(double real, double imag)
{
re = real;
im = imag;
}
double Complex::real()
{
return re;
}
double Complex::imag()
{
return im;
}
// polar2cartesian
void Complex::polar(double modulus, double phase)
{
re = modulus * cos(phase);
im = modulus * sin(phase);
}
double Complex::phase()
{
return atan2(im, re);
}
double Complex::modulus()
{
return hypot(re, im);
}
// conjugate is the number mirrored in x-axis
Complex Complex::conjugate()
{
return Complex(re,-im);
}
Complex Complex::reciprocal()
{
double f = 1.0/(re*re + im*im);
double r = re*f;
double i = -im*f;
return Complex(r,i);
}
// PRINTING
size_t Complex::printTo(Print& p) const
{
@ -86,13 +23,28 @@ size_t Complex::printTo(Print& p) const
return n;
};
void Complex::polar(const double modulus, const double phase)
{
re = modulus * cos(phase);
im = modulus * sin(phase);
}
Complex Complex::reciprocal()
{
double f = 1.0/ (re*re + im*im);
double r = re*f;
double i = -im*f;
return Complex(r,i);
}
// EQUALITIES
bool Complex::operator == (Complex c)
bool Complex::operator == (const Complex &c)
{
return (re == c.re) && (im == c.im);
}
bool Complex::operator != (Complex c)
bool Complex::operator != (const Complex &c)
{
return (re != c.re) || (im != c.im);
}
@ -104,58 +56,62 @@ Complex Complex::operator - ()
}
// BASIC MATH
Complex Complex::operator + (Complex c)
Complex Complex::operator + (const Complex &c)
{
return Complex(re + c.re, im + c.im);
}
Complex Complex::operator - (Complex c)
Complex Complex::operator - (const Complex &c)
{
return Complex(re - c.re, im - c.im);
}
Complex Complex::operator * (Complex c)
Complex Complex::operator * (const Complex &c)
{
double r = re * c.re - im * c.im;
double i = re * c.im + im * c.re;
return Complex(r, i);
}
Complex Complex::operator / (Complex c)
Complex Complex::operator / (const Complex &c)
{
double f = 1.0/(c.re*c.re + c.im*c.im);
double f = (c.re*c.re + c.im*c.im);
double r = re * c.re + im * c.im;
double i = im * c.re - re * c.im;
return Complex(r * f, i * f);
return Complex(r / f, i / f);
}
void Complex::operator += (Complex c)
Complex& Complex::operator += (const Complex &c)
{
re += c.re;
im += c.im;
return *this;
}
void Complex::operator -= (Complex c)
Complex& Complex::operator -= (const Complex &c)
{
re -= c.re;
im -= c.im;
return *this;
}
void Complex::operator *= (Complex c)
Complex& Complex::operator *= (const Complex &c)
{
double r = re * c.re - im * c.im;
double i = re * c.im + im * c.re;
re = r;
im = i;
return *this;
}
void Complex::operator /= (Complex c)
Complex& Complex::operator /= (const Complex &c)
{
double f = 1.0/(c.re*c.re + c.im*c.im);
double r = re * c.re + im * c.im;
double i = re * c.im - im * c.re;
re = r*f;
im = -i*f;
return *this;
}
#ifdef COMPLEX_EXTENDED
@ -215,12 +171,16 @@ Complex Complex::c_log10()
//
Complex Complex::c_sin()
{
return Complex(sin(re) * cosh(im), cos(re) * sinh(im));
double s = sin(re);
double c = sqrt(1.0-s*s);
return Complex(s * cosh(im), c * sinh(im));
}
Complex Complex::c_cos()
{
return Complex(cos(re) * cosh(im), -sin(re) * sinh(im));
double s = sin(re);
double c = sqrt(1.0-s*s);
return Complex(c * cosh(im), -s * sinh(im));
}
Complex Complex::c_tan()
@ -243,7 +203,7 @@ Complex Complex::c_tan()
return c_sin() / c_cos();
}
Complex Complex::gonioHelper1(int mode)
Complex Complex::gonioHelper1(const byte mode)
{
Complex c = (one - this->c_sqr()).c_sqrt();
if (mode == 0)
@ -270,7 +230,7 @@ Complex Complex::c_acos()
Complex Complex::c_atan()
{
return (Complex(0,-1) * (Complex(re, im - 1)/Complex(-re, -im - 1)).c_log())/2;
return (Complex(0,-1) * (Complex(re, im - 1)/Complex(-re, -im - 1)).c_log()) * 0.5;
}
#endif
@ -315,12 +275,16 @@ Complex Complex::c_acot()
//
Complex Complex::c_sinh()
{
return Complex(sinh(re) * cos(im), cosh(re)* sin(im));
double s = sin(im);
double c = sqrt(1.0 - s*s);
return Complex(sinh(re) * c, cosh(re)* s);
}
Complex Complex::c_cosh()
{
return Complex(cosh(re) * cos(im), sinh(re)* sin(im));
double s = sin(im);
double c = sqrt(1.0-s*s);
return Complex(cosh(re) * c, sinh(re)* s);
}
Complex Complex::c_tanh()
@ -328,10 +292,10 @@ Complex Complex::c_tanh()
return c_sinh() / c_cosh();
}
Complex Complex::gonioHelper2(int mode)
Complex Complex::gonioHelper2(const byte mode)
{
Complex c = c_sqr();
if (mode==0)
if (mode == 0)
{
c += 1;
}
@ -357,7 +321,7 @@ Complex Complex::c_atanh()
{
Complex c = (*this + one).c_log();
c = c - (-(*this - one)).c_log();
return c/2;
return c * 0.5;
}
#endif

View File

@ -1,7 +1,7 @@
//
// FILE: Complex.h
// AUTHOR: Rob Tillaart
// VERSION: see COMPLEX_LIB_VERSION
// VERSION: 0.1.07
// PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath
//
@ -19,7 +19,7 @@
#include "Printable.h"
#define COMPLEX_LIB_VERSION "0.1.05"
#define COMPLEX_LIB_VERSION "0.1.07"
// five categories of functions can be switched per category
// by (un)commenting next lines.
@ -33,37 +33,37 @@
class Complex: public Printable
{
public:
Complex(double, double);
Complex(const Complex &);
Complex(double);
Complex(double r, double i) : re(r), im(i) {};
Complex(const Complex &c) : re(c.re), im(c.im) {};
Complex(double d) : re(d), im(0) {};
void set(double r, double i ) { re = r; im = i; };
double real() { return re; };
double imag() { return im; };
size_t printTo(Print& p) const;
void set(double, double);
double real();
double imag();
void polar(double, double);
double phase();
double modulus();
Complex conjugate();
void polar(const double, const double);
double phase() { return atan2(im, re); };
double modulus() { return hypot(re, im); };
// conjugate is the number mirrored in x-axis
Complex conjugate() { return Complex(re,-im); };
Complex reciprocal();
bool operator == (Complex);
bool operator != (Complex);
bool operator == (const Complex&);
bool operator != (const Complex&);
Complex operator - (); // negation
Complex operator + (Complex);
Complex operator - (Complex);
Complex operator * (Complex);
Complex operator / (Complex);
Complex operator + (const Complex&);
Complex operator - (const Complex&);
Complex operator * (const Complex&);
Complex operator / (const Complex&);
void operator += (Complex);
void operator -= (Complex);
void operator *= (Complex);
void operator /= (Complex);
Complex& operator += (const Complex&);
Complex& operator -= (const Complex&);
Complex& operator *= (const Complex&);
Complex& operator /= (const Complex&);
#ifdef COMPLEX_EXTENDED
Complex c_sqrt();
@ -111,15 +111,15 @@ public:
Complex c_acoth();
#endif
private:
protected:
double re;
double im;
Complex gonioHelper1(int);
Complex gonioHelper2(int);
Complex gonioHelper1(const byte);
Complex gonioHelper2(const byte);
};
static Complex one(1,0);
static Complex one(1, 0);
#endif
// --- END OF FILE ---

View File

@ -0,0 +1,107 @@
Complex numbers test for Arduino: 0.1.06
1. Print Complex, set, real, imag
1.000 0.000i
10.000 -2.000i
3.000 0.000i
-10.000 4.000i
-5.000 -5.000i
0.000 0.000i
0.00
0.00
2. == !=
ok :)
ok :)
ok :)
3. negation -
-10.000 2.000i
10.000 -2.000i
ok :)
4. + -
13.000 -2.000i
13.000 -2.000i
7.000 -2.000i
7.000 -2.000i
5. * /
30.000 -6.000i
90.000 -18.000i
30.000 -6.000i
10.000 -2.000i
10.000 -2.000i
10.000 -2.000i
6. assign += -= *= /=
20.000 -4.000i
23.000 -4.000i
13.000 -2.000i
10.000 -2.000i
96.000 -40.000i
288.000 -120.000i
30.000 -6.000i
10.000 -2.000i
7. phase modulus polar
10.000 -2.000i
10.20
-0.20
10.000 -2.000i
8. conjugate reciprocal
10.000 2.000i
10.000 -2.000i
0.096 0.019i
10.000 -2.000i
9. power: exp log pow sqrt sqr logn log10
96.000 -40.000i
-9166.239 -20028.597i
10.000 -2.000i
96.000 -40.000i
10.000 -2.000i
96.000 -40.000i
880.000 -592.000i
10.000 -2.000i
0.534 0.542i
10.000 -2.000i
1.009 -0.086i
10. gonio: sin cos tan asin acos atan
0.541 0.457i
0.500 0.500i
0.990 -0.250i
0.500 0.500i
0.404 0.564i
0.500 0.500i
11. gonio csc sec cot acsc asec acot
1.078 -0.912i
0.500 0.500i
0.950 0.240i
0.500 0.500i
0.839 -1.172i
0.500 0.500i
12. gonio hyperbolicus I
0.457 0.541i
0.500 0.500i
0.990 0.250i
0.500 0.500i
0.564 0.404i
0.500 0.500i
13. gonio hyperbolicus II
0.912 -1.078i
0.500 0.500i
0.950 -0.240i
0.500 0.500i
1.172 -0.839i
0.500 0.500i
.. Complex done
465072
1.000 0.000i

View File

@ -0,0 +1,67 @@
Complex numbers performance test for Arduino: 0.1.07
5 constructors 8
set(0,0) 4
c1 + 1 2580
c1 + c2 2392
+= c2 2104
c5 = -c1 760
c1 - 3 2464
c1 - c2 2296
c5 -= c2 1976
c1 * 3 5700
c1 * c2 5560
c5 *= c2 5152
c1 / 3 13864
c1 / c2 13724
c5 /= c2 12248
real() 4
imag() 4
modulus() 68
phase 204
polar() 24476
conjugate() 4
reciprocal(); 8572
c_sqr() 5092
c_exp() 43100
c_log() 40144
c_pow(2) 89248
c_sqrt() 8616
c_logn(c4) 70780
c_pow(c5) 62192
c_log10() 38220
c_sin() 51772
c_asin() 76844
c_cos() 51860
c_acos() 75248
c_tan() 118520
c_atan() 69332
c_csc() 66048
c_acsc() 85060
c_sec() 66292
c_asec() 90756
c_cot() 132736
c_acot() 77304
c_sinh() 51764
c_asinh() 66212
c_cosh() 51860
c_acosh() 64388
c_tanh() 118524
c_atanh() 92468
c_csch() 66140
c_acsch() 70128
c_sech() 66292
c_asech() 81692
c_coth() 132664
c_acoth() 100032
2308648
.. Complex done