C++ 如何计算C+中列随机矩阵的特征向量+;
我有一列随机矩阵a,想用C++求解以下方程: Ax=xC++ 如何计算C+中列随机矩阵的特征向量+;,c++,matrix,linear-algebra,eigenvector,eigenvalue,C++,Matrix,Linear Algebra,Eigenvector,Eigenvalue,我有一列随机矩阵a,想用C++求解以下方程: Ax=x 我假设我需要找出特征向量x,其中特征值设置为1(右),但我不能在C++中找到它。到目前为止,我已经检查了一些数学库,如Seldon、CPPScaLapack、Eigen。。。其中,Eigen似乎是一个不错的选择,但我不知道如何利用它们中的任何一个来解决上述方程 你能给我一些建议/代码片段或想法来解决这个问题吗? 非常感谢您的帮助 谢谢 编辑:A是n乘n的实矩阵,非负矩阵。请记住,我没有使用Eigen,但它可以解决这个问题。这些被列为“实验性
我假设我需要找出特征向量x,其中特征值设置为1(右),但我不能在C++中找到它。到目前为止,我已经检查了一些数学库,如Seldon、CPPScaLapack、Eigen。。。其中,Eigen似乎是一个不错的选择,但我不知道如何利用它们中的任何一个来解决上述方程
你能给我一些建议/代码片段或想法来解决这个问题吗? 非常感谢您的帮助 谢谢编辑:A是n乘n的实矩阵,非负矩阵。请记住,我没有使用Eigen,但它可以解决这个问题。这些被列为“实验性的”,因此可能会有bug,并且API将来可能会更改
// simple, not very efficient
template <typename _M>
bool isSelfAdjoint(const _M& a) {
return a == a.adjoint();
}
template <typename _M>
std::pair<Eigen::EigenSolver<_M>::EigenvalueType Eigen::EigenSolver<_M>::EigenvectorType>
eigenvectors(const _M& a) {
if (isSelfAdjoint(a)) {
Eigen::EigenSolver<M> saes(a);
return pair(saes.eigenvalues(), saes.eigenvectors());
} else {
Eigen::EigenSolver<M> es(a);
return pair(es.eigenvalues, es.eigenvectors());
}
}
辅助类的接口,这些接口没有被用于const正确性和其他使C++工作的必要细节。
template <typename _M>
class RowWithPivot {
public:
typedef _M::RowXpr Wrapped;
typedef _M::Scalar Scalar;
RowWithPivot(_M& matrix, size_t row);
Wrapped base();
operator Wrapped();
void swap(RowWithPivot& other);
int cmp(RowWithPivot& other) const;
bool operator <(RowWithPivot& other) const;
// returns the index of the first non-zero scalar
// (best to cache this)
int pivot_index() const;
// returns first non-zero scalar, or 0 if none
Scalar pivot() const;
};
template <typename _M, typename _R = RowWithPivot<_M> >
class RowMajor {
public:
typedef _R value_type;
RowMajor(_M& matrix);
operator _M&();
_M& base();
value_type operator[](size_t i);
class iterator {
public:
// standard random access iterator
...
};
iterator begin();
iterator end();
};
模板
类RowWithPivot{
公众:
typedefu M::RowXpr-Wrapped;
typedef_M::标量;
RowWithPivot(_M&matrix,size_t row);
包底();
运算符包装();
无效掉期(RowWithPivot和其他);
int cmp(RowWithPivot和其他)常数;
bool操作符请记住,我没有使用Eigen,但是它看起来能够解决这个问题。这些被列为“实验性的”,因此可能会有bug,API可能会在将来更改
// simple, not very efficient
template <typename _M>
bool isSelfAdjoint(const _M& a) {
return a == a.adjoint();
}
template <typename _M>
std::pair<Eigen::EigenSolver<_M>::EigenvalueType Eigen::EigenSolver<_M>::EigenvectorType>
eigenvectors(const _M& a) {
if (isSelfAdjoint(a)) {
Eigen::EigenSolver<M> saes(a);
return pair(saes.eigenvalues(), saes.eigenvectors());
} else {
Eigen::EigenSolver<M> es(a);
return pair(es.eigenvalues, es.eigenvectors());
}
}
辅助类的接口,这些接口没有被用于const正确性和其他使C++工作的必要细节。
template <typename _M>
class RowWithPivot {
public:
typedef _M::RowXpr Wrapped;
typedef _M::Scalar Scalar;
RowWithPivot(_M& matrix, size_t row);
Wrapped base();
operator Wrapped();
void swap(RowWithPivot& other);
int cmp(RowWithPivot& other) const;
bool operator <(RowWithPivot& other) const;
// returns the index of the first non-zero scalar
// (best to cache this)
int pivot_index() const;
// returns first non-zero scalar, or 0 if none
Scalar pivot() const;
};
template <typename _M, typename _R = RowWithPivot<_M> >
class RowMajor {
public:
typedef _R value_type;
RowMajor(_M& matrix);
operator _M&();
_M& base();
value_type operator[](size_t i);
class iterator {
public:
// standard random access iterator
...
};
iterator begin();
iterator end();
};
模板
类RowWithPivot{
公众:
typedefu M::RowXpr-Wrapped;
typedef_M::标量;
RowWithPivot(_M&matrix,size_t row);
包底();
运算符包装();
无效掉期(RowWithPivot和其他);
int cmp(RowWithPivot和其他)常数;
bool算子你是不是想在一般情况下解任意大小的矩阵A?A的维数是多少?允许复特征值吗?在这种情况下,特征值必须是1。Duh…我开始考虑一般解。在这种情况下,找到A-I的零空间可能更简单,因为(A-I)x=x是一个齐次系统。是复数的项吗?a是一个n乘n的方阵,严格实数,非负(>0)。@zipcodeman在这种情况下右特征值=1。我需要解(a-I)x=0我签出Seldon它有直接/迭代解算器支持,但在我的例子中我无法让它工作:你是否在任何大小的矩阵A的一般情况下尝试求解它?A可以是什么维度?允许复特征值吗?在这种情况下,特征值必须是1。嗯…我开始考虑一般的解决方案。在这种情况下,我求A-I的零空间可能更简单,因为(A-I)x=x是一个齐次系统。复数的项是吗?A是一个n×n的严格实非负(>0)的平方矩阵。在这种情况下@zipcodeman右特征值=1。我需要解(A-I)x=0我签出Seldon它有直接/迭代解算器支持,但在我的案例中我无法让它工作:sOutis非常感谢!我遇到了以下链接:。我正在使用SuperLU解决问题,因为效率在我的案例中至关重要;)无需;如果你找到了更好的解决方案,你可以发布并接受它。我想看看是什么如果是其他问题,你最终会做。非常感谢!我发现了以下链接:。我正在使用SuperLU解决问题,因为效率对我来说至关重要;)没有必要;如果你找到了更好的解决方案,你可以发布并接受它。如果是其他问题,我想看看你最终会做什么。