Use algebra
Ultimaille provides a few classes and functions for basic linear algebra.
Vectors
In ultimaille you can define vectors of arbitrary length, but the most commonly used are 2D, 3D and 4D vectors. For this reason, ultimaille has predefined the following vector types:
Name | Number of dimensions |
---|---|
vec2 |
2 |
vec3 |
3 |
vec4 |
4 |
vec<n> |
n |
You can perform basic operations on vectors:
Name | Operation | Computation |
---|---|---|
Vector / scalar multiplication | $$\vec{v} * a $$ | $$ (v_1 * a, ..., v_n * a) $$ |
Vector / scalar division | $$\vec{v} / a $$ | $$ (v_1 / a, ..., v_n / a) $$ |
Vector addition | $$\vec{v} + \vec{u} $$ | $$ (v_1 + u_1, ..., v_n + u_n) $$ |
Vector subtraction | $$\vec{v} - \vec{u} $$ | $$ (v_1 - u_1, ..., v_n - u_n) $$ |
Dot product | $$\vec{v} \cdot \vec{u} $$ | $$ \sum_{i=1}^{n} v_i * u_i $$ |
Vector negation | $$-\vec{v} $$ | $$ (-v_1, ..., -v_n) $$ |
Vector indexation | $$\vec{v}[i] $$ | $$ v_i $$ |
Only on vec2
and vec3
:
Name | Operation | Computation |
---|---|---|
Square norm | $$ \lVert v \rVert^2 $$ | $$ (v_x * v_x + v_y * v_y + ...) $$ |
norm (length) | $$ \lVert v \rVert $$ | $$ \sqrt{(v_x * v_x + v_y * v_y + ...)} $$ |
normalize | $$ \frac{\vec{v}}{\lVert v \rVert} $$ | $$ (\frac{v_x}{\lVert v \rVert}, \frac{v_y}{\lVert v \rVert}, ...) $$ |
2D Vector example
// Create 2D vectors
std::cout << "--- 2D Vectors ---" << std::endl;
vec2 v2a{1., 1.};
vec2 v2b{3., 4.};
// Addition of vectors
std::cout << v2a << "+ " << v2b << "= " << v2a + v2b << std::endl;
// Subtraction of vectors
std::cout << v2a << "- " << v2b << "= " << v2a - v2b << std::endl;
double a = 2.;
// Vector scalar multiplication
std::cout << v2a << "* " << a << " = " << v2a * a << std::endl;
// Vector scalar division
std::cout << v2a << "/ " << a << " = " << v2a / a << std::endl;
// Negate
std::cout << "-( " << v2a << ")" << " = " << -v2a << std::endl;
// Dot product of vectors
std::cout << "dot product: " << v2a << ". " << v2b << "= " << v2a * v2b << std::endl;
// Compute the square of the norm (square of length) of vector
std::cout << "square norm: " << v2a.norm2() << std::endl;
// Compute the norm (length) of vector
std::cout << "norm: " << v2a.norm() << std::endl;
// Compute the normalization of vector and return it
std::cout << "normalized: " << v2a.normalized() << std::endl;
// Normalize vector "in-place"
v2a.normalize();
std::cout << "normalized: " << v2a << std::endl;
3D Vector example
// Create 3D vectors
std::cout << "--- 3D Vectors ---" << std::endl;
vec3 v3a{1., 2., 3.};
vec3 v3b{3., 4., .5};
// Addition of vectors
std::cout << v3a << "+ " << v3b << "= " << v3a + v3b << std::endl;
// Subtraction of vectors
std::cout << v3a << "- " << v3b << "= " << v3a - v3b << std::endl;
// Vector scalar multiplication
std::cout << v3a << "* " << a << " = " << v3a * a << std::endl;
// Vector scalar division
std::cout << v3a << "/ " << a << " = " << v3a / a << std::endl;
// Negate
std::cout << "-( " << v3a << ")" << " = " << -v3a << std::endl;
// Dot product of vectors
std::cout << "dot product: " << v3a << ". " << v3b << "= " << v3a * v3b << std::endl;
// Cross product of vectors
std::cout << "cross product: " << v3a << ". " << v3b << "= " << cross(v3a, v3b) << std::endl;
// Compute the square of the norm (square of length) of vector
std::cout << "square norm: " << v3a.norm2() << std::endl;
// Compute the norm (length) of vector
std::cout << "norm: " << v3a.norm() << std::endl;
// Compute the normalization of vector and return it
std::cout << "normalized: " << v3a.normalized() << std::endl;
// Normalize vector "in-place"
v3a.normalize();
std::cout << "normalized: " << v3a << std::endl;
Matrices
In ultimaille you can define matrices of arbitrary size, but the most commonly used are 2x2, 3x3 and 4x4 matrices. For this reason, ultimaille has predefined the following matrix types:
Name | Number of dimensions |
---|---|
mat2x2 |
2x2 |
mat3x3 |
3x3 |
mat4x4 |
4x4 |
mat<n, m> |
n x m |