mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
fix #7 add const
This commit is contained in:
parent
6035f172bf
commit
36c3e468a0
@ -1,12 +1,13 @@
|
||||
//
|
||||
// FILE: Complex.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.4
|
||||
// VERSION: 0.3.0
|
||||
// PURPOSE: Arduino library for Complex math
|
||||
// URL: https://github.com/RobTillaart/Complex
|
||||
// http://arduino.cc/playground/Main/ComplexMath
|
||||
//
|
||||
// HISTORY
|
||||
// 0.3.0 2021-11-15 fix #7 adding const to operators
|
||||
// 0.2.4 2021-10-19 update build-CI.
|
||||
// 0.2.3 2021-09-14 fix build-CI + update readme
|
||||
// 0.2.2 2020-12-16 add arduino-ci + unit test (starter)
|
||||
@ -43,7 +44,7 @@ void Complex::polar(const float modulus, const float phase)
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::reciprocal()
|
||||
Complex Complex::reciprocal() const
|
||||
{
|
||||
float f = 1.0 / (re * re + im * im);
|
||||
float r = re * f;
|
||||
@ -54,13 +55,13 @@ Complex Complex::reciprocal()
|
||||
//
|
||||
// EQUALITIES
|
||||
//
|
||||
bool Complex::operator == (const Complex &c)
|
||||
bool Complex::operator == (const Complex &c) const
|
||||
{
|
||||
return (re == c.re) && (im == c.im);
|
||||
}
|
||||
|
||||
|
||||
bool Complex::operator != (const Complex &c)
|
||||
bool Complex::operator != (const Complex &c) const
|
||||
{
|
||||
return (re != c.re) || (im != c.im);
|
||||
}
|
||||
@ -69,7 +70,7 @@ bool Complex::operator != (const Complex &c)
|
||||
//
|
||||
// NEGATE
|
||||
//
|
||||
Complex Complex::operator - ()
|
||||
Complex Complex::operator - () const
|
||||
{
|
||||
return Complex(-re, -im);
|
||||
}
|
||||
@ -78,19 +79,19 @@ Complex Complex::operator - ()
|
||||
//
|
||||
// BASIC MATH
|
||||
//
|
||||
Complex Complex::operator + (const Complex &c)
|
||||
Complex Complex::operator + (const Complex &c) const
|
||||
{
|
||||
return Complex(re + c.re, im + c.im);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::operator - (const Complex &c)
|
||||
Complex Complex::operator - (const Complex &c) const
|
||||
{
|
||||
return Complex(re - c.re, im - c.im);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::operator * (const Complex &c)
|
||||
Complex Complex::operator * (const Complex &c) const
|
||||
{
|
||||
float r = re * c.re - im * c.im;
|
||||
float i = re * c.im + im * c.re;
|
||||
@ -98,7 +99,7 @@ Complex Complex::operator * (const Complex &c)
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::operator / (const Complex &c)
|
||||
Complex Complex::operator / (const Complex &c) const
|
||||
{
|
||||
float f = 1.0/(c.re * c.re + c.im * c.im);
|
||||
float r = (re * c.re + im * c.im) * f;
|
||||
@ -147,7 +148,7 @@ Complex& Complex::operator /= (const Complex &c)
|
||||
//
|
||||
// POWER FUNCTIONS
|
||||
//
|
||||
Complex Complex::c_sqr()
|
||||
Complex Complex::c_sqr() const
|
||||
{
|
||||
float r = re * re - im * im;
|
||||
float i = 2 * re * im;
|
||||
@ -155,7 +156,7 @@ Complex Complex::c_sqr()
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_sqrt()
|
||||
Complex Complex::c_sqrt() const
|
||||
{
|
||||
float m = modulus();
|
||||
float r = sqrt(0.5 * (m + re));
|
||||
@ -165,14 +166,14 @@ Complex Complex::c_sqrt()
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_exp()
|
||||
Complex Complex::c_exp() const
|
||||
{
|
||||
float e = exp(re);
|
||||
return Complex(e * cos(im), e * sin(im));
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_log()
|
||||
Complex Complex::c_log() const
|
||||
{
|
||||
float m = modulus();
|
||||
float p = phase();
|
||||
@ -181,7 +182,7 @@ Complex Complex::c_log()
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_pow(const Complex &c)
|
||||
Complex Complex::c_pow(const Complex &c) const
|
||||
{
|
||||
Complex t = c_log();
|
||||
t = t * c;
|
||||
@ -189,14 +190,14 @@ Complex Complex::c_pow(const Complex &c)
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_logn(const Complex &c)
|
||||
Complex Complex::c_logn(const Complex &c) const
|
||||
{
|
||||
Complex t = c;
|
||||
return c_log()/t.c_log();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_log10()
|
||||
Complex Complex::c_log10() const
|
||||
{
|
||||
return c_logn(10);
|
||||
}
|
||||
@ -205,19 +206,19 @@ Complex Complex::c_log10()
|
||||
//
|
||||
// GONIO I - SIN COS TAN
|
||||
//
|
||||
Complex Complex::c_sin()
|
||||
Complex Complex::c_sin() const
|
||||
{
|
||||
return Complex(sin(re) * cosh(im), cos(re) * sinh(im));
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_cos()
|
||||
Complex Complex::c_cos() const
|
||||
{
|
||||
return Complex(cos(re) * cosh(im), -sin(re) * sinh(im));
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_tan()
|
||||
Complex Complex::c_tan() const
|
||||
{
|
||||
/* faster but 350 bytes longer!!
|
||||
float s = sin(re);
|
||||
@ -238,7 +239,7 @@ Complex Complex::c_tan()
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::gonioHelper1(const byte mode)
|
||||
Complex Complex::gonioHelper1(const byte mode) const
|
||||
{
|
||||
Complex c = (one - this->c_sqr()).c_sqrt();
|
||||
if (mode == 0)
|
||||
@ -254,19 +255,19 @@ Complex Complex::gonioHelper1(const byte mode)
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_asin()
|
||||
Complex Complex::c_asin() const
|
||||
{
|
||||
return gonioHelper1(0);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acos()
|
||||
Complex Complex::c_acos() const
|
||||
{
|
||||
return gonioHelper1(1);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_atan()
|
||||
Complex Complex::c_atan() const
|
||||
{
|
||||
return (Complex(0,-1) * (Complex(re, im - 1)/Complex(-re, -im - 1)).c_log()) * 0.5;
|
||||
}
|
||||
@ -275,37 +276,37 @@ Complex Complex::c_atan()
|
||||
//
|
||||
// GONIO II - CSC SEC COT
|
||||
//
|
||||
Complex Complex::c_csc()
|
||||
Complex Complex::c_csc() const
|
||||
{
|
||||
return one / c_sin();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_sec()
|
||||
Complex Complex::c_sec() const
|
||||
{
|
||||
return one / c_cos();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_cot()
|
||||
Complex Complex::c_cot() const
|
||||
{
|
||||
return one / c_tan();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acsc()
|
||||
Complex Complex::c_acsc() const
|
||||
{
|
||||
return (one / *this).c_asin();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_asec()
|
||||
Complex Complex::c_asec() const
|
||||
{
|
||||
return (one / *this).c_acos();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acot()
|
||||
Complex Complex::c_acot() const
|
||||
{
|
||||
return (one / *this).c_atan();
|
||||
}
|
||||
@ -314,25 +315,25 @@ Complex Complex::c_acot()
|
||||
//
|
||||
// GONIO HYPERBOLICUS I
|
||||
//
|
||||
Complex Complex::c_sinh()
|
||||
Complex Complex::c_sinh() const
|
||||
{
|
||||
return Complex(cos(im) * sinh(re), sin(im) * cosh(re));
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_cosh()
|
||||
Complex Complex::c_cosh() const
|
||||
{
|
||||
return Complex(cos(im) * cosh(re), sin(im) * sinh(re));
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_tanh()
|
||||
Complex Complex::c_tanh() const
|
||||
{
|
||||
return c_sinh() / c_cosh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::gonioHelper2(const byte mode)
|
||||
Complex Complex::gonioHelper2(const byte mode) const
|
||||
{
|
||||
Complex c = c_sqr();
|
||||
if (mode == 0)
|
||||
@ -348,19 +349,19 @@ Complex Complex::gonioHelper2(const byte mode)
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_asinh()
|
||||
Complex Complex::c_asinh() const
|
||||
{
|
||||
return gonioHelper2(0);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acosh()
|
||||
Complex Complex::c_acosh() const
|
||||
{
|
||||
return gonioHelper2(1);
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_atanh()
|
||||
Complex Complex::c_atanh() const
|
||||
{
|
||||
Complex c = (*this + one).c_log();
|
||||
c = c - (-(*this - one)).c_log();
|
||||
@ -371,37 +372,37 @@ Complex Complex::c_atanh()
|
||||
//
|
||||
// GONIO HYPERBOLICUS II
|
||||
//
|
||||
Complex Complex::c_csch()
|
||||
Complex Complex::c_csch() const
|
||||
{
|
||||
return one / c_sinh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_sech()
|
||||
Complex Complex::c_sech() const
|
||||
{
|
||||
return one / c_cosh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_coth()
|
||||
Complex Complex::c_coth() const
|
||||
{
|
||||
return one / c_tanh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acsch()
|
||||
Complex Complex::c_acsch() const
|
||||
{
|
||||
return (one / *this).c_asinh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_asech()
|
||||
Complex Complex::c_asech() const
|
||||
{
|
||||
return (one / *this).c_acosh();
|
||||
}
|
||||
|
||||
|
||||
Complex Complex::c_acoth()
|
||||
Complex Complex::c_acoth() const
|
||||
{
|
||||
return (one / *this).c_atanh();
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: Complex.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.4
|
||||
// VERSION: 0.3.0
|
||||
// PURPOSE: Arduino library for Complex math
|
||||
// URL: https://github.com/RobTillaart/Complex
|
||||
// http://arduino.cc/playground/Main/ComplexMath
|
||||
@ -13,7 +13,7 @@
|
||||
#include "Printable.h"
|
||||
|
||||
|
||||
#define COMPLEX_LIB_VERSION (F("0.2.4"))
|
||||
#define COMPLEX_LIB_VERSION (F("0.3.0"))
|
||||
|
||||
|
||||
class Complex: public Printable
|
||||
@ -26,32 +26,32 @@ public:
|
||||
void set(const float r, const float i ) { re = r; im = i; };
|
||||
void setReal(const float r) { re = r; };
|
||||
void setImag(const float i ) { im = i; };
|
||||
float real() { return re; };
|
||||
float imag() { return im; };
|
||||
float real() const { return re; };
|
||||
float imag() const { return im; };
|
||||
|
||||
|
||||
size_t printTo(Print& p) const;
|
||||
|
||||
|
||||
void polar(const float modulus, const float phase);
|
||||
float phase() { return atan2(im, re); };
|
||||
float modulus() { return hypot(re, im); };
|
||||
float phase() const { return atan2(im, re); };
|
||||
float modulus() const { return hypot(re, im); };
|
||||
// conjugate is the number mirrored in x-axis
|
||||
Complex conjugate() { return Complex(re, -im); };
|
||||
Complex reciprocal();
|
||||
Complex conjugate() const { return Complex(re, -im); };
|
||||
Complex reciprocal() const;
|
||||
|
||||
|
||||
bool operator == (const Complex&);
|
||||
bool operator != (const Complex&);
|
||||
bool operator == (const Complex&) const;
|
||||
bool operator != (const Complex&) const;
|
||||
|
||||
|
||||
Complex operator - (); // negation
|
||||
Complex operator - () const; // negation
|
||||
|
||||
|
||||
Complex operator + (const Complex&);
|
||||
Complex operator - (const Complex&);
|
||||
Complex operator * (const Complex&);
|
||||
Complex operator / (const Complex&);
|
||||
Complex operator + (const Complex&) const;
|
||||
Complex operator - (const Complex&) const;
|
||||
Complex operator * (const Complex&) const;
|
||||
Complex operator / (const Complex&) const;
|
||||
|
||||
Complex& operator += (const Complex&);
|
||||
Complex& operator -= (const Complex&);
|
||||
@ -59,57 +59,57 @@ public:
|
||||
Complex& operator /= (const Complex&);
|
||||
|
||||
|
||||
Complex c_sqrt();
|
||||
Complex c_sqr();
|
||||
Complex c_exp();
|
||||
Complex c_log();
|
||||
Complex c_log10();
|
||||
Complex c_pow(const Complex &);
|
||||
Complex c_logn(const Complex &);
|
||||
Complex c_sqrt() const;
|
||||
Complex c_sqr() const;
|
||||
Complex c_exp() const;
|
||||
Complex c_log() const;
|
||||
Complex c_log10() const;
|
||||
Complex c_pow(const Complex &) const;
|
||||
Complex c_logn(const Complex &) const;
|
||||
|
||||
|
||||
Complex c_sin();
|
||||
Complex c_cos();
|
||||
Complex c_tan();
|
||||
Complex c_asin();
|
||||
Complex c_acos();
|
||||
Complex c_atan();
|
||||
Complex c_sin() const;
|
||||
Complex c_cos() const;
|
||||
Complex c_tan() const;
|
||||
Complex c_asin() const;
|
||||
Complex c_acos() const;
|
||||
Complex c_atan() const;
|
||||
|
||||
|
||||
Complex c_csc();
|
||||
Complex c_sec();
|
||||
Complex c_cot();
|
||||
Complex c_acsc();
|
||||
Complex c_asec();
|
||||
Complex c_acot();
|
||||
Complex c_csc() const;
|
||||
Complex c_sec() const;
|
||||
Complex c_cot() const;
|
||||
Complex c_acsc() const;
|
||||
Complex c_asec() const;
|
||||
Complex c_acot() const;
|
||||
|
||||
|
||||
Complex c_sinh();
|
||||
Complex c_cosh();
|
||||
Complex c_tanh();
|
||||
Complex c_asinh();
|
||||
Complex c_acosh();
|
||||
Complex c_atanh();
|
||||
Complex c_sinh() const;
|
||||
Complex c_cosh() const;
|
||||
Complex c_tanh() const;
|
||||
Complex c_asinh() const;
|
||||
Complex c_acosh() const;
|
||||
Complex c_atanh() const;
|
||||
|
||||
|
||||
Complex c_csch();
|
||||
Complex c_sech();
|
||||
Complex c_coth();
|
||||
Complex c_acsch();
|
||||
Complex c_asech();
|
||||
Complex c_acoth();
|
||||
Complex c_csch() const;
|
||||
Complex c_sech() const;
|
||||
Complex c_coth() const;
|
||||
Complex c_acsch() const;
|
||||
Complex c_asech() const;
|
||||
Complex c_acoth() const;
|
||||
|
||||
|
||||
protected:
|
||||
float re;
|
||||
float im;
|
||||
|
||||
Complex gonioHelper1(const byte);
|
||||
Complex gonioHelper2(const byte);
|
||||
Complex gonioHelper1(const byte) const;
|
||||
Complex gonioHelper2(const byte) const;
|
||||
};
|
||||
|
||||
|
||||
static Complex one(1, 0);
|
||||
static const Complex one(1, 0);
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
40
libraries/Complex/examples/const/const.ino
Normal file
40
libraries/Complex/examples/const/const.ino
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// FILE: const.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2021-11-15
|
||||
//
|
||||
// PUPROSE: test complex math - https://github.com/RobTillaart/Complex/issues/7
|
||||
//
|
||||
|
||||
#include "Complex.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print(__FILE__);
|
||||
Serial.print("\n COMPLEX_LIB_VERSION: ");
|
||||
Serial.println(COMPLEX_LIB_VERSION);
|
||||
|
||||
const Complex unity(1, 0);
|
||||
Complex a(1,1);
|
||||
Complex b(2,2);
|
||||
Complex c = unity * a + b;
|
||||
|
||||
Serial.println(a);
|
||||
Serial.println(b);
|
||||
Serial.println(c);
|
||||
|
||||
const Complex d(1,1);
|
||||
// The next line is a warning on AVR
|
||||
// But an error on other platforms (which is correct)
|
||||
// d *= b;
|
||||
Serial.println(d);
|
||||
|
||||
Serial.println("\ndone...");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Complex.git"
|
||||
},
|
||||
"version": "0.2.4",
|
||||
"version": "0.3.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=Complex
|
||||
version=0.2.4
|
||||
version=0.3.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for Complex math.
|
||||
|
Loading…
Reference in New Issue
Block a user