Example #1
0
    def test_qr_returns_expected_shapes(self, arrays):
        m_sb, m_bs = arrays
        wide, tall = [arr.shape for arr in arrays]
        hy.assume(hn.wide(m_sb))

        # with self.subTest(msg='wide'):
        self.assertArrayShapesAre(gfl.qr_m(m_sb), (utn.chop(wide), wide))
        self.assertArrayShape(gfl.qr_rm(m_sb), wide)
        with self.assertRaisesRegex(*utn.invalid_err):
            gfl.qr_n(m_sb)
        # with self.subTest(msg='tall'):
        self.assertArrayShapesAre(gfl.qr_n(m_bs), (tall, utn.chop(tall)))
        self.assertArrayShape(gfl.qr_rn(m_bs), utn.chop(tall))
        # with self.subTest(msg='complete'):
        self.assertArrayShapesAre(gfl.qr_m(m_bs), (utn.grow(tall), tall))
        # with self.subTest(msg='raw'):
        self.assertArrayShapesAre(gfl.qr_rawm(m_sb),
                                  (utn.trnsp(wide), wide[:-1]))
        self.assertArrayShapesAre(gfl.qr_rawn(m_bs),
                                  (utn.trnsp(tall), utn.drop(tall)))
Example #2
0
    def test_qr_r_returns_expected_values(self, arrays):
        m_sb, m_bs = arrays
        hy.assume(hn.all_well_behaved(m_sb, m_bs))
        hy.assume(hn.wide(m_sb))

        # with self.subTest(msg='r_m'):
        cond = np.linalg.cond(m_sb).max()
        right = gfl.qr_rm(m_sb)
        rrr = gfl.qr_m(m_sb)[1]
        self.assertArrayAllClose(right, rrr, cond=cond)
        # with self.subTest(msg='r_n'):
        cond = np.linalg.cond(m_bs).max()
        right = gfl.qr_rn(m_bs)
        rrr = gfl.qr_n(m_bs)[1]
        self.assertArrayAllClose(right, rrr, cond=cond)
Example #3
0
    def test_qr_complete_returns_expected_values(self, m_bs):
        hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))
        cond = np.linalg.cond(m_bs).max()

        unitary, right = gfl.qr_m(m_bs)
        tall = unitary @ right
        eye = la.dagger(unitary) @ unitary
        eyet = unitary @ la.dagger(unitary)
        id_b = np.identity(m_bs.shape[-2], m_bs.dtype)
        # with self.subTest(msg='qr'):
        self.assertArrayAllClose(tall, m_bs, cond=cond)
        # with self.subTest(msg='Q^T Q'):
        self.assertArrayAllClose(id_b, eye, cond=cond)
        # with self.subTest(msg='Q Q^T'):
        self.assertArrayAllClose(id_b, eyet, cond=cond)
Example #4
0
    def test_qr_returns_expected_values_with_wide(self, m_sb):
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_sb))
        cond = np.linalg.cond(m_sb).max()

        unitary, right = gfl.qr_m(m_sb)
        wide = unitary @ right
        eye = la.dagger(unitary) @ unitary
        eyet = unitary @ la.dagger(unitary)
        id_s = np.identity(m_sb.shape[-2], m_sb.dtype)
        # with self.subTest(msg='qr'):
        self.assertArrayAllClose(wide, m_sb, cond=cond)
        # with self.subTest(msg='Q^T Q'):
        self.assertArrayAllClose(id_s, eye, cond=cond)
        # with self.subTest(msg='Q Q^T'):
        self.assertArrayAllClose(id_s, eyet, cond=cond)
Example #5
0
    def test_qr_rawm_returns_expected_values(self, m_sb):
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_sb))
        cond = np.linalg.cond(m_sb).max()

        rrr = gfl.qr_m(m_sb)[1]
        num = rrr.shape[-2]
        ht_sb, tau = gfl.qr_rawm(m_sb)
        h_sb = la.transpose(ht_sb)
        vecs = np.tril(h_sb, -1)
        vecs[(..., ) + np.diag_indices(num)] = 1
        vnorm = gfb.norm(la.row(tau) * vecs[..., :num], axis=-2)**2
        right = np.triu(h_sb)
        # with self.subTest(msg='raw_m'):
        self.assertArrayAllClose(right, rrr, cond=cond)
        self.assertArrayAllClose(vnorm, 2 * tau.real, cond=cond)
        for k in range(num):
            vvv = vecs[..., num - k - 1:num - k]
            ttt = la.scalar(tau[..., -k - 1])
            right -= ttt * vvv * (la.dagger(vvv) @ right)
        # with self.subTest(msg='h_m'):
        self.assertArrayAllClose(right, m_sb, cond=cond)