简体   繁体   中英

How to compute right kernel of a matrix with Eigen library?

I've started to implementation of an algorithm with Eigen library. I needed to calculate null space(kernel) of a matrix. I have tried with a cube's matrix that,

0, 0, 1,   
0, 1, 0,   
1, 0, 0,   
-1, 0, 0,    
0, 0, -1,   
0, -1, 0

Then, I call, its source

A.transposeInPlace();
std::cout << "and after being transposed:\n" << A << std::endl;
FullPivLU<MatrixXf> lu(A);
MatrixXf A_null_space = lu.kernel();
std::cout << "Null space:\n" << A_null_space << std::endl;
A_null_space.transposeInPlace();
std::cout << "Null space Transposed_A:\n" << A_null_space;

I obtain,

 0.5    0   -1    1    0    0    0    0    0  0.5
-0.5    0   -0    0    1    0    0    0    0 -0.5
 0.5    0   -0    0    0    1    0    0    0 -0.5
 0.5    0   -0    0    0    0    1    0    0  0.5
  -1    0    1    0    0    0    0    1    0   -1
-0.5    0    1    0    0    0    0    0    1 -0.5
-0.5    1   -0    0    0    0    0    0    0  0.5

But, I realized later on that its right kernel and left kernel is same and seemingly the code snippet calculates left kernel. The code is getting crazy output on the other test case. So, how can be the right kernel be calculated? The link is also to show the difference btw right and left kernels with examples. However, if I remove first line, the output is 0 0 0

Clearly problem of the case is,

MatrixXf A{10, 3};
A <<
        1, 0, 1 ,
        1, 0, 0 ,
        0, 1, 1 ,
        0, 1, 0 ,
        0, 0, 1 ,
        -1, 0, 0 ,
        0, 0, -1 ,
        0, -1, 1 ,
        0, -1, 0 ,
        -1, 0, 1;

Its output is expected as,

 1  0  0  0  0  0  0 -2  2  1
 0  1  0  0  0  0  0 -1  1  1
 0  0  1  0  0  0  0 -1  2  0
 0  0  0  1  0  0  0  0  1  0
 0  0  0  0  1  0  0 -1  1  0
 0  0  0  0  0  1  0  1 -1 -1
 0  0  0  0  0  0  1  1 -1  0

QR factorization,

HouseholderQR<MatrixXf> qr(A);
    cout << "\nQR matrix to compare \n" << qr.matrixQR().transpose();

Then I get,

   -1.41421           0    0.414214
  -0.707107   -0.707107          -1
  -0.707107    0.707107           1
          0           0           1
  -0.707107    0.707107           0
   0.707107    0.707107           0
   0.707107   -0.707107           0
  -0.707107    0.707107          -1
          0           0          -1
1.19209e-07     1.41421 5.96046e-08

@Edit 2, Does Eigen calculate wrongly?

在此处输入图片说明

Source

@Edit 3,

I'm really but really confused because both matrix seem right! How come?

在此处输入图片说明

As you observed, both matrices are valid right kernels. This is because they correspond to two different basis of the same subspace. To check it, you can reduce the two matrices to the reduced row echelon form (rref function in matlab, or see this online calculator ). This transformation is unique and does not change the span defined by the matrix. Your reference kernel basis is already in this form. So all you have to do is to reduce the one returned by Eigen and see that it gives you the same matrix as your reference one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM