Skip to content

Expression templates #1

@gf712

Description

@gf712

Like in Eigen, we should have an optimisation with expression templates. Right now the library creates temporary matrices/vectors for each operation. The paper says that we are allowed to return what we think is best from the four overloaded operators (+,- (negation and subtraction) and *) defined in arithmetic_operators.hpp. My idea is to return an expression template, eigen style, for example addition:

template<class OT, class ET1, class OT1, class ET2, class OT2>
struct matrix_addition_traits<OT, matrix<ET1, OT1>, matrix<ET2, OT2>>
{
    // not sure yet how these lines would have to change
    using engine_type = matrix_addition_engine_t<OT, ET1, ET2>;
    using op_traits   = OT;
    using result_type = matrix<engine_type, op_traits>;

    matrix_addition_traits(matrix<ET1, OT1> const& m1, matrix<ET2, OT2> const& m2): lhs(m1), rhs(m2) {}
    
    // do the actual calculation
    auto     operator ()(typename result_type::index_type i, typename result_type::index_type j)
    {
         return lhs(i, j) + rhs(i, j);
    }

    // hold reference to lhs and rhs matrices
    const matrix<ET1, OT1>& lhs;
    const matrix<ET2, OT2>& rhs; 
    
    // syntatic sugar that calls the constructor (rather than calculate the value explicitly with allocation)
    static auto  add(matrix<ET1, OT1> const& m1, matrix<ET2, OT2> const& m2);
};

would be good to discuss a bit how we could achieve this and remain compliant with P1385. It seems like they used Eigen quite a bit as a design inspiration, so this should be doable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions