Copy the top MatrixXd matrix to reduce the MatrixXd (Eigen3) C ++ library

I have a lower triangular MatrixXd, and I want to copy its lower values ​​to the upper side, as it will become a symmetric matrix. How can i do this?

So far I have been doing:

 MatrixXd m(n,n); 
 .....
 //do something with m
 for(j=0; j < n; j++)
       {
         for(i=0; i<j; i++)
           {
             m(i,j) = m(j,i);

           }
       }

Is there a quickest way to do this? I was thinking of some kind of internal method that is able to "copy" the lower triangular matrix into the upper one. Say I have this matrix, we call m:

1 2 3
4 5 6
7 8 9

what i need to get in m:

1 4 7
4 5 8
7 8 9

I also know that you can force the top or bottom of the matrix to do something:

MatrixXd m1(n,n);
 m1 = m.triangularView<Eigen::Upper>();
cout << m1 <<endl;

1 2 3
0 5 6
0 0 9

But I still can’t get what I want ...

+5
source share
7 answers

, Eigen3 ++. . , . Eigen , . Eigen , , ,

using namespace Eigen;
MatrixXd m(m,n);
...
(generate uppper triangular entries in m)
...
VectorXd r(n), p(n);
r = m.selfadjointView<Upper>() * p;

, :

#include <Eigen/Core>

using namespace std;
using namespace Eigen;

int main()
{
    Matrix2d m,c;
    m << 1, 2,
         0, 1;

    Vector2d x(0,2), r;

    // perform copy operation 
    c = m.selfadjointView<Upper>(); 
    cout << c << endl;

    // directly apply selfadjoint view in matrix operation
    // (no entries are copied)
    r = m.selfadjointView<Upper>() * x;
} 

  [1, 2,   2, 1]. r , c * x. , , .

+4

, selfadjointView , :

m.triangularView<Lower>() = m.transpose();

+3

, , - m, :

    m.triangularView<Upper>() = m.transpose();

, :

    MatrixXd m(3,3);
    m << 1, 2, 3, 4, 5, 6, 7, 8, 9;

    m.triangularView<Upper>() = m.transpose();
    std::cout << m << std::endl;

, :

1 4 7
4 5 8
7 8 9

.

+2

, . , . tiling.

+1

, , / , (x, y) (y, x). () , .

+1

:

m = m.selfadjointView<Upper>();
+1
source

This works, you can cut something, but you need at least n * m / 2 (less than something), so only 2x

edit: I see that you are using this matrixd object ... the syntax is different, but the algorithm is, anyway

#include <stdio.h>


int main ( )
{
    int mat [ 4 ] [ 4 ];
    int i, j;

    mat [ 0 ] [ 0 ] = 0;
    mat [ 0 ] [ 1 ] = 1;
    mat [ 0 ] [ 2 ] = 2;
    mat [ 0 ] [ 3 ] = 3;
    mat [ 1 ] [ 0 ] = 4;
    mat [ 1 ] [ 1 ] = 5;
    mat [ 1 ] [ 2 ] = 6;
    mat [ 1 ] [ 3 ] = 7;
    mat [ 2 ] [ 0 ] = 8;
    mat [ 2 ] [ 1 ] = 9;
    mat [ 2 ] [ 2 ] = 10;
    mat [ 2 ] [ 3 ] = 11;
    mat [ 3 ] [ 0 ] = 12;
    mat [ 3 ] [ 1 ] = 13;
    mat [ 3 ] [ 2 ] = 14;
    mat [ 3 ] [ 3 ] = 15;

    for ( i = 0; i < 4; i++ )
    {
        for ( j = 0; j < 4; j++ )
            printf ( "%02d", mat [ i ] [ j ] );
        printf ( "\n" );
    }
    printf ( "\n" );

    for ( i = 1; i < 4; i++ )
    {
        for ( j = 0; j < i; j++ )
            mat [ j ] [ i ] = mat [ i ] [ j ];
    }

    for ( i = 0; i < 4; i++ )
    {
        for ( j = 0; j < 4; j++ )
            printf ( "%02d ", mat [ i ] [ j ] );
        printf ( "\n" );
    }
    printf ( "\n" );

    scanf ( "%d", &i );
}
0
source

All Articles