├── .arduino-ci.yml ├── .github ├── FUNDING.yml └── workflows │ ├── arduino-lint.yml │ ├── arduino_test_runner.yml │ └── jsoncheck.yml ├── CHANGELOG.md ├── Complex.cpp ├── Complex.h ├── LICENSE ├── README.md ├── examples ├── array │ └── array.ino ├── complex │ ├── complex.ino │ ├── referenceOutput_0.2.0.txt │ └── referenceOutput_0.3.1.txt ├── const │ └── const.ino └── performance │ ├── performance.ino │ ├── preformanceOutput_0.2.0.txt │ └── preformanceOutput_0.3.1.txt ├── keywords.txt ├── library.json ├── library.properties └── test └── unit_test_001.cpp /.arduino-ci.yml: -------------------------------------------------------------------------------- 1 | platforms: 2 | rpipico: 3 | board: rp2040:rp2040:rpipico 4 | package: rp2040:rp2040 5 | gcc: 6 | features: 7 | defines: 8 | - ARDUINO_ARCH_RP2040 9 | warnings: 10 | flags: 11 | 12 | packages: 13 | rp2040:rp2040: 14 | url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json 15 | 16 | compile: 17 | # Choosing to run compilation tests on 2 different Arduino platforms 18 | platforms: 19 | - uno 20 | # - due 21 | # - zero 22 | # - leonardo 23 | - m4 24 | - esp32 25 | # - esp8266 26 | # - mega2560 27 | - rpipico 28 | 29 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: RobTillaart 4 | custom: "https://www.paypal.me/robtillaart" 5 | -------------------------------------------------------------------------------- /.github/workflows/arduino-lint.yml: -------------------------------------------------------------------------------- 1 | name: Arduino-lint 2 | 3 | on: [push, pull_request] 4 | jobs: 5 | lint: 6 | runs-on: ubuntu-latest 7 | timeout-minutes: 5 8 | steps: 9 | - uses: actions/checkout@v4 10 | - uses: arduino/arduino-lint-action@v1 11 | with: 12 | library-manager: update 13 | compliance: strict -------------------------------------------------------------------------------- /.github/workflows/arduino_test_runner.yml: -------------------------------------------------------------------------------- 1 | name: Arduino CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | runTest: 7 | runs-on: ubuntu-latest 8 | timeout-minutes: 20 9 | 10 | steps: 11 | - uses: actions/checkout@v4 12 | - uses: ruby/setup-ruby@v1 13 | with: 14 | ruby-version: 2.6 15 | - run: | 16 | gem install arduino_ci 17 | arduino_ci.rb 18 | -------------------------------------------------------------------------------- /.github/workflows/jsoncheck.yml: -------------------------------------------------------------------------------- 1 | name: JSON check 2 | 3 | on: 4 | push: 5 | paths: 6 | - '**.json' 7 | pull_request: 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | timeout-minutes: 5 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: json-syntax-check 16 | uses: limitusus/json-syntax-check@v2 17 | with: 18 | pattern: "\\.json$" -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log Complex 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/). 7 | 8 | 9 | ## [0.3.4] - 2024-01-02 10 | - fix examples 11 | - update readme.md 12 | - minor edits 13 | 14 | 15 | ## [0.3.3] - 2023-10-18 16 | - update readme.md 17 | 18 | ## [0.3.2] - 2022-10-29 19 | - add changelog.md 20 | - add rp2040 to build-CI 21 | - minor edit unit test 22 | 23 | ## [0.3.1] - 2021-12-14 24 | - update library.json 25 | - update license 26 | - minor edits 27 | 28 | ## [0.3.0] - 2021-11-15 29 | - fix #7 adding const to operators 30 | 31 | ---- 32 | 33 | ## [0.2.4] - 2021-10-19 34 | - update build-CI. 35 | 36 | ## [0.2.3] - 2021-09-14 37 | - fix build-CI 38 | - update readme.md 39 | 40 | ## [0.2.2] - 2020-12-16 41 | - add arduino-ci + unit test (starter) 42 | - setReal(), setImag() 43 | 44 | ## [0.2.1] - 2020-06-05 45 | - fix library.json 46 | 47 | ## [0.2.0] - 2020-03-29 48 | - #pragma once, 49 | - create own repo 50 | 51 | ---- 52 | 53 | ## [0.1.12] - 2018-04-02 54 | - fix issue #33 double -> float 55 | 56 | ## [0.1.11] - 2018-01-29 57 | - fix sin and cos formula - issue #91 58 | 59 | ## [0.1.10] - 2018-01-15 60 | - uppercase #define COMPLEX_H 61 | 62 | ## [0.1.09] - 2016-10-15 63 | - added (0,0) constructor 64 | 65 | ## [0.1.08] - 2015-06-03 66 | - refactor 67 | 68 | ## [0.1.07] - 2015-06-03 69 | - refactor interfaces 70 | 71 | some missing history 72 | 73 | ## [0.1.0] - 2013-09-23 74 | - initial version. 75 | 76 | -------------------------------------------------------------------------------- /Complex.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: Complex.cpp 3 | // AUTHOR: Rob Tillaart 4 | // VERSION: 0.3.4 5 | // PURPOSE: Arduino library for Complex math 6 | // URL: https://github.com/RobTillaart/Complex 7 | // http://arduino.cc/playground/Main/ComplexMath 8 | 9 | 10 | #include "Complex.h" 11 | 12 | 13 | // PRINTING 14 | size_t Complex::printTo(Print& p) const 15 | { 16 | size_t n = 0; 17 | n += p.print(re, 3); 18 | n += p.print(' '); 19 | n += p.print(im, 3); 20 | n += p.print('i'); 21 | return n; 22 | }; 23 | 24 | 25 | void Complex::polar(const float modulus, const float phase) 26 | { 27 | re = modulus * cos(phase); 28 | im = modulus * sin(phase); 29 | } 30 | 31 | 32 | Complex Complex::reciprocal() const 33 | { 34 | float f = 1.0 / (re * re + im * im); 35 | float r = re * f; 36 | float i = -im * f; 37 | return Complex(r, i); 38 | } 39 | 40 | // 41 | // EQUALITIES 42 | // 43 | bool Complex::operator == (const Complex &c) const 44 | { 45 | return (re == c.re) && (im == c.im); 46 | } 47 | 48 | 49 | bool Complex::operator != (const Complex &c) const 50 | { 51 | return (re != c.re) || (im != c.im); 52 | } 53 | 54 | 55 | // 56 | // NEGATE 57 | // 58 | Complex Complex::operator - () const 59 | { 60 | return Complex(-re, -im); 61 | } 62 | 63 | 64 | // 65 | // BASIC MATH 66 | // 67 | Complex Complex::operator + (const Complex &c) const 68 | { 69 | return Complex(re + c.re, im + c.im); 70 | } 71 | 72 | 73 | Complex Complex::operator - (const Complex &c) const 74 | { 75 | return Complex(re - c.re, im - c.im); 76 | } 77 | 78 | 79 | Complex Complex::operator * (const Complex &c) const 80 | { 81 | float r = re * c.re - im * c.im; 82 | float i = re * c.im + im * c.re; 83 | return Complex(r, i); 84 | } 85 | 86 | 87 | Complex Complex::operator / (const Complex &c) const 88 | { 89 | float f = 1.0/(c.re * c.re + c.im * c.im); 90 | float r = (re * c.re + im * c.im) * f; 91 | float i = (im * c.re - re * c.im) * f; 92 | return Complex(r, i); 93 | } 94 | 95 | 96 | Complex& Complex::operator += (const Complex &c) 97 | { 98 | re += c.re; 99 | im += c.im; 100 | return *this; 101 | } 102 | 103 | 104 | Complex& Complex::operator -= (const Complex &c) 105 | { 106 | re -= c.re; 107 | im -= c.im; 108 | return *this; 109 | } 110 | 111 | 112 | Complex& Complex::operator *= (const Complex &c) 113 | { 114 | float r = re * c.re - im * c.im; 115 | float i = re * c.im + im * c.re; 116 | re = r; 117 | im = i; 118 | return *this; 119 | } 120 | 121 | 122 | Complex& Complex::operator /= (const Complex &c) 123 | { 124 | float f = 1.0/(c.re * c.re + c.im * c.im); 125 | float r = (re * c.re + im * c.im) * f; 126 | float i = (im * c.re - re * c.im) * f; 127 | re = r; 128 | im = i; 129 | return *this; 130 | } 131 | 132 | 133 | // 134 | // POWER FUNCTIONS 135 | // 136 | Complex Complex::c_sqr() const 137 | { 138 | float r = re * re - im * im; 139 | float i = 2 * re * im; 140 | return Complex(r, i); 141 | } 142 | 143 | 144 | Complex Complex::c_sqrt() const 145 | { 146 | float m = modulus(); 147 | float r = sqrt(0.5 * (m + re)); 148 | float i = sqrt(0.5 * (m - re)); 149 | if (im < 0) i = -i; 150 | return Complex(r, i); 151 | } 152 | 153 | 154 | Complex Complex::c_exp() const 155 | { 156 | float e = exp(re); 157 | return Complex(e * cos(im), e * sin(im)); 158 | } 159 | 160 | 161 | Complex Complex::c_log() const 162 | { 163 | float m = modulus(); 164 | float p = phase(); 165 | if (p > PI) p -= 2 * PI; 166 | return Complex(log(m), p); 167 | } 168 | 169 | 170 | Complex Complex::c_pow(const Complex &c) const 171 | { 172 | Complex t = c_log(); 173 | t = t * c; 174 | return t.c_exp(); 175 | } 176 | 177 | 178 | Complex Complex::c_logn(const Complex &c) const 179 | { 180 | Complex t = c; 181 | return c_log()/t.c_log(); 182 | } 183 | 184 | 185 | Complex Complex::c_log10() const 186 | { 187 | return c_logn(10); 188 | } 189 | 190 | 191 | // 192 | // GONIO I - SIN COS TAN 193 | // 194 | Complex Complex::c_sin() const 195 | { 196 | return Complex(sin(re) * cosh(im), cos(re) * sinh(im)); 197 | } 198 | 199 | 200 | Complex Complex::c_cos() const 201 | { 202 | return Complex(cos(re) * cosh(im), -sin(re) * sinh(im)); 203 | } 204 | 205 | 206 | Complex Complex::c_tan() const 207 | { 208 | /* faster but 350 bytes longer!! 209 | float s = sin(re); 210 | float c = cos(re); 211 | float sh = sinh(im); 212 | float ch = cosh(im); 213 | // return Complex(s*ch, c*sh) / Complex(c*ch, -s*sh); 214 | float r0 = s*ch; 215 | float i0 = c*sh; 216 | float cre = c*ch; 217 | float cim = -s*sh; 218 | float f = 1.0/(cre*cre + cim*cim); 219 | float r = r0 * cre + i0 * cim; 220 | float i = r0 * cim - i0 * cre; 221 | return Complex(r * f, -i * f); 222 | */ 223 | return c_sin() / c_cos(); 224 | } 225 | 226 | 227 | Complex Complex::gonioHelper1(const byte mode) const 228 | { 229 | Complex c = (one - this->c_sqr()).c_sqrt(); 230 | if (mode == 0) 231 | { 232 | c = c + *this * Complex(0,-1); 233 | } 234 | else 235 | { 236 | c = *this + c * Complex(0,-1); 237 | } 238 | c = c.c_log() * Complex(0,1); 239 | return c; 240 | } 241 | 242 | 243 | Complex Complex::c_asin() const 244 | { 245 | return gonioHelper1(0); 246 | } 247 | 248 | 249 | Complex Complex::c_acos() const 250 | { 251 | return gonioHelper1(1); 252 | } 253 | 254 | 255 | Complex Complex::c_atan() const 256 | { 257 | return (Complex(0,-1) * (Complex(re, im - 1)/Complex(-re, -im - 1)).c_log()) * 0.5; 258 | } 259 | 260 | 261 | // 262 | // GONIO II - CSC SEC COT 263 | // 264 | Complex Complex::c_csc() const 265 | { 266 | return one / c_sin(); 267 | } 268 | 269 | 270 | Complex Complex::c_sec() const 271 | { 272 | return one / c_cos(); 273 | } 274 | 275 | 276 | Complex Complex::c_cot() const 277 | { 278 | return one / c_tan(); 279 | } 280 | 281 | 282 | Complex Complex::c_acsc() const 283 | { 284 | return (one / *this).c_asin(); 285 | } 286 | 287 | 288 | Complex Complex::c_asec() const 289 | { 290 | return (one / *this).c_acos(); 291 | } 292 | 293 | 294 | Complex Complex::c_acot() const 295 | { 296 | return (one / *this).c_atan(); 297 | } 298 | 299 | 300 | // 301 | // GONIO HYPERBOLICUS I 302 | // 303 | Complex Complex::c_sinh() const 304 | { 305 | return Complex(cos(im) * sinh(re), sin(im) * cosh(re)); 306 | } 307 | 308 | 309 | Complex Complex::c_cosh() const 310 | { 311 | return Complex(cos(im) * cosh(re), sin(im) * sinh(re)); 312 | } 313 | 314 | 315 | Complex Complex::c_tanh() const 316 | { 317 | return c_sinh() / c_cosh(); 318 | } 319 | 320 | 321 | Complex Complex::gonioHelper2(const byte mode) const 322 | { 323 | Complex c = c_sqr(); 324 | if (mode == 0) 325 | { 326 | c += 1; 327 | } 328 | else 329 | { 330 | c -= 1; 331 | } 332 | c = (*this + c.c_sqrt()).c_log(); 333 | return c; 334 | } 335 | 336 | 337 | Complex Complex::c_asinh() const 338 | { 339 | return gonioHelper2(0); 340 | } 341 | 342 | 343 | Complex Complex::c_acosh() const 344 | { 345 | return gonioHelper2(1); 346 | } 347 | 348 | 349 | Complex Complex::c_atanh() const 350 | { 351 | Complex c = (*this + one).c_log(); 352 | c = c - (-(*this - one)).c_log(); 353 | return c * 0.5; 354 | } 355 | 356 | 357 | // 358 | // GONIO HYPERBOLICUS II 359 | // 360 | Complex Complex::c_csch() const 361 | { 362 | return one / c_sinh(); 363 | } 364 | 365 | 366 | Complex Complex::c_sech() const 367 | { 368 | return one / c_cosh(); 369 | } 370 | 371 | 372 | Complex Complex::c_coth() const 373 | { 374 | return one / c_tanh(); 375 | } 376 | 377 | 378 | Complex Complex::c_acsch() const 379 | { 380 | return (one / *this).c_asinh(); 381 | } 382 | 383 | 384 | Complex Complex::c_asech() const 385 | { 386 | return (one / *this).c_acosh(); 387 | } 388 | 389 | 390 | Complex Complex::c_acoth() const 391 | { 392 | return (one / *this).c_atanh(); 393 | } 394 | 395 | 396 | // --- END OF FILE --- 397 | 398 | -------------------------------------------------------------------------------- /Complex.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // 3 | // FILE: Complex.h 4 | // AUTHOR: Rob Tillaart 5 | // VERSION: 0.3.4 6 | // PURPOSE: Arduino library for Complex math 7 | // URL: https://github.com/RobTillaart/Complex 8 | // http://arduino.cc/playground/Main/ComplexMath 9 | 10 | 11 | #include "Arduino.h" 12 | #include "Printable.h" 13 | 14 | 15 | #define COMPLEX_LIB_VERSION (F("0.3.4")) 16 | 17 | 18 | class Complex: public Printable 19 | { 20 | public: 21 | Complex(const float r = 0, const float i = 0) : re(r), im(i) {}; 22 | Complex(const Complex &c) : re(c.re), im(c.im) {}; 23 | 24 | 25 | void set(const float r, const float i ) { re = r; im = i; }; 26 | void setReal(const float r) { re = r; }; 27 | void setImag(const float i ) { im = i; }; 28 | float real() const { return re; }; 29 | float imag() const { return im; }; 30 | 31 | 32 | size_t printTo(Print& p) const; 33 | 34 | 35 | void polar(const float modulus, const float phase); 36 | float phase() const { return atan2(im, re); }; 37 | float modulus() const { return hypot(re, im); }; 38 | // conjugate is the number mirrored in x-axis 39 | Complex conjugate() const { return Complex(re, -im); }; 40 | Complex reciprocal() const; 41 | 42 | 43 | bool operator == (const Complex&) const; 44 | bool operator != (const Complex&) const; 45 | 46 | 47 | Complex operator - () const; // negation 48 | 49 | 50 | Complex operator + (const Complex&) const; 51 | Complex operator - (const Complex&) const; 52 | Complex operator * (const Complex&) const; 53 | Complex operator / (const Complex&) const; 54 | 55 | Complex& operator += (const Complex&); 56 | Complex& operator -= (const Complex&); 57 | Complex& operator *= (const Complex&); 58 | Complex& operator /= (const Complex&); 59 | 60 | 61 | Complex c_sqrt() const; 62 | Complex c_sqr() const; 63 | Complex c_exp() const; 64 | Complex c_log() const; 65 | Complex c_log10() const; 66 | Complex c_pow(const Complex &) const; 67 | Complex c_logn(const Complex &) const; 68 | 69 | 70 | Complex c_sin() const; 71 | Complex c_cos() const; 72 | Complex c_tan() const; 73 | Complex c_asin() const; 74 | Complex c_acos() const; 75 | Complex c_atan() const; 76 | 77 | 78 | Complex c_csc() const; 79 | Complex c_sec() const; 80 | Complex c_cot() const; 81 | Complex c_acsc() const; 82 | Complex c_asec() const; 83 | Complex c_acot() const; 84 | 85 | 86 | Complex c_sinh() const; 87 | Complex c_cosh() const; 88 | Complex c_tanh() const; 89 | Complex c_asinh() const; 90 | Complex c_acosh() const; 91 | Complex c_atanh() const; 92 | 93 | 94 | Complex c_csch() const; 95 | Complex c_sech() const; 96 | Complex c_coth() const; 97 | Complex c_acsch() const; 98 | Complex c_asech() const; 99 | Complex c_acoth() const; 100 | 101 | 102 | protected: 103 | float re; 104 | float im; 105 | 106 | Complex gonioHelper1(const byte) const; 107 | Complex gonioHelper2(const byte) const; 108 | }; 109 | 110 | 111 | static const Complex one(1, 0); 112 | 113 | 114 | // -- END OF FILE -- 115 | 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013-2024 Rob Tillaart 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![Arduino CI](https://github.com/RobTillaart/Complex/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) 3 | [![Arduino-lint](https://github.com/RobTillaart/Complex/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Complex/actions/workflows/arduino-lint.yml) 4 | [![JSON check](https://github.com/RobTillaart/Complex/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Complex/actions/workflows/jsoncheck.yml) 5 | [![GitHub issues](https://img.shields.io/github/issues/RobTillaart/Complex.svg)](https://github.com/RobTillaart/Complex/issues) 6 | 7 | [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Complex/blob/master/LICENSE) 8 | [![GitHub release](https://img.shields.io/github/release/RobTillaart/Complex.svg?maxAge=3600)](https://github.com/RobTillaart/Complex/releases) 9 | [![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/Complex.svg)](https://registry.platformio.org/libraries/robtillaart/Complex) 10 | 11 | 12 | # Complex 13 | 14 | Arduino library for Complex mathematical operations. 15 | 16 | 17 | ## Description 18 | 19 | This library defines the complex data type and all the common mathematical functions for it. 20 | 21 | These functions include basic = "+ - \* /" and also power and goniometric functions. 22 | 23 | The library implements the **Printable** interface so one can directly print the complex values 24 | in any stream e.g. Serial. 25 | 26 | 27 | ## Interface 28 | 29 | ```cpp 30 | #include "Complex.h" 31 | ``` 32 | 33 | See Complex.h for a full list of functions implemented. 34 | 35 | The library uses **float** for the real and imaginary part so precision is limited. 36 | Reasons are memory and performance, see also Future section below. 37 | 38 | The library implements the constant **one** as this value is often used in the code. 39 | 40 | 41 | ## Note 42 | 43 | The library has a big footprint so it fills up the memory of an UNO quite fast, 44 | especially if all functionality is used. 45 | 46 | 47 | #### Known problem 48 | 49 | Issue found in version 0.1.9 - https://github.com/RobTillaart/Arduino/issues/90 50 | Class does not compile for DUE and TEENSY 51 | Also Raspberry Pico - https://github.com/RobTillaart/Complex/issues/6 52 | 53 | Apparently the name "Complex" is already in use (reserved) by some non-AVR compilers 54 | so it won't include the Complex.h file. Problem seen on Due and Teensy3.5 55 | 56 | 57 | #### Solution 58 | 59 | - Make a copy of the Complex Library and rename the folder to CComplex 60 | - Rename Complex.h to CComplex.h 61 | - Rename Complex.cpp to CComplex.cpp 62 | - change one line in CComplex.cpp to include CComplex.h 63 | - note your sketches need to include CComplex.h too. 64 | 65 | 66 | ## Future 67 | 68 | #### Must 69 | 70 | - improve documentation 71 | 72 | #### Should 73 | 74 | #### Could 75 | 76 | - create a (8 byte) double based variant for high precision e.g. Complex8() 77 | - Note that some platforms map double to float 78 | - others support float in hardware etc. 79 | - so expect a big difference in both memory and performance. 80 | 81 | #### Wont 82 | 83 | - create the constant **i** ?? 84 | - => expect conflicts with int i... 85 | 86 | 87 | ## Support 88 | 89 | If you appreciate my libraries, you can support the development and maintenance. 90 | Improve the quality of the libraries by providing issues and Pull Requests, or 91 | donate through PayPal or GitHub sponsors. 92 | 93 | Thank you, 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/array/array.ino: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: array.ino 3 | // AUTHOR: Rob Tillaart 4 | // PURPOSE: example complex array 5 | // URL: https://github.com/RobTillaart/Complex 6 | 7 | 8 | #include "Complex.h" 9 | 10 | Complex samples[100]; 11 | 12 | 13 | void setup() 14 | { 15 | Serial.begin(115200); 16 | Serial.println(__FILE__); 17 | Serial.print("\n Complex numbers array example: "); 18 | Serial.println(COMPLEX_LIB_VERSION); 19 | 20 | Serial.println("\n1. Read into array"); 21 | for (int i = 0; i < 100; i++) 22 | { 23 | float re = analogRead(A0) / 1023.0; 24 | float im = analogRead(A1) / 1023.0; 25 | samples[i].set(re, im); 26 | } 27 | 28 | Serial.println("\n2. Print array"); 29 | for (int i = 0; i < 100; i++) 30 | { 31 | if (i % 5 == 0) Serial.println(); 32 | Serial.print(samples[i]); 33 | Serial.print("\t"); 34 | } 35 | Serial.println(); 36 | 37 | Serial.println("\n3. Sum array"); 38 | Complex sum; 39 | for (int i = 0; i < 100; i++) 40 | { 41 | sum += samples[i]; 42 | } 43 | Serial.println(sum); 44 | 45 | Serial.println("\n done"); 46 | } 47 | 48 | 49 | void loop() 50 | { 51 | } 52 | 53 | 54 | 55 | // -- END OF FILE -- 56 | 57 | -------------------------------------------------------------------------------- /examples/complex/complex.ino: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: complex.ino 3 | // AUTHOR: Rob Tillaart 4 | // DATE: 2013-09-23 5 | // PURPOSE: demo complex 6 | // URL: https://github.com/RobTillaart/Complex 7 | // 8 | // Serial.print(Complex) supported since 0.1.05 9 | 10 | 11 | #include "Complex.h" 12 | 13 | 14 | void setup() 15 | { 16 | Serial.begin(115200); 17 | Serial.print("\n Complex numbers test for Arduino: "); 18 | Serial.println(COMPLEX_LIB_VERSION); 19 | Serial.println("\n1. Print Complex, set, real, imag"); 20 | 21 | Complex c1(10.0, -2.0); 22 | Complex c2(3, 0); 23 | Complex c3(-10, 4); 24 | Complex c4(-5,-5); 25 | Complex c5(0, 0); 26 | 27 | Serial.println(one); 28 | Serial.println(c1); 29 | Serial.println(c2); 30 | Serial.println(c3); 31 | Serial.println(c4); 32 | c3.set(0,0); 33 | Serial.println(c3); 34 | Serial.println(c3.real()); 35 | Serial.println(c3.imag()); 36 | 37 | Serial.println("\n2. == != "); 38 | c5 = c1; 39 | if (c5 == c1) Serial.println("ok :)"); 40 | else Serial.println(" fail :("); 41 | c5 = c1 + 1; 42 | if (c5 != c1) Serial.println("ok :)"); 43 | else Serial.println(" fail :("); 44 | c5 = 3; 45 | if (c5 == 3) Serial.println("ok :)"); 46 | else Serial.println(" fail :("); 47 | 48 | 49 | Serial.println("\n3. negation - "); 50 | c5 = -c1; 51 | Serial.println(c5); 52 | c5 = -c5; 53 | Serial.println(c5); 54 | if (c5 == c1) Serial.println("ok :)"); 55 | else Serial.println(" fail :("); 56 | 57 | 58 | Serial.println("\n4. + - "); 59 | c5 = c1 + c2; 60 | Serial.println(c5); 61 | c5 = c1 + 3; 62 | Serial.println(c5); 63 | c5 = c1 - c2; 64 | Serial.println(c5); 65 | c5 = c1 - 3; 66 | Serial.println(c5); 67 | 68 | 69 | Serial.println("\n5. * / "); 70 | c5 = c1 * c2; 71 | Serial.println(c5); 72 | c5 = c5 * 3; 73 | Serial.println(c5); 74 | c5 = c5 / c2; 75 | Serial.println(c5); 76 | c5 = c5 / 3; 77 | Serial.println(c5); 78 | 79 | c5 = c1 / c2 * c2; 80 | Serial.println(c5); 81 | c5 = c1 * c4 / c4; 82 | Serial.println(c5); 83 | 84 | 85 | Serial.println("\n6. assign += -= *= /="); 86 | c5 = c1; 87 | c5 += c1; 88 | Serial.println(c5); 89 | c5 += 3; 90 | Serial.println(c5); 91 | c5 -= c1; 92 | Serial.println(c5); 93 | c5 -= 3; 94 | Serial.println(c5); 95 | c5 *= c1; 96 | Serial.println(c5); 97 | c5 *= 3; 98 | Serial.println(c5); 99 | c5 /= c1; 100 | Serial.println(c5); 101 | c5 /= 3; 102 | Serial.println(c5); 103 | 104 | 105 | Serial.println("\n7. phase modulus polar"); 106 | Serial.println(c1); 107 | double m = c1.modulus(); 108 | Serial.println(m); 109 | double p = c1.phase(); 110 | Serial.println(p); 111 | c5.polar(m, p); 112 | Serial.println(c5); 113 | 114 | 115 | Serial.println("\n8. conjugate reciprocal"); 116 | c5 = c1.conjugate(); 117 | Serial.println(c5); 118 | c5 = c5.conjugate(); 119 | Serial.println(c5); 120 | c5 = c1.reciprocal(); 121 | Serial.println(c5); 122 | c5 = c5.reciprocal(); 123 | Serial.println(c5); 124 | 125 | Serial.println("\n9. power: exp log pow sqrt sqr logn log10"); 126 | c5 = c1.c_sqr(); 127 | Serial.println(c5); 128 | c5 = c1.c_exp(); 129 | Serial.println(c5); 130 | c5 = c5.c_log(); 131 | Serial.println(c5); 132 | c5 = c1.c_pow(2); 133 | Serial.println(c5); 134 | c5 = c5.c_sqrt(); 135 | Serial.println(c5); 136 | c5 = c5.c_sqr(); 137 | Serial.println(c5); 138 | c5 = c1.c_pow(c2); 139 | Serial.println(c5); 140 | c5 = c5.c_pow(c2.reciprocal()); 141 | Serial.println(c5); 142 | c5 = c5.c_logn(c4); 143 | Serial.println(c5); 144 | c5 = c4.c_pow(c5); 145 | Serial.println(c5); 146 | c5 = c5.c_log10(); 147 | Serial.println(c5); 148 | 149 | Serial.println("\n10. gonio: sin cos tan asin acos atan"); 150 | c1.set(0.5, 0.5); 151 | c5 = c1.c_sin(); 152 | Serial.println(c5); 153 | c5 = c5.c_asin(); 154 | Serial.println(c5); 155 | c5 = c1.c_cos(); 156 | Serial.println(c5); 157 | c5 = c5.c_acos(); 158 | Serial.println(c5); 159 | c5 = c1.c_tan(); 160 | Serial.println(c5); 161 | c5 = c5.c_atan(); 162 | Serial.println(c5); 163 | 164 | Serial.println("\n11. gonio csc sec cot acsc asec acot"); 165 | c1.set(0.5, 0.5); 166 | c5 = c1.c_csc(); 167 | Serial.println(c5); 168 | c5 = c5.c_acsc(); 169 | Serial.println(c5); 170 | c5 = c1.c_sec(); 171 | Serial.println(c5); 172 | c5 = c5.c_asec(); 173 | Serial.println(c5); 174 | c5 = c1.c_cot(); 175 | Serial.println(c5); 176 | c5 = c5.c_acot(); 177 | Serial.println(c5); 178 | 179 | Serial.println("\n12. gonio hyperbolicus I "); 180 | c1.set(0.5, 0.5); 181 | c5 = c1.c_sinh(); 182 | Serial.println(c5); 183 | c5 = c5.c_asinh(); 184 | Serial.println(c5); 185 | c5 = c1.c_cosh(); 186 | Serial.println(c5); 187 | c5 = c5.c_acosh(); 188 | Serial.println(c5); 189 | c5 = c1.c_tanh(); 190 | Serial.println(c5); 191 | c5 = c5.c_atanh(); 192 | Serial.println(c5); 193 | 194 | Serial.println("\n13. gonio hyperbolicus II "); 195 | c1.set(0.5, 0.5); 196 | c5 = c1.c_csch(); 197 | Serial.println(c5); 198 | c5 = c5.c_acsch(); 199 | Serial.println(c5); 200 | c5 = c1.c_sech(); 201 | Serial.println(c5); 202 | c5 = c5.c_asech(); 203 | Serial.println(c5); 204 | c5 = c1.c_coth(); 205 | Serial.println(c5); 206 | c5 = c5.c_acoth(); 207 | Serial.println(c5); 208 | 209 | Serial.println("\n14. gonio bug fix (minimal) test"); 210 | c1.set(3, 4); 211 | Serial.println(c1); 212 | c2 = c1.c_sin(); 213 | c2 = c2 * c2; 214 | c3 = c1.c_cos(); 215 | c3 = c3 * c3; 216 | Serial.println(c2 + c3); // should print 1, 0i 217 | 218 | Serial.println("\n.. Complex done"); 219 | 220 | uint32_t start = micros(); 221 | for (int i = 0; i < 1000; i++) 222 | { 223 | c5 = c5.c_csc(); 224 | } 225 | uint32_t dur = micros() - start; 226 | Serial.println(dur); 227 | Serial.println(one); 228 | } 229 | 230 | 231 | void loop() 232 | { 233 | } 234 | 235 | 236 | // -- END OF FILE -- 237 | 238 | -------------------------------------------------------------------------------- /examples/complex/referenceOutput_0.2.0.txt: -------------------------------------------------------------------------------- 1 | Complex numbers test for Arduino: 0.2.0 2 | 3 | 1. Print Complex, set, real, imag 4 | 1.000 0.000i 5 | 10.000 -2.000i 6 | 3.000 0.000i 7 | -10.000 4.000i 8 | -5.000 -5.000i 9 | 0.000 0.000i 10 | 0.00 11 | 0.00 12 | 13 | 2. == != 14 | ok :) 15 | ok :) 16 | ok :) 17 | 18 | 3. negation - 19 | -10.000 2.000i 20 | 10.000 -2.000i 21 | ok :) 22 | 23 | 4. + - 24 | 13.000 -2.000i 25 | 13.000 -2.000i 26 | 7.000 -2.000i 27 | 7.000 -2.000i 28 | 29 | 5. * / 30 | 30.000 -6.000i 31 | 90.000 -18.000i 32 | 30.000 -6.000i 33 | 10.000 -2.000i 34 | 10.000 -2.000i 35 | 10.000 -2.000i 36 | 37 | 6. assign += -= *= /= 38 | 20.000 -4.000i 39 | 23.000 -4.000i 40 | 13.000 -2.000i 41 | 10.000 -2.000i 42 | 96.000 -40.000i 43 | 288.000 -120.000i 44 | 30.000 -6.000i 45 | 10.000 -2.000i 46 | 47 | 7. phase modulus polar 48 | 10.000 -2.000i 49 | 10.20 50 | -0.20 51 | 10.000 -2.000i 52 | 53 | 8. conjugate reciprocal 54 | 10.000 2.000i 55 | 10.000 -2.000i 56 | 0.096 0.019i 57 | 10.000 -2.000i 58 | 59 | 9. power: exp log pow sqrt sqr logn log10 60 | 96.000 -40.000i 61 | -9166.239 -20028.597i 62 | 10.000 -2.000i 63 | 96.000 -40.000i 64 | 10.000 -2.000i 65 | 96.000 -40.000i 66 | 880.000 -592.000i 67 | 10.000 -2.000i 68 | 0.534 0.542i 69 | 10.000 -2.000i 70 | 1.009 -0.086i 71 | 72 | 10. gonio: sin cos tan asin acos atan 73 | 0.541 0.457i 74 | 0.500 0.500i 75 | 0.990 -0.250i 76 | 0.500 0.500i 77 | 0.404 0.564i 78 | 0.500 0.500i 79 | 80 | 11. gonio csc sec cot acsc asec acot 81 | 1.078 -0.912i 82 | 0.500 0.500i 83 | 0.950 0.240i 84 | 0.500 0.500i 85 | 0.839 -1.172i 86 | 0.500 0.500i 87 | 88 | 12. gonio hyperbolicus I 89 | 0.457 0.541i 90 | 0.500 0.500i 91 | 0.990 0.250i 92 | 0.500 0.500i 93 | 0.564 0.404i 94 | 0.500 0.500i 95 | 96 | 13. gonio hyperbolicus II 97 | 0.912 -1.078i 98 | 0.500 0.500i 99 | 0.950 -0.240i 100 | 0.500 0.500i 101 | 1.172 -0.839i 102 | 0.500 0.500i 103 | 104 | 14. gonio bug fix (minimal) test 105 | 3.000 4.000i 106 | 1.000 0.000i 107 | 108 | .. Complex done 109 | 521964 110 | 1.000 0.000i 111 | -------------------------------------------------------------------------------- /examples/complex/referenceOutput_0.3.1.txt: -------------------------------------------------------------------------------- 1 | Complex numbers test for Arduino: 0.3.1 2 | 3 | 1. Print Complex, set, real, imag 4 | 1.000 0.000i 5 | 10.000 -2.000i 6 | 3.000 0.000i 7 | -10.000 4.000i 8 | -5.000 -5.000i 9 | 0.000 0.000i 10 | 0.00 11 | 0.00 12 | 13 | 2. == != 14 | ok :) 15 | ok :) 16 | ok :) 17 | 18 | 3. negation - 19 | -10.000 2.000i 20 | 10.000 -2.000i 21 | ok :) 22 | 23 | 4. + - 24 | 13.000 -2.000i 25 | 13.000 -2.000i 26 | 7.000 -2.000i 27 | 7.000 -2.000i 28 | 29 | 5. * / 30 | 30.000 -6.000i 31 | 90.000 -18.000i 32 | 30.000 -6.000i 33 | 10.000 -2.000i 34 | 10.000 -2.000i 35 | 10.000 -2.000i 36 | 37 | 6. assign += -= *= /= 38 | 20.000 -4.000i 39 | 23.000 -4.000i 40 | 13.000 -2.000i 41 | 10.000 -2.000i 42 | 96.000 -40.000i 43 | 288.000 -120.000i 44 | 30.000 -6.000i 45 | 10.000 -2.000i 46 | 47 | 7. phase modulus polar 48 | 10.000 -2.000i 49 | 10.20 50 | -0.20 51 | 10.000 -2.000i 52 | 53 | 8. conjugate reciprocal 54 | 10.000 2.000i 55 | 10.000 -2.000i 56 | 0.096 0.019i 57 | 10.000 -2.000i 58 | 59 | 9. power: exp log pow sqrt sqr logn log10 60 | 96.000 -40.000i 61 | -9166.239 -20028.597i 62 | 10.000 -2.000i 63 | 96.000 -40.000i 64 | 10.000 -2.000i 65 | 96.000 -40.000i 66 | 880.000 -592.000i 67 | 10.000 -2.000i 68 | 0.534 0.542i 69 | 10.000 -2.000i 70 | 1.009 -0.086i 71 | 72 | 10. gonio: sin cos tan asin acos atan 73 | 0.541 0.457i 74 | 0.500 0.500i 75 | 0.990 -0.250i 76 | 0.500 0.500i 77 | 0.404 0.564i 78 | 0.500 0.500i 79 | 80 | 11. gonio csc sec cot acsc asec acot 81 | 1.078 -0.912i 82 | 0.500 0.500i 83 | 0.950 0.240i 84 | 0.500 0.500i 85 | 0.839 -1.172i 86 | 0.500 0.500i 87 | 88 | 12. gonio hyperbolicus I 89 | 0.457 0.541i 90 | 0.500 0.500i 91 | 0.990 0.250i 92 | 0.500 0.500i 93 | 0.564 0.404i 94 | 0.500 0.500i 95 | 96 | 13. gonio hyperbolicus II 97 | 0.912 -1.078i 98 | 0.500 0.500i 99 | 0.950 -0.240i 100 | 0.500 0.500i 101 | 1.172 -0.839i 102 | 0.500 0.500i 103 | 104 | 14. gonio bug fix (minimal) test 105 | 3.000 4.000i 106 | 1.000 0.000i 107 | 108 | .. Complex done 109 | 521964 110 | 1.000 0.000i 111 | -------------------------------------------------------------------------------- /examples/const/const.ino: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: const.ino 3 | // AUTHOR: Rob Tillaart 4 | // DATE: 2021-11-15 5 | // PURPOSE: test complex math - https://github.com/RobTillaart/Complex/issues/7 6 | // URL: https://github.com/RobTillaart/Complex 7 | 8 | 9 | #include "Complex.h" 10 | 11 | 12 | void setup() 13 | { 14 | Serial.begin(115200); 15 | Serial.print(__FILE__); 16 | Serial.print("\n COMPLEX_LIB_VERSION: "); 17 | Serial.println(COMPLEX_LIB_VERSION); 18 | 19 | const Complex unity(1, 0); 20 | Complex a(1,1); 21 | Complex b(2,2); 22 | Complex c = unity * a + b; 23 | 24 | Serial.println(a); 25 | Serial.println(b); 26 | Serial.println(c); 27 | 28 | const Complex d(1,1); 29 | // The next line is a warning on AVR 30 | // But an error on other platforms (which is correct) 31 | // d *= b; 32 | Serial.println(d); 33 | 34 | Serial.println("\ndone..."); 35 | } 36 | 37 | 38 | void loop() 39 | { 40 | } 41 | 42 | 43 | // -- END OF FILE -- 44 | 45 | -------------------------------------------------------------------------------- /examples/performance/performance.ino: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: performance.ino 3 | // AUTHOR: Rob Tillaart 4 | // DATE: 2013-09-23 5 | // PURPOSE: test complex math 6 | // URL: https://github.com/RobTillaart/Complex 7 | // 8 | // Serial.print(Complex) supported since 0.1.05 9 | 10 | 11 | #include "Complex.h" 12 | 13 | unsigned long start, stop, total; 14 | double re, im, m, p; 15 | 16 | 17 | void setup() 18 | { 19 | Serial.begin(115200); 20 | Serial.print("\n Complex numbers performance test for Arduino: "); 21 | Serial.println(COMPLEX_LIB_VERSION); 22 | Serial.println(); 23 | 24 | start = micros(); 25 | Complex c1(10.0, -2.0); 26 | Complex c2(3, 0); 27 | Complex c3(-10, 4); 28 | Complex c4(-5,-5); 29 | Complex c5(0, 0); 30 | stop = micros(); 31 | Serial.print("5 constructors\t"); 32 | Serial.println(stop - start); 33 | total += (stop - start); 34 | 35 | start = micros(); 36 | for(uint8_t i=0; i< 100; i++) c3.set(0,0); 37 | stop = micros(); 38 | Serial.print("set(0,0)\t"); 39 | Serial.println(stop - start); 40 | total += (stop - start); 41 | 42 | start = micros(); 43 | for(uint8_t i=0; i< 100; i++) c5 = c1 + 1; 44 | stop = micros(); 45 | Serial.print("c1 + 1 \t"); 46 | Serial.println(stop - start); 47 | total += (stop - start); 48 | 49 | start = micros(); 50 | for(uint8_t i=0; i< 100; i++) c5 = c1 + c2; 51 | stop = micros(); 52 | Serial.print("c1 + c2\t\t"); 53 | Serial.println(stop - start); 54 | total += (stop - start); 55 | 56 | start = micros(); 57 | for(uint8_t i=0; i< 100; i++) c5 += c2; 58 | stop = micros(); 59 | Serial.print("+= c2\t\t"); 60 | Serial.println(stop - start); 61 | total += (stop - start); 62 | 63 | start = micros(); 64 | for(uint8_t i=0; i< 100; i++) c5 = -c1; 65 | stop = micros(); 66 | Serial.print("c5 = -c1\t"); 67 | Serial.println(stop - start); 68 | total += (stop - start); 69 | 70 | start = micros(); 71 | for(uint8_t i=0; i< 100; i++) c5 = c1 - 3; 72 | stop = micros(); 73 | Serial.print("c1 - 3\t\t"); 74 | Serial.println(stop - start); 75 | total += (stop - start); 76 | 77 | start = micros(); 78 | for(uint8_t i=0; i< 100; i++) c5 = c1 - c2; 79 | stop = micros(); 80 | Serial.print("c1 - c2\t\t"); 81 | Serial.println(stop - start); 82 | total += (stop - start); 83 | 84 | start = micros(); 85 | for(uint8_t i=0; i< 100; i++) c5 -= c2; 86 | stop = micros(); 87 | Serial.print("c5 -= c2\t"); 88 | Serial.println(stop - start); 89 | total += (stop - start); 90 | 91 | start = micros(); 92 | for(uint8_t i=0; i< 100; i++) c5 = c1 * 3; 93 | stop = micros(); 94 | Serial.print("c1 * 3\t\t"); 95 | Serial.println(stop - start); 96 | total += (stop - start); 97 | 98 | start = micros(); 99 | for(uint8_t i=0; i< 100; i++) c5 = c1 * c2; 100 | stop = micros(); 101 | Serial.print("c1 * c2\t\t"); 102 | Serial.println(stop - start); 103 | total += (stop - start); 104 | 105 | start = micros(); 106 | for(uint8_t i=0; i< 100; i++) c5 *= c2; 107 | stop = micros(); 108 | Serial.print("c5 *= c2\t"); 109 | Serial.println(stop - start); 110 | total += (stop - start); 111 | 112 | start = micros(); 113 | for(uint8_t i=0; i< 100; i++) c5 = c1 / 3; 114 | stop = micros(); 115 | Serial.print("c1 / 3\t\t"); 116 | Serial.println(stop - start); 117 | total += (stop - start); 118 | 119 | start = micros(); 120 | for(uint8_t i=0; i< 100; i++) c5 = c1 / c2; 121 | stop = micros(); 122 | Serial.print("c1 / c2\t\t"); 123 | Serial.println(stop - start); 124 | total += (stop - start); 125 | 126 | start = micros(); 127 | for(uint8_t i=0; i< 100; i++) c5 /= c2; 128 | stop = micros(); 129 | Serial.print("c5 /= c2\t"); 130 | Serial.println(stop - start); 131 | total += (stop - start); 132 | 133 | Serial.println(); 134 | 135 | start = micros(); 136 | for(uint8_t i=0; i< 100; i++) re = c1.real(); 137 | stop = micros(); 138 | Serial.print("real()\t\t"); 139 | Serial.println(stop - start); 140 | total += (stop - start); 141 | 142 | start = micros(); 143 | for(uint8_t i=0; i< 100; i++) im = c1.imag(); 144 | stop = micros(); 145 | Serial.print("imag()\t\t"); 146 | Serial.println(stop - start); 147 | total += (stop - start); 148 | 149 | start = micros(); 150 | for(uint8_t i=0; i< 100; i++) m = c1.modulus(); 151 | stop = micros(); 152 | Serial.print("modulus()\t"); 153 | Serial.println(stop - start); 154 | total += (stop - start); 155 | 156 | start = micros(); 157 | for(uint8_t i=0; i< 100; i++) p = c1.phase(); 158 | stop = micros(); 159 | Serial.print("phase\t\t"); 160 | Serial.println(stop - start); 161 | total += (stop - start); 162 | 163 | start = micros(); 164 | for(uint8_t i=0; i< 100; i++) c5.polar(m, p); 165 | stop = micros(); 166 | Serial.print("polar()\t\t"); 167 | Serial.println(stop - start); 168 | total += (stop - start); 169 | 170 | start = micros(); 171 | for(uint8_t i=0; i< 100; i++) c5 = c1.conjugate(); 172 | stop = micros(); 173 | Serial.print("conjugate()\t"); 174 | Serial.println(stop - start); 175 | total += (stop - start); 176 | 177 | start = micros(); 178 | for(uint8_t i=0; i< 100; i++) c5 = c1.reciprocal(); 179 | stop = micros(); 180 | Serial.print("reciprocal();\t"); 181 | Serial.println(stop - start); 182 | total += (stop - start); 183 | 184 | Serial.println(); 185 | 186 | start = micros(); 187 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_sqr(); 188 | stop = micros(); 189 | Serial.print("c_sqr()\t\t"); 190 | Serial.println(stop - start); 191 | total += (stop - start); 192 | 193 | start = micros(); 194 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_exp(); 195 | stop = micros(); 196 | Serial.print("c_exp()\t\t"); 197 | Serial.println(stop - start); 198 | total += (stop - start); 199 | 200 | start = micros(); 201 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_log(); 202 | stop = micros(); 203 | Serial.print("c_log()\t\t"); 204 | Serial.println(stop - start); 205 | total += (stop - start); 206 | 207 | start = micros(); 208 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_pow(2); 209 | stop = micros(); 210 | Serial.print("c_pow(2)\t"); 211 | Serial.println(stop - start); 212 | total += (stop - start); 213 | 214 | start = micros(); 215 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_sqrt(); 216 | stop = micros(); 217 | Serial.print("c_sqrt()\t"); 218 | Serial.println(stop - start); 219 | total += (stop - start); 220 | 221 | start = micros(); 222 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_logn(c4); 223 | stop = micros(); 224 | Serial.print("c_logn(c4)\t"); 225 | Serial.println(stop - start); 226 | total += (stop - start); 227 | 228 | start = micros(); 229 | for(uint8_t i=0; i< 100; i++) c5 = c4.c_pow(c5); 230 | stop = micros(); 231 | Serial.print("c_pow(c5)\t"); 232 | Serial.println(stop - start); 233 | total += (stop - start); 234 | 235 | start = micros(); 236 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_log10(); 237 | stop = micros(); 238 | Serial.print("c_log10()\t"); 239 | Serial.println(stop - start); 240 | total += (stop - start); 241 | 242 | Serial.println(); 243 | c1.set(0.5, 0.5); 244 | 245 | start = micros(); 246 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_sin(); 247 | stop = micros(); 248 | Serial.print("c_sin()\t\t"); 249 | Serial.println(stop - start); 250 | total += (stop - start); 251 | 252 | start = micros(); 253 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_asin(); 254 | stop = micros(); 255 | Serial.print("c_asin()\t"); 256 | Serial.println(stop - start); 257 | 258 | start = micros(); 259 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_cos(); 260 | stop = micros(); 261 | Serial.print("c_cos()\t\t"); 262 | Serial.println(stop - start); 263 | total += (stop - start); 264 | 265 | start = micros(); 266 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acos(); 267 | stop = micros(); 268 | Serial.print("c_acos()\t"); 269 | Serial.println(stop - start); 270 | total += (stop - start); 271 | 272 | start = micros(); 273 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_tan(); 274 | stop = micros(); 275 | Serial.print("c_tan()\t\t"); 276 | Serial.println(stop - start); 277 | total += (stop - start); 278 | 279 | start = micros(); 280 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_atan(); 281 | stop = micros(); 282 | Serial.print("c_atan()\t"); 283 | Serial.println(stop - start); 284 | total += (stop - start); 285 | 286 | Serial.println(); 287 | c1.set(0.5, 0.5); 288 | 289 | start = micros(); 290 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_csc(); 291 | stop = micros(); 292 | Serial.print("c_csc()\t\t"); 293 | Serial.println(stop - start); 294 | total += (stop - start); 295 | 296 | start = micros(); 297 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acsc(); 298 | stop = micros(); 299 | Serial.print("c_acsc()\t"); 300 | Serial.println(stop - start); 301 | total += (stop - start); 302 | 303 | start = micros(); 304 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_sec(); 305 | stop = micros(); 306 | Serial.print("c_sec()\t\t"); 307 | Serial.println(stop - start); 308 | total += (stop - start); 309 | 310 | start = micros(); 311 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_asec(); 312 | stop = micros(); 313 | Serial.print("c_asec()\t"); 314 | Serial.println(stop - start); 315 | total += (stop - start); 316 | 317 | start = micros(); 318 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_cot(); 319 | stop = micros(); 320 | Serial.print("c_cot()\t\t"); 321 | Serial.println(stop - start); 322 | total += (stop - start); 323 | 324 | start = micros(); 325 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acot(); 326 | stop = micros(); 327 | Serial.print("c_acot()\t"); 328 | Serial.println(stop - start); 329 | total += (stop - start); 330 | 331 | Serial.println(); 332 | c1.set(0.5, 0.5); 333 | 334 | start = micros(); 335 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_sinh(); 336 | stop = micros(); 337 | Serial.print("c_sinh()\t"); 338 | Serial.println(stop - start); 339 | total += (stop - start); 340 | 341 | start = micros(); 342 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_asinh(); 343 | stop = micros(); 344 | Serial.print("c_asinh()\t"); 345 | Serial.println(stop - start); 346 | total += (stop - start); 347 | 348 | start = micros(); 349 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_cosh(); 350 | stop = micros(); 351 | Serial.print("c_cosh()\t"); 352 | Serial.println(stop - start); 353 | total += (stop - start); 354 | 355 | start = micros(); 356 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acosh(); 357 | stop = micros(); 358 | Serial.print("c_acosh()\t"); 359 | Serial.println(stop - start); 360 | total += (stop - start); 361 | 362 | start = micros(); 363 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_tanh(); 364 | stop = micros(); 365 | Serial.print("c_tanh()\t"); 366 | Serial.println(stop - start); 367 | total += (stop - start); 368 | 369 | start = micros(); 370 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_atanh(); 371 | stop = micros(); 372 | Serial.print("c_atanh()\t"); 373 | Serial.println(stop - start); 374 | total += (stop - start); 375 | 376 | Serial.println(); 377 | c1.set(0.5, 0.5); 378 | 379 | start = micros(); 380 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_csch(); 381 | stop = micros(); 382 | Serial.print("c_csch()\t"); 383 | Serial.println(stop - start); 384 | total += (stop - start); 385 | 386 | start = micros(); 387 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acsch(); 388 | stop = micros(); 389 | Serial.print("c_acsch()\t"); 390 | Serial.println(stop - start); 391 | total += (stop - start); 392 | 393 | start = micros(); 394 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_sech(); 395 | stop = micros(); 396 | Serial.print("c_sech()\t"); 397 | Serial.println(stop - start); 398 | total += (stop - start); 399 | 400 | start = micros(); 401 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_asech(); 402 | stop = micros(); 403 | Serial.print("c_asech()\t"); 404 | Serial.println(stop - start); 405 | total += (stop - start); 406 | 407 | start = micros(); 408 | for(uint8_t i=0; i< 100; i++) c5 = c1.c_coth(); 409 | stop = micros(); 410 | Serial.print("c_coth()\t"); 411 | Serial.println(stop - start); 412 | total += (stop - start); 413 | 414 | start = micros(); 415 | for(uint8_t i=0; i< 100; i++) c5 = c5.c_acoth(); 416 | stop = micros(); 417 | Serial.print("c_acoth()\t"); 418 | Serial.println(stop - start); 419 | total += (stop - start); 420 | 421 | Serial.println(); 422 | Serial.println(total); 423 | Serial.println("\n.. Complex done"); 424 | } 425 | 426 | 427 | void loop() 428 | { 429 | } 430 | 431 | 432 | // -- END OF FILE -- 433 | 434 | -------------------------------------------------------------------------------- /examples/performance/preformanceOutput_0.2.0.txt: -------------------------------------------------------------------------------- 1 | Complex numbers performance test for Arduino: 0.2.0 2 | 3 | 5 constructors 12 4 | set(0,0) 4 5 | c1 + 1 2024 6 | c1 + c2 2036 7 | += c2 1616 8 | c5 = -c1 692 9 | c1 - 3 1440 10 | c1 - c2 2112 11 | c5 -= c2 1552 12 | c1 * 3 5728 13 | c1 * c2 5592 14 | c5 *= c2 4408 15 | c1 / 3 12356 16 | c1 / c2 12232 17 | c5 /= c2 6336 18 | 19 | real() 4 20 | imag() 4 21 | modulus() 6000 22 | phase 204 23 | polar() 260 24 | conjugate() 676 25 | reciprocal(); 8160 26 | 27 | c_sqr() 4672 28 | c_exp() 42588 29 | c_log() 39628 30 | c_pow(2) 88384 31 | c_sqrt() 8520 32 | c_logn(c4) 72644 33 | c_pow(c5) 61896 34 | c_log10() 40776 35 | 36 | c_sin() 57968 37 | c_asin() 75552 38 | c_cos() 58076 39 | c_acos() 73968 40 | c_tan() 129156 41 | c_atan() 66864 42 | 43 | c_csc() 70372 44 | c_acsc() 85828 45 | c_sec() 70412 46 | c_asec() 87528 47 | c_cot() 141652 48 | c_acot() 74420 49 | 50 | c_sinh() 57964 51 | c_asinh() 64612 52 | c_cosh() 58060 53 | c_acosh() 62200 54 | c_tanh() 129148 55 | c_atanh() 91388 56 | 57 | c_csch() 70452 58 | c_acsch() 66464 59 | c_sech() 70420 60 | c_asech() 77548 61 | c_coth() 141584 62 | c_acoth() 98544 63 | 64 | 2337184 65 | 66 | .. Complex done 67 | -------------------------------------------------------------------------------- /examples/performance/preformanceOutput_0.3.1.txt: -------------------------------------------------------------------------------- 1 | 2 | Complex numbers performance test for Arduino: 0.3.1 3 | 4 | 5 constructors 16 5 | set(0,0) 4 6 | c1 + 1 2020 7 | c1 + c2 2032 8 | += c2 1616 9 | c5 = -c1 692 10 | c1 - 3 1444 11 | c1 - c2 2112 12 | c5 -= c2 1548 13 | c1 * 3 5724 14 | c1 * c2 5592 15 | c5 *= c2 4408 16 | c1 / 3 12356 17 | c1 / c2 12228 18 | c5 /= c2 6336 19 | 20 | real() 4 21 | imag() 4 22 | modulus() 6000 23 | phase 204 24 | polar() 256 25 | conjugate() 680 26 | reciprocal(); 8156 27 | 28 | c_sqr() 4672 29 | c_exp() 42584 30 | c_log() 39636 31 | c_pow(2) 88380 32 | c_sqrt() 8504 33 | c_logn(c4) 72644 34 | c_pow(c5) 61896 35 | c_log10() 40780 36 | 37 | c_sin() 57972 38 | c_asin() 75556 39 | c_cos() 58060 40 | c_acos() 73968 41 | c_tan() 129156 42 | c_atan() 66864 43 | 44 | c_csc() 70372 45 | c_acsc() 85824 46 | c_sec() 70416 47 | c_asec() 87528 48 | c_cot() 141652 49 | c_acot() 74420 50 | 51 | c_sinh() 57964 52 | c_asinh() 64616 53 | c_cosh() 58060 54 | c_acosh() 62204 55 | c_tanh() 129148 56 | c_atanh() 91384 57 | 58 | c_csch() 70448 59 | c_acsch() 66464 60 | c_sech() 70420 61 | c_asech() 77548 62 | c_coth() 141584 63 | c_acoth() 98540 64 | 65 | 2337140 66 | 67 | .. Complex done 68 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | # Syntax Colouring Map For Complex 2 | 3 | # Data types (KEYWORD1) 4 | Complex KEYWORD1 5 | 6 | 7 | # Methods and Functions (KEYWORD2) 8 | set KEYWORD2 9 | setReal KEYWORD2 10 | setImag KEYWORD2 11 | real KEYWORD2 12 | imag KEYWORD2 13 | polar KEYWORD2 14 | phase KEYWORD2 15 | modulus KEYWORD2 16 | conjugate KEYWORD2 17 | reciprocal KEYWORD2 18 | 19 | c_sqrt KEYWORD2 20 | c_sqr KEYWORD2 21 | c_exp KEYWORD2 22 | c_log KEYWORD2 23 | c_log10 KEYWORD2 24 | c_pow KEYWORD2 25 | c_logn KEYWORD2 26 | 27 | c_sin KEYWORD2 28 | c_cos KEYWORD2 29 | c_tan KEYWORD2 30 | c_asin KEYWORD2 31 | c_acos KEYWORD2 32 | c_atan KEYWORD2 33 | 34 | c_csc KEYWORD2 35 | c_sec KEYWORD2 36 | c_cot KEYWORD2 37 | c_acsc KEYWORD2 38 | c_asec KEYWORD2 39 | c_acot KEYWORD2 40 | 41 | c_sinh KEYWORD2 42 | c_cosh KEYWORD2 43 | c_tanh KEYWORD2 44 | c_asinh KEYWORD2 45 | c_acosh KEYWORD2 46 | c_atanh KEYWORD2 47 | 48 | c_csch KEYWORD2 49 | c_sech KEYWORD2 50 | c_coth KEYWORD2 51 | c_acsch KEYWORD2 52 | c_asech KEYWORD2 53 | c_acoth KEYWORD2 54 | 55 | 56 | # Constants (LITERAL1) 57 | COMPLEX_LIB_VERSION LITERAL1 58 | 59 | 60 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Complex", 3 | "keywords": "Complex,numbers,Imaginary,phase,modulus,polar,conjugate,math,sin,cos,tan,exp,pow", 4 | "description": "Library for Complex math. Implements Printable interface.", 5 | "authors": 6 | [ 7 | { 8 | "name": "Rob Tillaart", 9 | "email": "Rob.Tillaart@gmail.com", 10 | "maintainer": true 11 | } 12 | ], 13 | "repository": 14 | { 15 | "type": "git", 16 | "url": "https://github.com/RobTillaart/Complex.git" 17 | }, 18 | "version": "0.3.4", 19 | "license": "MIT", 20 | "frameworks": "*", 21 | "platforms": "*", 22 | "headers": "Complex.h" 23 | } 24 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Complex 2 | version=0.3.4 3 | author=Rob Tillaart 4 | maintainer=Rob Tillaart 5 | sentence=Arduino library for Complex math. 6 | paragraph=implements Printable interface 7 | category=Data Processing 8 | url=https://github.com/RobTillaart/Complex 9 | architectures=* 10 | includes=Complex.h 11 | depends= 12 | -------------------------------------------------------------------------------- /test/unit_test_001.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // FILE: unit_test_001.cpp 3 | // AUTHOR: Rob Tillaart 4 | // DATE: 2020-12-16 5 | // PURPOSE: unit tests for the Complex datatype Library 6 | // https://github.com/RobTillaart/Complex 7 | // https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md 8 | // 9 | 10 | // supported assertions 11 | // ---------------------------- 12 | // assertEqual(expected, actual) 13 | // assertNotEqual(expected, actual) 14 | // assertLess(expected, actual) 15 | // assertMore(expected, actual) 16 | // assertLessOrEqual(expected, actual) 17 | // assertMoreOrEqual(expected, actual) 18 | // assertTrue(actual) 19 | // assertFalse(actual) 20 | // assertNull(actual) 21 | // assertNotNull(actual) 22 | 23 | 24 | #include 25 | 26 | 27 | #include "Complex.h" 28 | 29 | 30 | unittest_setup() 31 | { 32 | fprintf(stderr, "COMPLEX_LIB_VERSION: %s\n", (char *) COMPLEX_LIB_VERSION); 33 | } 34 | 35 | 36 | unittest_teardown() 37 | { 38 | } 39 | 40 | 41 | unittest(test_constructor) 42 | { 43 | Complex c1(10.0, -2.0); 44 | Complex c2(3, 0); 45 | Complex c3(-10, 4); 46 | Complex c4(-5,-5); 47 | Complex c5; // (0, 0) 48 | 49 | assertEqual(10.0, c1.real()); 50 | assertEqual(-2.0, c1.imag()); 51 | assertEqual( 3.0, c2.real()); 52 | assertEqual( 0.0, c2.imag()); 53 | assertEqual(-10, c3.real()); 54 | assertEqual(4.0, c3.imag()); 55 | assertEqual(-5.0, c4.real()); 56 | assertEqual(-5.0, c4.imag()); 57 | assertEqual(0.0, c5.real()); 58 | assertEqual(0.0, c5.imag()); 59 | 60 | // one is a default available var. 61 | assertEqual(1.0, one.real()); 62 | assertEqual(0.0, one.imag()); 63 | 64 | // polar notation setter. 65 | c1.polar(5, PI/4); 66 | assertEqualFloat(5, c1.modulus(), 0.0001); 67 | assertEqualFloat(PI/4, c1.phase(), 0.0001); 68 | } 69 | 70 | 71 | unittest(test_basic_math) 72 | { 73 | Complex a(10.0, -2.5); 74 | Complex b(3, 1); 75 | 76 | Complex c1 = a + b; 77 | assertEqual(13, c1.real()); 78 | assertEqual(-1.5, c1.imag()); 79 | 80 | Complex c2 = a - b; 81 | assertEqual(7, c2.real()); 82 | assertEqual(-3.5, c2.imag()); 83 | 84 | Complex c3 = a * b; 85 | assertEqual(32.5, c3.real()); 86 | assertEqual(2.5, c3.imag()); 87 | 88 | Complex c4 = a / b; 89 | assertEqual(2.75, c4.real()); 90 | assertEqual(-1.75, c4.imag()); 91 | 92 | Complex c5 = -a; 93 | assertEqual(-10, c5.real()); 94 | assertEqual(2.5, c5.imag()); 95 | 96 | a += b; 97 | assertEqual(13, a.real()); 98 | assertEqual(-1.5, a.imag()); 99 | 100 | a -= b; 101 | assertEqual(10, a.real()); 102 | assertEqual(-2.5, a.imag()); 103 | 104 | a *= b; 105 | assertEqual(32.5, a.real()); 106 | assertEqual(2.5, a.imag()); 107 | 108 | a /= b; 109 | assertEqual(10, a.real()); 110 | assertEqual(-2.5, a.imag()); 111 | } 112 | 113 | 114 | unittest(test_basic_functions) 115 | { 116 | Complex a; 117 | assertEqual(0, a.real()); 118 | assertEqual(0, a.imag()); 119 | 120 | a.set(5, 34); 121 | assertEqual(5, a.real()); 122 | assertEqual(34, a.imag()); 123 | 124 | a.setReal(10); 125 | assertEqual(10, a.real()); 126 | assertEqual(34, a.imag()); 127 | 128 | a.setImag(-2.5); 129 | assertEqual(10, a.real()); 130 | assertEqual(-2.5, a.imag()); 131 | 132 | float ph = a.phase(); 133 | assertEqualFloat(-0.244979, ph, 0.0001); 134 | 135 | float mod = a.modulus(); 136 | assertEqualFloat(10.3078, mod, 0.0001); 137 | 138 | Complex conj = a.conjugate(); 139 | assertEqual(10, conj.real()); 140 | assertEqual(2.5, conj.imag()); 141 | 142 | Complex reci = a.reciprocal(); 143 | assertEqualFloat(0.0941176, reci.real(), 0.0001); 144 | assertEqualFloat(0.0235294, reci.imag(), 0.0001); 145 | 146 | reci *= a; 147 | assertEqualFloat(1.0, reci.real(), 0.0001); 148 | assertEqualFloat(0, reci.imag(), 0.0001); 149 | } 150 | 151 | 152 | unittest(test_power_functions) 153 | { 154 | Complex a(3, 4); 155 | Complex b; 156 | 157 | b = a.c_sqrt(); 158 | assertEqual(2, b.real()); 159 | assertEqual(1, b.imag()); 160 | 161 | b = a.c_sqr(); 162 | assertEqual(-7, b.real()); 163 | assertEqual(24, b.imag()); 164 | 165 | b = a.c_exp(); 166 | assertEqualFloat(-13.1288, b.real(), 0.0001); 167 | assertEqualFloat(-15.2008, b.imag(), 0.0001); 168 | 169 | b = a.c_log(); 170 | assertEqualFloat(1.60944, b.real(), 0.0001); 171 | assertEqualFloat(0.927295, b.imag(), 0.0001); 172 | 173 | b = a.c_log10(); 174 | assertEqualFloat(0.69897, b.real(), 0.0001); 175 | assertEqualFloat(0.402719, b.imag(), 0.0001); 176 | 177 | b = a.c_pow(a); 178 | assertEqualFloat(-2.99799, b.real(), 0.0001); 179 | assertEqualFloat(0.623786, b.imag(), 0.0001); 180 | 181 | b = a.c_logn(a); 182 | assertEqual(1, b.real()); 183 | assertEqual(0, b.imag()); 184 | } 185 | 186 | 187 | unittest(test_gonio_functions_I) 188 | { 189 | Complex a(3, 4); 190 | Complex b; 191 | 192 | b = a.c_sin(); 193 | assertEqualFloat(3.85374, b.real(), 0.0001); 194 | assertEqualFloat(-27.0168, b.imag(), 0.0001); 195 | 196 | b = a.c_cos(); 197 | assertEqualFloat(-27.0349, b.real(), 0.0001); 198 | assertEqualFloat(-3.85115, b.imag(), 0.0001); 199 | 200 | b = a.c_tan(); 201 | assertEqualFloat(-0.000187318, b.real(), 0.0001); 202 | assertEqualFloat(0.999356, b.imag(), 0.0001); 203 | 204 | b = a.c_asin(); 205 | assertEqualFloat(0.633984, b.real(), 0.0001); 206 | assertEqualFloat(2.30551, b.imag(), 0.0001); 207 | 208 | b = a.c_acos(); 209 | assertEqualFloat(0.936813, b.real(), 0.0001); 210 | assertEqualFloat(-2.30551, b.imag(), 0.0001); 211 | 212 | b = a.c_atan(); 213 | assertEqualFloat(1.44831, b.real(), 0.0001); 214 | assertEqualFloat(0.158997, b.imag(), 0.0001); 215 | } 216 | 217 | 218 | unittest_main() 219 | 220 | 221 | // -- END OF FILE -- 222 | 223 | --------------------------------------------------------------------------------