项目作者: meh

项目描述 :
Embedded Linear Algebra
高级语言: C++
项目地址: git://github.com/meh/ela.git
创建时间: 2017-11-26T00:57:05Z
项目社区:https://github.com/meh/ela

开源协议:MIT License

下载


Embedded Linear Algebra License Build Status

Minimal header only linear algebra library with expression templates and low
footprint designed to run on embedded devices.

Matrix

A matrix is generic over the scalar type, the number of rows and the number
of columns, dynamically sized matrices are not supported.

The internal buffer is always allocated on the stack as a contiguous
row-major array and by default is set to 0.

Vector

There are three classes of vectors, two concrete and one as a view.

Column Vector

A column_vector<Type, Size> is just a type alias for matrix<Type, Size, 1>
but in addition to expression access it implements direct access through the
[] operator.

Row Vector

A row_vector<Type, Size> is just a type alias for matrix<Type, 1, Size> but
in addition to expression access it implements direct access through the []
operator.

Vector

A vector is a column or row vector view into an expression (or matrix if
mutable access is required).

Example

  1. ela::matrix<float, 3, 3> a{{1, 0, 3}, {4, 0, 6}, {7, 0, 9}};
  2. // Assign a column of a matrix.
  3. a.column(1) = {2, 5, 8};
  4. // Scale a row of the matrix and save it as a column vector.
  5. ela::column_vector<float, 3> b = ~(a.row(1) * 2);

Bound checking

Bound checks are used at any runtime indexing operation through a call to
ELA_ASSUME.

Unless ELA_ASSUME is defined before ela/ela.hpp is included its definition
will become an assert call when NDEBUG is not defined and a compiler
unreachable hint when NDEBUG is defined.

In your custom expression implementations you’re advised to call ELA_ASSUME
yourself to make sure the compiler knows what’s going on.

Expression

All matrix operations are implemented as expression templates so no
intermediary objects are created unless explictly assigned to a matrix.

If all your matrix values are known at compile time compilers are able to
completely constant unfold the results most of the time.

What is an expression?

An expression is any type that implements the ela::expression::traits and
provides the appropriate indexing operator.

Example

We’ll implement a generic RGB type as if it were a column vector.

  1. /* Inheriting from `ela::expression::base` is not required but it automatically
  2. * implements all generic expression operators for free, it doesn't add any
  3. * data.
  4. */
  5. template <typename Type>
  6. struct RGB: public ela::expression::base<RGB<Type>>
  7. {
  8. public:
  9. using ela::expression::base<RGB<Type>>::operator =;
  10. public:
  11. Type r = 0;
  12. Type g = 0;
  13. Type b = 0;
  14. /* Create an empty color.
  15. */
  16. RGB () noexcept
  17. { }
  18. RGB (Type r, Type g, Type b) noexcept
  19. : r(r), g(g), b(b)
  20. { }
  21. template <typename Input, typename T = Type>
  22. RGB (Input const& expr, typename std::enable_if<
  23. 3 == ela::expression::traits<Input>::rows &&
  24. 1 == ela::expression::traits<Input>::columns &&
  25. std::is_same<T, typename ela::expression::traits<Input>::type>::value>::type* = 0) noexcept
  26. {
  27. ela::expression::base<RGB<Type>>::operator=(expr);
  28. }
  29. RGB (std::initializer_list<std::initializer_list<Type>> elements) noexcept
  30. {
  31. ela::expression::base<RGB<Type>>::operator=(elements);
  32. }
  33. RGB (std::initializer_list<Type> elements) noexcept
  34. {
  35. ela::expression::base<RGB<Type>>::operator=(elements);
  36. }
  37. /* This is the expression access operator.
  38. */
  39. inline
  40. Type const&
  41. operator () (size_t row, size_t column) const noexcept
  42. {
  43. ELA_ASSUME(row < 3 && column == 0);
  44. if (row == 0) {
  45. return r;
  46. }
  47. else if (row == 1) {
  48. return g;
  49. }
  50. else {
  51. return b;
  52. }
  53. }
  54. /* This is the expression access operator.
  55. */
  56. inline
  57. Type&
  58. operator () (size_t row, size_t column) noexcept
  59. {
  60. ELA_ASSUME(row < 3 && column == 0);
  61. if (row == 0) {
  62. return r;
  63. }
  64. else if (row == 1) {
  65. return g;
  66. }
  67. else {
  68. return b;
  69. }
  70. }
  71. };
  72. namespace ela { namespace expression {
  73. /* This defines the experssion traits for `RGB<T>`.
  74. */
  75. template <typename Type>
  76. struct traits<RGB<Type>>
  77. {
  78. /* This is always the scalar type.
  79. */
  80. typedef Type type;
  81. /* This is the number of rows the expression will produce.
  82. */
  83. static constexpr size_t rows = 3;
  84. /* This is the number of columns the expression will produce.
  85. */
  86. static constexpr size_t columns = 1;
  87. /* This says the expression can return references.
  88. */
  89. static constexpr bool concrete = true;
  90. };
  91. } }

This code will make RGB behave as an expression, you can look into tests/
for more examples of expressions and other stuff.