Boost LA

matrix_traits

#include <boost/la/matrix_traits.hpp>

namespace
boost
    {
    namespace
    la
        {
        template <class M>
        struct
        matrix_traits
            {
            /*main template members unspecified*/
            };
        
        /*
        User-defined (partial) specializations:
        
        template <>
        struct
        matrix_traits< Matrix >
            {
            static int const rows = /*user-defined*/;        
            static int const cols = /*user-defined*/;        
            typedef /*user-defined*/ scalar_type;        
        
            template <int R,int C> static inline scalar_type r( Matrix const & m );        
            template <int R,int C> static inline scalar_type & w( Matrix & m );        
        
            static inline scalar_type ir( int r, int c, Matrix const & m );        
            static inline scalar_type & iw( int r, int c, Matrix & m );        
            };
        */
        }
    }

The matrix_traits template must be specialized for (user-defined) matrix types in order to enable vector and matrix operations defined in Boost LA headers for objects of those types.

Note: matrix types are not required to be copyable.

The main matrix_traits template members are not specified. Valid specializations are required to define the following members:

  • rows: the expression matrix_traits<Matrix>::rows must evaluate to a compile-time integer constant greater than 0 that specifies the number of rows in a matrix.
  • cols must evaluate to a compile-time integer constant greater than 0 that specifies the number of columns in a matrix.
  • scalar_type: the expression matrix_traits<Matrix>::scalar_type must be a value type which satisfies the scalar requirements.

In addition, valid specializations of the matrix_traits template must define the following access functions as static members, where m is an object of type Matrix, R and C are compile-time integer constants, and r and c are variables of type int:

  • r: the expression matrix_traits<Matrix>::r<R,C>(m) returns either a copy or a const reference to the element at row R and column C of m.
  • w: the expression matrix_traits<Matrix>::w<R,C>(m) returns mutable reference to the element at row R and column C of m.
  • ir: the expression matrix_traits<Matrix>::ir(r,c,m) returns either a copy or a const reference to the element at row r and column c of m.
  • iw: the expression matrix_traits<Matrix>::iw(r,c,m) returns mutable reference to the element at row r and column c of m.

It is illegal to call any of the above functions unless is_matrix<Matrix>::value is true. Even then, matrix types are allowed to define only a subset of the access functions. The general requirements are:

  • if ir is defined, r must also be defined;
  • if iw is defined, w must also be defined.

In addition, every Boost LA function that takes matrix type parameter(s) specifies whether it requires read-only, write-only or read-and-write access.

Below is an example of a user-defined 3x3 matrix type, and its corresponding specialization of the matrix_traits template:

#include <boost/la/matrix_traits.hpp>

struct float33 { float a[3][3]; };

namespace
boost
    {
    namespace
    la
        {
        template <>
        struct
        matrix_traits<float33>
            {
            static int const rows=3;
            static int const cols=3;
            typedef float scalar_type;

            template <int R,int C> static inline scalar_type & w( float33 & m ) { return m.a[R][C]; }
            template <int R,int C> static inline scalar_type r( float33 const & m ) { return v.a[R][C]; }

            static inline scalar_type & iw( int r, int c, float33 & m ) { return m.a[r][c]; }
            static inline scalar_type ir( int r, int c, float33 const & m ) { return m.a[r][c]; }
            };
        }
    }