Numeric
Example of using the standard library numeric functions.
- Boards:
- AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32
This sketch creates an incremental array using the iota function, then computes the inner product of the vector with itself, and uses that to calculate the norm of the vector.
Output
v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
〈v, v〉 = 385
‖v‖ = 19.6214160
Written by PieterP, 2019-11-18
https://github.com/tttapa/Arduino-Helpers
#include <AH/STL/cmath>
#include <AH/STL/numeric>
void setup() {
Serial.begin(115200);
while (!Serial)
;
std::iota(std::begin(vector), std::end(vector), 1);
Serial <<
"v = [" << vector <<
']' <<
endl;
int sum_sq = std::inner_product(std::begin(vector), std::end(vector),
std::begin(vector), 0);
Serial <<
"〈v, v〉 = " << sum_sq <<
endl;
double norm = std::sqrt(sum_sq);
}
void loop() {}
Dummy header file for Arduino builder.
Print & endl(Print &printer)
Setprecision setprecision(int n)
An array wrapper for easy copying, comparing, and iterating.