+ added (0,0) constructor

+ added array example
+ removed unneeded ifdef constructs
This commit is contained in:
rob tillaart 2015-10-18 12:22:28 +02:00
parent 62e3379758
commit 7a698f8c17
4 changed files with 63 additions and 48 deletions

View File

@ -1,13 +1,14 @@
//
// FILE: Complex.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.08
// VERSION: 0.1.09
// PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath
//
// Released to the public domain
//
// 0.1.09 - added (0,0) constructor
// 0.1.08 - refactor
// 0.1.07 - refactor interfaces
@ -115,7 +116,6 @@ Complex& Complex::operator /= (const Complex &c)
return *this;
}
#ifdef COMPLEX_EXTENDED
//
// POWER FUNCTIONS
//
@ -166,11 +166,9 @@ Complex Complex::c_log10()
{
return c_logn(10);
}
#endif
#ifdef COMPLEX_GONIO_1
//
// GONIO I - SIN COS TAN
// GONIO I - SIN COS TAN
//
Complex Complex::c_sin()
{
@ -235,9 +233,7 @@ Complex Complex::c_atan()
{
return (Complex(0,-1) * (Complex(re, im - 1)/Complex(-re, -im - 1)).c_log()) * 0.5;
}
#endif
#ifdef COMPLEX_GONIO_2
//
// GONIO II - CSC SEC COT
//
@ -270,9 +266,7 @@ Complex Complex::c_acot()
{
return (one / *this).c_atan();
}
#endif
#ifdef COMPLEX_GONIO_3
//
// GONIO HYPERBOLICUS I
//
@ -326,9 +320,7 @@ Complex Complex::c_atanh()
c = c - (-(*this - one)).c_log();
return c * 0.5;
}
#endif
#ifdef COMPLEX_GONIO_4
//
// GONIO HYPERBOLICUS II
//
@ -361,6 +353,5 @@ Complex Complex::c_acoth()
{
return (one / *this).c_atanh();
}
#endif
// --- END OF FILE ---

View File

@ -1,7 +1,7 @@
//
// FILE: Complex.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.08
// VERSION: 0.1.09
// PURPOSE: library for Complex math for Arduino
// URL: http://arduino.cc/playground/Main/ComplexMath
//
@ -19,25 +19,15 @@
#include "Printable.h"
#define COMPLEX_LIB_VERSION "0.1.08"
#define COMPLEX_LIB_VERSION "0.1.09"
// five categories of functions can be switched per category
// by (un)commenting next lines.
#define COMPLEX_EXTENDED
#define COMPLEX_GONIO_1
#define COMPLEX_GONIO_2
#define COMPLEX_GONIO_3
#define COMPLEX_GONIO_4
class Complex: public Printable
class Complex: public Printable
{
public:
Complex(double r, double i) : re(r), im(i) {};
Complex(const double r=0, const double i=0) : 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; };
void set(const double r, const double i ) { re = r; im = i; };
double real() { return re; };
double imag() { return im; };
@ -65,7 +55,6 @@ public:
Complex& operator *= (const Complex&);
Complex& operator /= (const Complex&);
#ifdef COMPLEX_EXTENDED
Complex c_sqrt();
Complex c_sqr();
Complex c_exp();
@ -73,43 +62,34 @@ public:
Complex c_log10();
Complex c_pow(const Complex &);
Complex c_logn(const Complex &);
#endif
#ifdef COMPLEX_GONIO_1
Complex c_sin();
Complex c_cos();
Complex c_tan();
Complex c_asin();
Complex c_acos();
Complex c_atan();
#endif
#ifdef COMPLEX_GONIO_2
Complex c_csc();
Complex c_sec();
Complex c_cot();
Complex c_acsc();
Complex c_asec();
Complex c_acot();
#endif
#ifdef COMPLEX_GONIO_3
Complex c_sinh();
Complex c_cosh();
Complex c_tanh();
Complex c_asinh();
Complex c_acosh();
Complex c_atanh();
#endif
#ifdef COMPLEX_GONIO_4
Complex c_csch();
Complex c_sech();
Complex c_coth();
Complex c_acsch();
Complex c_asech();
Complex c_acoth();
#endif
protected:
double re;

View File

@ -0,0 +1,55 @@
//
// FILE: array.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// DATE: 2015-10-18
//
// PUPROSE: example complex array
//
#include "complex.h"
Complex samples[100];
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println("\n Complex numbers array example");
Serial.println(" Library version: ");
Serial.println(COMPLEX_LIB_VERSION);
Serial.println("\n1. Read into array");
for (int i = 0; i < 100; i++)
{
float re = analogRead(A0) / 1023.0;
float im = analogRead(A1) / 1023.0;
samples[i].set(re, im);
}
Serial.println("\n2. Print array");
for (int i = 0; i < 100; i++)
{
if (i % 5 == 0) Serial.println();
Serial.print(samples[i]);
Serial.print("\t");
}
Serial.println();
Serial.println("\n3. Sum array");
Complex sum;
for (int i = 0; i < 100; i++)
{
sum += samples[i];
}
Serial.println(sum);
Serial.println("\n done");
}
void loop()
{
}
//
// END OF FILE
//

View File

@ -120,7 +120,6 @@ void setup()
c5 = c5.reciprocal();
Serial.println(c5);
#ifdef COMPLEX_EXTENDED
Serial.println("\n9. power: exp log pow sqrt sqr logn log10");
c5 = c1.c_sqr();
Serial.println(c5);
@ -144,9 +143,7 @@ void setup()
Serial.println(c5);
c5 = c5.c_log10();
Serial.println(c5);
#endif
#ifdef COMPLEX_GONIO_1
Serial.println("\n10. gonio: sin cos tan asin acos atan");
c1.set(0.5, 0.5);
c5 = c1.c_sin();
@ -161,9 +158,7 @@ void setup()
Serial.println(c5);
c5 = c5.c_atan();
Serial.println(c5);
#endif
#ifdef COMPLEX_GONIO_2
Serial.println("\n11. gonio csc sec cot acsc asec acot");
c1.set(0.5, 0.5);
c5 = c1.c_csc();
@ -178,9 +173,7 @@ void setup()
Serial.println(c5);
c5 = c5.c_acot();
Serial.println(c5);
#endif
#ifdef COMPLEX_GONIO_3
Serial.println("\n12. gonio hyperbolicus I ");
c1.set(0.5, 0.5);
c5 = c1.c_sinh();
@ -195,9 +188,7 @@ void setup()
Serial.println(c5);
c5 = c5.c_atanh();
Serial.println(c5);
#endif
#ifdef COMPLEX_GONIO_4
Serial.println("\n13. gonio hyperbolicus II ");
c1.set(0.5, 0.5);
c5 = c1.c_csch();
@ -212,7 +203,6 @@ void setup()
Serial.println(c5);
c5 = c5.c_acoth();
Serial.println(c5);
#endif
Serial.println("\n.. Complex done");
@ -224,7 +214,6 @@ void setup()
uint32_t dur = micros() - start;
Serial.println(dur);
Serial.println(one);
}
void loop()