本征对角矩阵的构造 [英] Constructing diagonal matrix in eigen

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

问题描述

在特征值中,我们可以创建一个矩阵,如下所示:

In eigen, we can create a matrix as

Matrix3f m;
m << 1, 2, 3,
     4, 5, 6,
     7, 8, 9;

如何创建像下面这样的对角矩阵

How can I create a diagonal matrix like the one below

 3, 0, 0,
 0, 8, 0,
 0, 0, 6;

我不明白Eigen如何处理对角矩阵?这里仅对角线元素很重要。因此,Eigen是否保存了上面示例中的所有9个元素,或者Eigen仅保存了3、8、6个元素。另外,如果特征值保存所有9个元素,那么是否有必要将矩阵定义为对角线,还是与定义普通3 * 3矩阵相同?

I don't understand how Eigen handle diagonal matrix? Only the diagonal elements are important here. So does Eigen save all 9 elements from above example or Eigen just save only 3 elements 3,8,6. Also, if eigen save all 9 elements then is it necessary to define the matrix as diagonal or is it the same as defining normal 3*3 matrix?

推荐答案

如果您想要一个独立的对角矩阵,请构造一个 DiagonalMatrix

If you want a standalone diagonal matrix, construct a DiagonalMatrix.

DiagonalMatrix<double, 3> m(3, 8, 6);

// Input after construction
m.diagonal() << 3, 8, 6;

DiagonalMatrix的作用类似于普通矩阵,但仅存储对角线。

A DiagonalMatrix works like a normal matrix but stores only the diagonal.

Vector3d v(1, 2, 3);
m * v;  // 3 16 18

如果要使用现有矢量制作对角矩阵,请调用。 asDiagonal ()。请注意, .diagonal()返回对角线作为矢量,因此 .diagonal()。asDiagonal()提取矩阵的对角线部分,并将其视为对角线矩阵。

If you want to make a diagonal matrix out of an existing vector, call .asDiagonal(). Note that .diagonal() returns the diagonal as a vector, so .diagonal().asDiagonal() extract the diagonal part of a matrix and regard it as a diagonal matrix.

这篇关于本征对角矩阵的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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