Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
Basic-matrix-operations-no-iostream.ino
Go to the documentation of this file.
1 
70 #include <Linear_Algebra.h> // Include the library first
71 
72 #include <Linear_Algebra.h> // Include the library first
73 
74 // Then include the necessary headers:
77 
78 void setup() {
79  Serial.begin(115200);
80  while (!Serial)
81  ;
82 
83  Serial.println("Solve system Ax = v");
84  Serial.println("-------------------\n");
85  SquareMatrix A = {
86  {1, 6, 2},
87  {3, 2, 1},
88  {5, 1, 3},
89  };
90  Vector x = Vector::ones(3);
91  Vector v = A * x;
92 
93  HouseholderQR qr(A); // QR factorization
94  Vector x_solution = qr.solve(v);
95 
96  Serial.println("A = ");
97  Serial.println(A);
98  Serial.println("v =");
99  Serial.println(v);
100  Serial.println("QR factorization of A:");
101  Serial.println("Q =");
102  Serial.println(qr.get_Q());
103  Serial.println("R =");
104  Serial.println(qr.get_R());
105  Serial.println("solution x =");
106  Serial.println(x_solution);
107 
108  Serial.println("Basic matrix operations");
109  Serial.println("-----------------------\n");
110  Matrix B = {
111  {1, 2},
112  {3, 4},
113  {5, 6},
114  };
115  Matrix C = {
116  {1, 2, 3},
117  {4, 5, 6},
118  };
119  Matrix D = {
120  {10, 11},
121  {12, 13},
122  };
123  Matrix E = C * B + D;
124  Serial.println("C×B + D =");
125  Serial.println(E);
126 
127  Serial.println("Basic vector operations");
128  Serial.println("-----------------------\n");
129  RowVector a = {1, 2, 3};
130  RowVector b = {4, 6, 5};
131  RowVector c = a.cross(b);
132  double d = a.dot(b);
133  Serial.print("a = ");
134  Serial.print(a);
135  Serial.print("b = ");
136  Serial.print(b);
137  Serial.print("a×b = ");
138  Serial.print(c);
139  Serial.print("a·b = ");
140  Serial.println(d);
141  Serial.println();
142 
143  Serial.println("Element access");
144  Serial.println("--------------\n");
145  Serial.print("A[2,0] = ");
146  Serial.println(A(2, 0));
147  // │ └─ column
148  // └──── row
149  Serial.println("A[2,0] ← 100");
150  A(2, 0) = 100; // assign a new value to the element
151  Serial.println("A =");
152  Serial.println(A);
153 }
154 
155 void loop() {}
QR factorization using Householder reflectors.
Matrix solve(const Matrix &B) const
Solve the system AX = B or QRX = B.
Matrix get_R() const &
Get a copy of the upper-triangular matrix R.
SquareMatrix get_Q() const
Compute the unitary matrix Q.
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