Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
Basics.cpp
Go to the documentation of this file.
1 
62 #include <iomanip> // std::setw
63 #include <iostream> // std::cout
64 #include <linalg/Matrix.hpp>
65 
67  // Initialize a matrix with an initializer list:
68  Matrix A = {
69  {11, 12, 13},
70  {21, 22, 23},
71  {31, 32, 33},
72  };
73  // Print a matrix:
74  std::cout << "A = \n" << A << std::endl;
75 
76  // Transposing a matrix:
77  Matrix AT = transpose(A);
78  std::cout << "Aᵀ = \n" << AT << std::endl;
79  // Matrix multiplication:
80  std::cout << "AᵀA = \n" << AT * A << std::endl;
81 
82  // Creating special matrices:
83  Matrix E = Matrix::ones(3, 2); // 3×2 (rows×columns) matrix of all ones
84  Matrix O = Matrix::zeros(2, 2); // 2×2 matrix of all zeros
85  Matrix C = Matrix::constant(2, 3, 42.42); // 2×3 all elements are 42.42
86  Matrix I = Matrix::identity(3); // 3×3 identity matrix
87  Matrix R = Matrix::random(3, 3, -10, +10); // 3×3 uniform random in [-10,10]
88 
89  // Adding, subtracting, negation, scalar multiplication, etc.
90  Matrix B = A + I * (-R) - 3 * I + E * O * C / 3.14;
91  std::cout << "B = \n" << B << std::endl;
92 
93  // Element access:
94  std::cout << "A(1, 2) = " << A(1, 2) << std::endl;
95  // (row, column), indices are zero-based.
96 
97  // Matrix size:
98  std::cout << "Dimensions of C: " << C.rows() << "×" << C.cols() << "\n"
99  << "Number of elements of C: " << C.num_elems() << "\n"
100  << std::endl;
101 
102  // Creating a matrix with a given size:
103  Matrix D(3, 4); // Equivalent to `Matrix D = Matrix::zeros(3, 4)`
104 
105  // Iterators:
106  std::iota(std::begin(D), std::end(D), 0.);
107  std::cout << "D = \n" << D << std::endl;
108  // Iterators go column-by-column (column major order).
109  double D_sum = std::accumulate(std::begin(D), std::end(D), 0.);
110  std::cout << "Sum of elements of D = " << D_sum << std::endl;
111 }
112 
114  // Vectors can be initialized just like matrices:
115  Vector v = {1, 2, 3}; // Column vector (3×1)
116  std::cout << "v = \n" << v << std::endl;
117  RowVector a = {4, 6, 5}; // Row vector (1×3)
118 
119  // Transpose from column to row vector:
120  RowVector b = transpose(v);
121 
122  // Dot and cross products:
123  std::cout << "a = " << a //
124  << "b = " << b //
125  << "a×b = " << a.cross(b) //
126  << "a·b = " << std::setw(15) << a.dot(b) << "\n"
127  << std::endl;
128 
129  // Element access:
130  std::cout << "v(2) = " << v(2) << "\n" << std::endl;
131  // indices are zero-based.
132 
133  // Rank-1 multiplication:
134  Matrix V = v * transpose(v);
135  std::cout << "vvᵀ = \n" << V << std::endl;
136  // Dot product:
137  double d = transpose(v) * v;
138  std::cout << "vᵀv = " << d << "\n" << std::endl;
139 }
140 
141 int main() {
144 }
void vector_operations()
Definition: Basics.cpp:113
int main()
Definition: Basics.cpp:141
void matrix_operations()
Definition: Basics.cpp:66
General matrix class.
Definition: Matrix.hpp:34
static Matrix constant(size_t rows, size_t cols, double value)
Create a matrix filled with a constant value.
Definition: Matrix.cpp:157
static Matrix identity(size_t rows, size_t cols)
Create an identity matrix.
Definition: Matrix.cpp:163
static Matrix random(size_t rows, size_t cols, double min=0, double max=1, std::default_random_engine::result_type seed=std::default_random_engine::default_seed)
Create a matrix with uniformly distributed random values.
Definition: Matrix.cpp:172
static Matrix zeros(size_t rows, size_t cols)
Create a matrix filled with zeros.
Definition: Matrix.cpp:152
size_t num_elems() const
Get the number of elements in the matrix:
Definition: Matrix.hpp:85
static Matrix ones(size_t rows, size_t cols)
Create a matrix filled with ones.
Definition: Matrix.cpp:148
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
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
A column vector (n×1 matrix).
Definition: Matrix.hpp:278
Matrix transpose(const Matrix &in)
Matrix transpose for rectangular or square matrices and row or column vectors.
Definition: Matrix.cpp:1064