# ## MIT License

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# In[1]:
import numpy as np
import numpy.linalg as la
np.set_printoptions(precision=2, linewidth=150)
# In[2]:
n = 8
e1 = np.zeros(n); e1[0] = 1
e2 = np.zeros(n); e2[1] = 1
A = np.random.randn(n, n)
A
# Now try to zero the first column with a similarity transform.
#
# ## Starting with the first row
#
# Lets first try to proceed as in Householder QR, creating a transformation
# $$H=I-2\frac{vv^T}{v^Tv}$$
# where $v = a_1 - ||a_1||_2e_1$ with $a_1$ being the first column of $A$.
# In[3]:
a = A[:, 0].copy()
v = a-la.norm(a)*e1
H1 = np.eye(n) - 2*np.outer(v, v)/(v@v)
# We can apply the transformation from the left as in QR to reduce the first column to a multiple of the first elementary vector.
# In[4]:
(H1@A).round(4)
# However, to ensure we do not perturb the eigenvalues of $A$, we must also apply the matrix from the right, resulting in a similarity transformation.
# In[5]:
(H1@A@H1.T).round(4)
# Note that applying the Householder transformation from the right filled in the elements annihilated by applying it from the left.
#
# ## Starting in the second row
#
# To avoid this, we define the Householder transformation to annihilate elements below the first subdiagonal. That way, the first transformation does not affect the first row when applied from the left, and consequently does not affect the first column when applied for the right, preserving the zeros we've annihilated.
# In[6]:
a = A[:, 0].copy()
a[0] = 0
v = a-la.norm(a)*e2
H2 = np.eye(n) - 2*np.outer(v, v)/(v@v)
# In[7]:
(H2 @ A).round(4)
# In[8]:
(H2 @ A @ H2.T).round(4)
# To generalize this process, we continue to eliminate everything below the subdiagonal in the next column and applying the two-sided transformations, finally resulting in an upper-Hessenberg matrix.
#
# -----
#
# Why does post-multiplying with `H2.T` not destroy the zeros?
# In[9]:
H2.T.round(4)
# In[ ]: