简体   繁体   English

用于 QR 分解的户主反射

[英]House-Holder Reflection for QR Decomposition

I am trying to implement the QR decomposition via householder reflectors.我正在尝试通过家庭反射器实现 QR 分解。 While attempting this on a very simple array, I am getting weird numbers.在一个非常简单的数组上尝试这个时,我得到了奇怪的数字。 Anyone who can tell me, also, why using the @ vs * operator between vec and vec.T on the last line of the function definition gets major bonus points.任何人都可以告诉我,为什么在 function 定义的最后一行在 vec 和 vec.T 之间使用 @ vs * 运算符可以获得主要奖励积分。

This has stumped two math/comp sci phds as of this morning.截至今天早上,这已经难倒了两位数学/计算机科学博士。

    import numpy as np

    def householder(vec):
       vec[0] += np.sign(vec[0])*np.linalg.norm(vec)
       vec = vec/vec[0]
       gamma = 2/(np.linalg.norm(vec)**2)
       return np.identity(len(vec)) - gamma*(vec*vec.T)

    array = np.array([1, 3 ,4])
    Q = householder(array)
    print(Q@array)

Output: Output:

   array([-4.06557377, -7.06557377, -6.06557377])

Where it should be:它应该在哪里:

   array([5.09, 0, 0])

* is elementwise multiplication, @ is matrix multiplication. *是元素乘法, @是矩阵乘法。 Both have their uses, but for matrix calculations you most likely want the matrix product.两者都有其用途,但对于矩阵计算,您很可能需要矩阵乘积。

vec.T for an array returns the same array.数组的vec.T返回相同的数组。 A simple array only has one dimension, there is nothing to transpose.一个简单的数组只有一维,没有什么可以转置的。 vec*vec.T just returns the elementwise squared array. vec*vec.T只返回元素平方数组。

You might want to use vec=vec.reshape(-1,1) to get a proper column vector, a one-column matrix.您可能希望使用vec=vec.reshape(-1,1)来获得正确的列向量,即单列矩阵。 Then vec*vec.T does "by accident" the correct thing.然后vec*vec.T “偶然”做了正确的事情。 You might want to put the matrix multiplication operator there anyway.无论如何,您可能希望将矩阵乘法运算符放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM