Matrix_hub
Matrix_hub copied to clipboard
householder函数向量模为0情况下计算错误以及是否需要添加0 == _x->data[0]的情况
作者你好,感谢开源 householder函数使用过程中遇到一些问题,当待变换的向量为0向量时,会计算错误,我将householder函数前4行修改为:
Matrix *H = NULL;
Matrix *y = M_Zeros(_x->row, _x->column);
MATRIX_TYPE x_norm = M_norm(_x, 2);
if (x_norm < 1e-5f)
{
M_free(y);
H = M_I(_x->row);
return H;
}
y->data[0] = x_norm;
Matrix *w = NULL;
当向量为0向量时,将H阵设为单位阵,计算就不会出现错误了, 此外我看参考文档https://max.book118.com/html/2017/0527/109650252.shtm 中提到sgn(a)在a=0的时候为0,所以将if分支修改为:
if (_x->data[0] > 1e-5f)
{
w = M_add_sub(1, _x, -1, y);
Matrix *temp_w = w;
w = M_numul(w, 1 / M_norm(w, 2));
M_free(temp_w);
}
else if (fabs(_x->data[0]) <= 1e-5f)
{
w = _x;
w = M_numul(w, 1 / M_norm(w, 2));
}
else
{
w = M_add_sub(1, _x, 1, y);
Matrix *temp_w = w;
w = M_numul(w, 1 / M_norm(w, 2));
M_free(temp_w);
}
从而和文档上对应。