PDL / 向量和矩阵代数 / 对称矩阵的特征值排序

问题描述 投票:0回答:1

我正在尝试使用伟大的 perl PDL 的伟大的 eigens_sym 函数来计算对称矩阵的特征向量;代码片段

use PDL ;

<etc>

print "\nA: \n" ; 

print $matrix ; # Here $matrix is the matrix A 

<etc>

$matrix3 = $matrix2 x $matrix ; # Here $matrix3 is set to the matrix product A* A  , where  A*  is the transpose of A 

print "\nA*A : \n" ; 

print $matrix3 ; 


($ev,$e) = eigens_sym $matrix3 ;  # Make the eigenvector matrix of A* A 



print "\nEigenvalues with eigenvectors of A*A : \n\n" ; 

print $e ;  # The eigenvalues 

print $ev ; # " The eigenvectors are returned in COLUMNS of the returned PDL "

打印:

A: 

[
 [ 2  1  9  2 -6  5]
 [ 0  5  3  0 -8 -3]
 [ 1  0  5  1 -6  5]
 [-4 -6 -7  2 -1  5]
 [ 5 -2  3  1 -8  3]
 [ 1 -6  9 -2  9  5]
]



A*A : 

[
 [ 47  10  75   0 -45  15]
 [ 10 102   6   0 -78 -76]
 [ 75   6 254  -6 -44  80]
 [  0   0  -6  14 -46  18]
 [-45 -78 -44 -46 282 -20]
 [ 15 -76  80  18 -20 118]
]




Eigenvalues with eigenvectors of A*A : 

[ 23.285687  4.3552833  277.10958 0.79058644  367.06761  144.39125]

[
 [  0.86502676   0.39257997  0.090239841 -0.070634235  -0.25470872  -0.14000255]
 [ -0.36255636   0.53399064  -0.40332417  -0.31499006  -0.16574127  -0.54226155]
 [ -0.19994359  -0.20028032   0.58246948      0.19444  -0.60114242  -0.42598184]
 [ -0.14771441   0.50816552 -0.082459683   0.81070439 -0.092150685   0.21775057]
 [-0.037584019   0.29620226   0.55789823  0.032583693   0.69935299  -0.33082462]
 [ -0.23889757   0.41791719   0.41456683  -0.44685776  -0.22066884   0.58994146]
]

。到目前为止一切顺利,我很高兴,但有一些问题:

  1. 有时我需要得到特征值 [ 23.285687 4.3552833 277.10958 0.79058644 367.06761 144.39125] 按升序或降序排序的结果,我怎样才能得到这个?

  2. 在上述作业之后如何释放对象 $matrix2 $matrix3 使用的内存?

  3. 对于进一步的向量/矩阵处理:是否有很好的 perl PDL 教程和手册?

sorting perl eigenvalue pdl
1个回答
0
投票

要回答您的第一个问题如何对特征值进行排序:您可以使用 qsort 函数:

# Sort eigenvalues in ascending order
my $sorted_ev_asc = qsort($e);

# Sorted eigenvalues in descending order
my $sorted_ev_desc = $sorted_ev_asc->slice("-1:0");

第二个问题:如何释放

$matrix3
的内存? Perl 自动管理变量的内存,通常不需要显式释放内存。但是,如果您想显式释放 PDL 变量占用的内存,可以使用 null 函数。

$matrix3 = PDL->null;

第三个问题:哪里可以找到PDL矩阵运算的教程?例如,看看这个页面:https://metacpan.org/pod/PDL::MatrixOps

© www.soinside.com 2019 - 2024. All rights reserved.