2020-12-13
作者:互联网
这是我的项目老师布置给我的代码优化题目,但对于一个初学c语言不到三个月的小白来说着实有点困难,所以求大佬帮助。
int ScaC_DSPF_sp_sort_singular_values(const int Nrows, const int Ncols, double *U, double *V, double *singular_values)
{
int i, j, row, max_index;
double temp;
// 用选择法实现奇异值从大到小排序, U的列和V的行也跟着改变顺序.
for (i = 0; i < Ncols - 1; i++)
{
max_index = i;
for (j = i + 1; j < Ncols; j++)
{
if (singular_values[j] > singular_values[max_index])
{
max_index = j;
}
}
if (max_index != i)
{
temp = singular_values[i];
singular_values[i] = singular_values[max_index];
singular_values[max_index] = temp;
#ifndef ENABLE_REDUCED_FORM_CN
for (row = 0; row < Nrows; row++)
{ // 交换列
temp = U[max_index + row * Nrows];
U[max_index + row * Nrows] = U[i + row * Nrows];
U[i + row * Nrows] = temp;
}
#else
for (row = 0; row < Nrows; row++)
{
temp = U[max_index + row * Ncols];
U[max_index + row * Ncols] = U[i + row * Ncols];
U[i + row * Ncols] = temp;
}
#endif
for (row = 0; row < Ncols; row++)
{ // 交换列
temp = V[max_index + row * Ncols];
V[max_index + row * Ncols] = V[i + row * Ncols];
V[i + row * Ncols] = temp;
}
}
}
return 0;
}
标签:index,13,12,temp,Ncols,singular,max,2020,row 来源: https://blog.csdn.net/m0_52467923/article/details/111145634