Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
ArduinoMatrixPrinter.cpp
Go to the documentation of this file.
1 
8 
9 #ifdef AVR // AVR has no printf float support, so use dtostre.
10 
11 #include <stdlib.h>
12 
13 namespace detail {
14 char *pad_spaces(char *buf, uint8_t count) {
15  while (count-- > 0)
16  *buf++ = ' ';
17  return buf;
18 }
19 void format_double(double d, char *buffer, uint8_t width, uint8_t precision) {
20  int pad = width - 6 - precision;
21  pad = pad > 0 ? pad : 0;
22  buffer = pad_spaces(buffer, pad);
23  dtostre(d, buffer, precision - 1, DTOSTR_ALWAYS_SIGN);
24 }
25 } // namespace detail
26 
27 #else // If printf float support available, use it.
28 #include <stdio.h>
29 #include <stdint.h>
30 
31 namespace detail {
32 void format_double(double d, char *buf, uint8_t width, uint8_t precision) {
33  int len = snprintf(buf, width + 1, "%*.*e", (int)width, (int)precision, d);
34  // if the text didn't fit the field, fill buffer with *, Fortran style:
35  if (len > width)
36  while (width-- > 0)
37  *buf++ = '*';
38 }
39 } // namespace detail
40 
41 #endif
42 
43 #ifndef NO_ARDUINO_PRINT_SUPPORT
44 
46 #include <new>
47 
48 void Matrix::print(Print &print, uint8_t precision, uint8_t width) const {
49  precision = precision > 0 ? precision : 6;
50  width = width > 0 ? width : precision + 9;
51  char *buffer = new char[width + 1];
52  for (size_t r = 0; r < rows(); ++r) {
53  for (size_t c = 0; c < cols(); ++c) {
54  detail::format_double((*this)(r, c), buffer, width, precision);
55  print.print(buffer);
56  }
57  print.println();
58  }
59  delete[] buffer;
60 }
61 
62 Print &operator<<(Print &print, const Matrix &M) {
63  M.print(print);
64  return print;
65 }
66 
67 #endif // NO_ARDUINO_PRINT_SUPPORT
Preprocessor logic for configuring the library to make it compatible with the Arduino environment.
std::ostream & operator<<(std::ostream &os, const HouseholderQR &qr)
Print the Q and R matrices of a HouseholderQR object.
General matrix class.
Definition: Matrix.hpp:34
size_t rows() const
Get the number of rows of the matrix.
Definition: Matrix.hpp:81
size_t cols() const
Get the number of columns of the matrix.
Definition: Matrix.hpp:83
void print(std::ostream &os, uint8_t precision=0, uint8_t width=0) const
Print a matrix.
Definition: Matrix.cpp:242
void format_double(double d, char *buf, uint8_t width, uint8_t precision)