特征根中的置换稀疏矩阵 [英] Permuting sparse matrices in Eigen

查看:114
本文介绍了特征根中的置换稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Eigen中置换稀疏矩阵的行和列.这是我的代码,但是没有用.

I want to permute the rows and columns of a sparse matrix in Eigen. Here's the code I have but it's not working.

#include <iostream>
#include <Eigen/Core>
#include <Eigen/SparseCore>
typedef Eigen::SparseMatrix<double> SpMat;
using namespace Eigen;
using namespace std;

int myrandom (int i) { return std::rand()%i;}
int main() {
    PermutationMatrix<Dynamic,Dynamic> perm(5);
    MatrixXd x = MatrixXd::Random(5,5);
    SpMat y = x.sparseView();
    int dim=5;
    perm.setIdentity();
    for (int i=dim-1; i>0; --i) {
        swap (perm.indices()[i],perm.indices()[myrandom(i+1)]);
    }
    cout << "permutation\n" << perm.indices() << endl << endl;
    cout << "original x\n" << y << endl << endl;
    cout << "permuted left x \n" << perm * y << endl << endl;
    cout << "permuted right x \n" << y * perm << endl << endl;
    cout << "permuted both x \n" << perm * y * perm << endl << endl;
}

它可以对行和列进行置换,但不能同时进行.有谁知道列和行都可以如何排列?

It permutes the rows and permutes the columns, but it doesn't do both. Does anyone know how both the columns and rows can be permuted?

谢谢.

推荐答案

如果要应用对称置换P * Y * P^-1,则最好使用twistedBy方法:

If you want to apply a symmetric permutation P * Y * P^-1, then the best is to use the twistedBy method:

SpMat z = y.twistedBy(perm);

否则,您必须先应用一个,然后再应用另一个:

otherwise you have to apply one and then the other:

SpMAt z = (perm * y).eval() * perm;

这篇关于特征根中的置换稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆