|
Typed Linear Algebra 0.2.0
Typed Linear Algebra
|
A C++ strongly-typed facade to a matrix linear algebra backend. Brings type safety to matrix operations. Enforces dimensional consistency and unit compatibility. Prevents common errors in scientific and engineering computations.
Example of installation commands in Shell:
Another variation for your CMake infrastructure via fetch content:
For more, see installation instructions.
Strongly typed matrix. Compose a linear algebra backend matrix into a typed matrix. Row and column indexes provide each element's index type.
Also documented in the fcarouge/typed_linear_algebra.hpp header.
| Template Parameter | Definition |
|---|---|
Matrix | The underlying linear algebra matrix. |
RowIndexes | The tuple type of the row indexes. |
ColumnIndexes | The tuple type of the row indexes. |
| Member Type | Definition |
|---|---|
matrix | The type of the composed matrix. |
underlying | The type of the element's underlying storage. |
row_indexes | The tuple with the row components of the indexes. |
column_indexes | The tuple with the column components of the indexes. |
element<i, j> | The type of the element at the given matrix indexes position. |
| Member Variable | Definition |
|---|---|
rows | The count of rows. |
columns | The count of columns. |
| Member Function | Definition |
|---|---|
(default constructor) | Construct a default typed matrix. |
(default copy constructor) | Copy construct the typed matrix. |
(default copy assignment operator) | Copy assign a typed matrix. |
(default move constructor) | Move construct a typed matrix. |
(default move assignment operator) | Move construct a typed matrix. |
(conversion copy constructor) | Copy construct generalization of a compatible typed matrix. |
(conversion copy assignment operator) | Copy assign generalization of a compatible typed matrix. |
(conversion move constructor) | Move construct generalization of a compatible typed matrix. |
(conversion move assignment operator) | Move assign generalization of a compatible typed matrix. |
(conversion copy constructor) | Convert construct a typed matrix from an underlying matrix. |
(conversion copy constructor) | Convert construct a one-dimension uniformly typed matrix from array. |
(conversion copy constructor) | Convert construct a uniformly typed matrix from list-initializers. |
(conversion copy constructor) | Convert construct a row or column typed vector from elements. |
(conversion copy constructor) | Convert construct a singleton typed matrix from a single value. |
operator[i, j] | Read the specified element. |
operator(i, j) | Read the specified element. |
at<i, j>() | Read, write the specified element. |
(conversion operator) | Access the singleton typed matrix element. |
(destructor) | Destruct a default typed matrix. |
The following useful operations are supported. This library attempts to align its nomenclature aligned with that of the primitives provided by std::linalg. This library attempts some compatibility with other C++ standard library primitives, ranges, and iterators.
| Operation | Definition |
|---|---|
- | Substraction where the terms are of identical shapes and substractable types. |
* | Multiplication where the factors are of multipliable shapes and multipliable types. |
/ | Solution, if there exists one, to the inverse multiplication, where the factor are of compatible shapes and types. |
+ | Addition where the terms are of identical shapes and addable types. |
== | Direct, strict equality comparison, with traditional floating-point comparison pitfalls. |
add | Element-wise add two matrices. |
matrix_product | General matrix-matrix product. |
scale | Multiply matrix elements by a scalar. |
transposed | Transpose the input matrix. |
A specialization of the standard formatter is provided for the typed matrix. Use std::format to store a formatted representation of the matrix. Standard format parameters to be supported.
A user-defined literal _i operator in the fcarouge::literals namespace that converts a decimal integer literal into a compile-time index type permitting the use of traditional accessor operator with strong types.
| Concept | Definition |
|---|---|
column_typed_matrix | Concept of a column typed matrix, vector. |
rank_typed_matrix<0> | Concept of a singleton, one-element typed matrix type. |
rank_typed_matrix<1> | Concept of a typed matrix with only one dimension, row, or column. |
rank_typed_matrix<2> | Concept of a regular two-dimension typed matrix. |
row_typed_matrix | Concept of a row typed matrix, vector. |
same_as_typed_matrix | Concept of a typed matrix type. |
same_shape | Concept of typed matrices of the same shape, that is they have the same number of rows and columns. |
uniform_typed_matrix | Concept of a typed matrix in which all element types are the same. |
Typed matrix element conversions customization point. Specialize this template to allow conversion to and from the element's type and underlying type.
The library uses a static instance of element_caster to make a call to the function call operator allowing the instance to be called as if it were a conversion function. This idiom permits the conversion to be externally defined and found through template specialization lookup.
A variety of conversions may be needed, notably value and reference conversions. Performance considerations may influence the value conversion implementation and whether the converted value is provided by a value parameter or by a constant reference parameter.
Type safety cannot be guaranteed at compilation time without index safety. The indexes can either be non-type template parameters or strong types overloadings. Converting a runtime index to a dependent template type is not possible in C++. A proxy reference could be used to allow traditional assignment syntax but the runtime check and extra indirection are not interesting tradeoffs. A template call operator can be used for getting a type safe value but impractical syntax for setting. Without index safety, the accepted tradeoff is a templated index at<i, j>() method.
Lvalue reference assignment cannot be provided due to a contradiction. For example, in user code: m[0_i, 0_i] = 2 * m;. On one hand, if the storage is a tuple of strong types, then the access for linear operations suffers a performance penalty because std::tuple is not a contiguous array. Tuples are stored in implementation-dependent order, alignement, size, and padding. On the other hand, if the storage is a contiguous of built-in types, then materializing a lvalue reference of strong type from the build-in type is undefined behavior. Reinterpret-casting is never constexpr and undefined behavior when aliasing, punning types, even when appropriatly sized and aligned memory. Fortunately, there is a conjecture that lvalue reference assignment is never useful given linear algebra works over vector and matrix entities, and not their elements. A practical set of constructors should suffice. While lvalue reference assignment could be conditionally provided given compatible storage, the end-user experience would be confusing when assignment could not be provided. Assigning an element is supported via the at member function.
Extraneous indexes tradeoffs convenience for safety. For example, m[0, 0, 0, 0, ...] to access the first row/column element of a matrix, rank 2. While the allowance was practical for generic meta-programming in early development, community feedback informed of the user defects permitted by the mis-matching number of indeces, weak guarantees. For safety, the number of indexes must match the rank at compile-time.
Strongly typed memory storage is not a selected design due to its performance tradeoff. A std::tuple or other type-list storages can be used as underlying memory storage. Such storage permits strongly typed lvalue reference assignment which improves the end-user ergonomics. Unfortunately std::tuple does not offer the same guarantees provided by contiguous memory storage and its alignment, padding, and ordering specification. The lack of guarantees prevents the direct, efficient, optimized memory accesses, resulting in a performance penalty. Undefined behavior is avoided. No known solution.
The library is used in projects:
Your project link here!
The library is designed, developed, and tested with the help of third-party tools and services acknowledged and thanked here:
Become a sponsor today! Support this project with coffee and infrastructure!
Your group logo and link here!
Your name and link here!
Thanks everyone!

TypedLinearAlgebra is public domain:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to https://unlicense.org