Join matrices and vectors

Issue #186 duplicate
Santiago Peñate Vera created an issue

Hi,

I cannot find how to join several matrices or vectors into a new one.

My case is that I am programming a Newton-Raphson method and build the Jacobian, I need to create four sub-matrices (A, B, C, D) and then join them.

J = {{A, B}, {C, D}}

I also need to do this with vectors. Is this implemented?

Comments (2)

  1. Klaus Iglberger

    Hi Santiago!

    Thanks for creating this issue. Blaze does not provide the syntax you suggest to join matrices or vectors. One of the reasons is that the elements in an std::initializer_list are copy-initialized, which in this case would result in a significant and unacceptable overhead. Instead you should use Subvectors and Submatrices to assign to a specific part of a vector or matrix, respectively. For instance, the following join() function uses submatrices to assign A, B, C, and D to the four parts of the given matrix T:

    template< typename MT1, bool SO1
            , typename MT2, bool SO2
            , typename MT3, bool SO3
            , typename MT4, bool SO4
            , typename MT5, bool SO5 > 
    void join( const DenseMatrix<MT1,SO1>& A
             , const DenseMatrix<MT2,SO2>& B
             , const DenseMatrix<MT3,SO3>& C
             , const DenseMatrix<MT4,SO4>& D
             , DenseMatrix<MT5,SO5>& T )
    {
       BLAZE_CONSTRAINT_MUST_BE_OPERATION_TYPE( MT5 );
    
       if( (~A).rows() != (~B).rows() ||
           (~C).rows() != (~D).rows() ||
           (~A).columns() != (~C).columns() ||
           (~B).columns() != (~D).columns() )
          throw std::invalid_argument( "Invalid matrix parameters for join operation" );
    
       const size_t M1( (~A).rows() );
       const size_t M2( (~C).rows() );
       const size_t N1( (~A).columns() );
       const size_t N2( (~B).columns() );
    
       resize( ~T, M1+M2, N1+N2 );
    
       submatrix( ~T, 0UL, 0UL, M1, N1 ) = A;
       submatrix( ~T, 0UL,  N1, M1, N2 ) = B;
       submatrix( ~T,  M1, 0UL, M2, N1 ) = C;
       submatrix( ~T,  M1,  N1, M2, N2 ) = D;
    }
    

    An alternative approach will be the block matrix view, which has been requested in issue #149. Please feel free to vote for this issue and to watch it if you are interested in the progress. Thanks again for creating this issue,

    Best regards,

    Klaus!

  2. Log in to comment