+ version 0.1.08

+ refactor
This commit is contained in:
rob tillaart 2015-03-06 14:02:38 +01:00
parent 39de4fa177
commit 411126e5a9
2 changed files with 23 additions and 20 deletions

View File

@ -1,13 +1,14 @@
// //
// FILE: Complex.cpp // FILE: Complex.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.07 // VERSION: 0.1.08
// PURPOSE: library for Complex math for Arduino // PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath // URL: http://arduino.cc/playground/Main/ComplexMath
// //
// Released to the public domain // Released to the public domain
// //
// 0.1.08 - refactor
// 0.1.07 - refactor interfaces // 0.1.07 - refactor interfaces
#include "complex.h" #include "complex.h"
@ -31,7 +32,7 @@ void Complex::polar(const double modulus, const double phase)
Complex Complex::reciprocal() Complex Complex::reciprocal()
{ {
double f = 1.0/ (re*re + im*im); double f = 1.0/(re*re + im*im);
double r = re*f; double r = re*f;
double i = -im*f; double i = -im*f;
return Complex(r,i); return Complex(r,i);
@ -75,10 +76,10 @@ Complex Complex::operator * (const Complex &c)
Complex Complex::operator / (const Complex &c) Complex Complex::operator / (const Complex &c)
{ {
double f = (c.re*c.re + c.im*c.im); double f = 1.0/(c.re*c.re + c.im*c.im);
double r = re * c.re + im * c.im; double r = (re * c.re + im * c.im) * f;
double i = im * c.re - re * c.im; double i = (im * c.re - re * c.im) * f;
return Complex(r / f, i / f); return Complex(r, i);
} }
Complex& Complex::operator += (const Complex &c) Complex& Complex::operator += (const Complex &c)
@ -107,10 +108,10 @@ Complex& Complex::operator *= (const Complex &c)
Complex& Complex::operator /= (const Complex &c) Complex& Complex::operator /= (const Complex &c)
{ {
double f = 1.0/(c.re*c.re + c.im*c.im); double f = 1.0/(c.re*c.re + c.im*c.im);
double r = re * c.re + im * c.im; double r = (re * c.re + im * c.im) * f;
double i = re * c.im - im * c.re; double i = (im * c.re - re * c.im) * f;
re = r*f; re = r;
im = -i*f; im = i;
return *this; return *this;
} }
@ -122,7 +123,7 @@ Complex Complex::c_sqr()
{ {
double r = re * re - im * im; double r = re * re - im * im;
double i = 2 * re * im; double i = 2 * re * im;
return Complex(r,i); return Complex(r, i);
} }
Complex Complex::c_sqrt() Complex Complex::c_sqrt()
@ -131,7 +132,7 @@ Complex Complex::c_sqrt()
double r = sqrt(0.5 * (m+re)); double r = sqrt(0.5 * (m+re));
double i = sqrt(0.5 * (m-re)); double i = sqrt(0.5 * (m-re));
if (im < 0) i = -i; if (im < 0) i = -i;
return Complex(r,i); return Complex(r, i);
} }
Complex Complex::c_exp() Complex Complex::c_exp()
@ -148,15 +149,17 @@ Complex Complex::c_log()
return Complex(log(m), p); return Complex(log(m), p);
} }
Complex Complex::c_pow(Complex c) Complex Complex::c_pow(const Complex &c)
{ {
Complex t = c * c_log(); Complex t = c_log();
t = t * c;
return t.c_exp(); return t.c_exp();
} }
Complex Complex::c_logn(Complex c) Complex Complex::c_logn(const Complex &c)
{ {
return c_log()/c.c_log(); Complex t = c;
return c_log()/t.c_log();
} }
Complex Complex::c_log10() Complex Complex::c_log10()

View File

@ -1,7 +1,7 @@
// //
// FILE: Complex.h // FILE: Complex.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.07 // VERSION: 0.1.08
// PURPOSE: library for Complex math for Arduino // PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath // URL: http://arduino.cc/playground/Main/ComplexMath
// //
@ -19,7 +19,7 @@
#include "Printable.h" #include "Printable.h"
#define COMPLEX_LIB_VERSION "0.1.07" #define COMPLEX_LIB_VERSION "0.1.08"
// five categories of functions can be switched per category // five categories of functions can be switched per category
// by (un)commenting next lines. // by (un)commenting next lines.
@ -71,8 +71,8 @@ public:
Complex c_exp(); Complex c_exp();
Complex c_log(); Complex c_log();
Complex c_log10(); Complex c_log10();
Complex c_pow(Complex); Complex c_pow(const Complex &);
Complex c_logn(Complex); Complex c_logn(const Complex &);
#endif #endif
#ifdef COMPLEX_GONIO_1 #ifdef COMPLEX_GONIO_1