Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
Basic-matrix-operations.ino
Go to the documentation of this file.
1 
75 #include <Linear_Algebra.h> // Include the library first
76 
77 // Then include the necessary headers:
80 
81 // For printing using arduino::cout:
83 using arduino::cout;
84 
85 void setup() {
86  Serial.begin(115200);
87  while (!Serial)
88  ;
89  cout.precision(6); // How many significant digits to print
90 
91  cout << "Solve system Ax = v \n"
92  "------------------- \n\n";
93  SquareMatrix A = {
94  {1, 6, 2},
95  {3, 2, 1},
96  {5, 1, 3},
97  };
98  Vector x = Vector::ones(3);
99  Vector v = A * x;
100 
101  HouseholderQR qr(A); // QR factorization
102  Vector x_solution = qr.solve(v);
103 
104  cout << "A = \n"
105  << A << "\n"
106  << "v = \n"
107  << v << "\n"
108  << "QR factorization of A: \n"
109  << qr << "\n"
110  << "solution x = \n"
111  << x << std::endl;
112 
113  cout << "Basic matrix operations \n"
114  "----------------------- \n\n";
115  Matrix B = {
116  {1, 2},
117  {3, 4},
118  {5, 6},
119  };
120  Matrix C = {
121  {1, 2, 3},
122  {4, 5, 6},
123  };
124  Matrix D = {
125  {10, 11},
126  {12, 13},
127  };
128  cout << " C×B + D = \n" << (C * B + D) << std::endl;
129 
130  cout << "Basic vector operations \n"
131  "----------------------- \n\n";
132  RowVector a = {1, 2, 3};
133  RowVector b = {4, 6, 5};
134  cout << "a = " << a << "\n"
135  << "b = " << b << "\n"
136  << "a×b = " << a.cross(b) << "\n"
137  << "a·b = " << a.dot(b) << "\n\n"
138  << std::endl;
139 
140  cout << "Element access \n"
141  "-------------- \n\n";
142  cout << "A[2,0] = " << A(2, 0) << std::endl;
143  // │ └─ column
144  // └──── row
145  cout << "A[2,0] ← 100" << std::endl;
146  A(2, 0) = 100; // assign a new value to the element
147  cout << "A = \n" << A << std::endl;
148 }
149 
150 void loop() {}
Arduino std::ostream Serial wrapper.
void setup()
QR factorization using Householder reflectors.
Matrix solve(const Matrix &B) const
Solve the system AX = B or QRX = B.
General matrix class.
Definition: Matrix.hpp:34
A row vector (1×n matrix).
Definition: Matrix.hpp:437
static RowVector cross(const RowVector &a, const RowVector &b)
Compute the cross product of two 3-vectors.
Definition: Matrix.cpp:506
static double dot(const RowVector &a, const RowVector &b)
Compute the dot product of two vectors.
Definition: Matrix.cpp:471
Square matrix class.
Definition: Matrix.hpp:572
A column vector (n×1 matrix).
Definition: Matrix.hpp:278
static Vector ones(size_t size)
Create a vector filled with ones.
Definition: Matrix.cpp:287