我有一个复数向量(VSII_Complex
包含特征值)和一个复数矩阵(CUII_Complex
包含特征向量)。 VSII_Complex
的每个元素都对应CUII_Complex
列。我的问题是我想在VSII_Complex
内对Eigen值进行排序
根据他们的真实部分(非虚部),我将根据排序的CUII_Complex
对VSII_Complex
的列进行排序。以下代码是由我的朋友开发的,但我觉得这个代码有问题,但我无法弄清楚。我想知道是否有人可以告诉我有什么问题。
EIG eigA=EIG(m_IIStiffnessAct,m_IIMassAct,true);
ComplexColumnVector VSII_Complex=eigA.eigenvalues();
ComplexMatrix CUII_Complex=eigA.eigenvectors();
///// make eigenvalues in decreasing order, so do eigenvectors
for (long ii = 0; ii < VSII_Complex.rows(); ii++)
{
for (long jj = ii+1; jj < VSII_Complex.rows(); jj++)
{
if (VSII_Complex(ii).real() < VSII_Complex(jj).real())
{
Complex temp = VSII_Complex(ii);
VSII_Complex(ii) = VSII_Complex(jj);
VSII_Complex(jj) = temp;
for (long kk = 0; kk < CUII_Complex.rows(); kk++)
{
Complex tempVec = CUII_Complex(kk,ii);
CUII_Complex(kk,ii) = CUII_Complex(kk,jj);
CUII_Complex(kk,jj) = tempVec;
}
}
}
}
答案 0 :(得分:1)
只需使用内置排序,它返回元素之前的位置。
//couldn't find this in the docs, I'm overlooking something probably:
void swapColumns (ComplexMatrix &mat, octave_idx_type colA, octave_idx_type colB)
{
if(colA == colB) return;
ComplexColumnVector temp = mat.column(colA);
mat.insert(mat.column(colB),0,colA);
mat.insert(temp,0,colB);
}
bool isRealGreater(const Complex& a, const Complex& b)
{
return a.real() > b.real();
}
//presumably in another function
//int func() {
EIG eigA=EIG(m_IIStiffnessAct,m_IIMassAct,true);
ComplexColumnVector VSII_Complex=eigA.eigenvalues();
ComplexMatrix CUII_Complex=eigA.eigenvectors();
///// make eigenvalues in decreasing order, so do eigenvectors
//create indices from 1-len(VSII_Complex)
Array<octave_idx_type> sort_order(VSII_Complex.length(),0);
for(int i =0 ; i< sort_order.length(); i++)
{sort_order.elem(i)= i;}
//create sorting object and sort VSII_Complex in descending order of the real component
octave_sort<Complex> sort_obj(&isRealGreater);
sort_obj.sort(VSII_Complex.jit_slice_data(), sort_order.jit_slice_data(), VSII_Complex.length());
//swap the columns of CUII_Complex in the same way VSII_Complex got sorted
for(octave_idx_type i=0; i<sort_order.length(); i++)
{
if(sort_order.elem(i) > i)
{
swapColumns(CUII_Complex,i,sort_order.elem(i));
}
}
//}
Haven实际上对此进行了测试,因为我懒得安装八度,我只是阅读了文档。
答案 1 :(得分:0)
我用下面的代码解决了这个问题,我根据以下网页中的算法开发了这个代码。但PeterT的答案对我来说更好。
http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/
///// make eigenvalues in decreasing order, so do eigenvectors
ComplexColumnVector ComplexColumnVector_toBesSorted=VSII_Complex;
ComplexMatrix ComplexMatrix_toBeSorted=CUII_Complex;
for (long idx_start = 0; idx_start < ComplexColumnVector_toBesSorted.rows(); idx_start++)
{
long idx_smallest=idx_start;
for (long idx_current = idx_start+1; idx_current < ComplexColumnVector_toBesSorted.rows(); idx_current++)
{
if (ComplexColumnVector_toBesSorted(idx_current).real() < ComplexColumnVector_toBesSorted(idx_smallest).real())
idx_smallest=idx_current;
}
Complex Complex_temp=ComplexColumnVector_toBesSorted(idx_start);
ComplexColumnVector_toBesSorted(idx_start)=ComplexColumnVector_toBesSorted(idx_smallest);
ComplexColumnVector_toBesSorted(idx_smallest)=Complex_temp;
for (long kk = 0; kk < ComplexMatrix_toBeSorted.rows(); kk++)
{
Complex Complex_temp_2 = ComplexMatrix_toBeSorted(kk,idx_start);
ComplexMatrix_toBeSorted(kk,idx_start) = ComplexMatrix_toBeSorted(kk,idx_smallest);
ComplexMatrix_toBeSorted(kk,idx_smallest) = Complex_temp_2;
}
}