解决SVD分解U和V的符号和matlab不同的问题
/// <summary>
/// Sign correction to ensure deterministic output from SVD.
//Adjusts the columns of u and the rows of v such that the loadings in the
// columns in u that are largest in absolute value are always positive.
/// </summary>
/// <param name="U"></param>
/// <param name="V"></param>
/// <param name="u_based_desion">主成分数大于样本数则设置为false</param>
private void svd_flip(ref Matrix<double> U,ref Matrix<double> V,bool u_based_decision = true)
{
if(u_based_decision)
{
for (int j = 0; j < U.ColumnCount; j++)
{
int rowIndex = U.Column(j).AbsoluteMaximumIndex();
if (U[rowIndex,j] < 0)
{
U.SetColumn(j, U.Column(j).Multiply(-1));
V.SetRow(j, V.Row(j).Multiply(-1));
}
}
}
else
{
for (int j = 0; j < V.RowCount; j++)
{
int ColIndex = V.Row(j).AbsoluteMaximumIndex();
if (V[j, ColIndex] < 0)
{
U.SetColumn(j, U.Column(j).Multiply(-1));
V.SetRow(j, V.Row(j).Multiply(-1));
}
}
}
}