コード例 #1
0
    def test_lnarray_shape_methods(self, arrays):
        m_bs, m_ss = view_as(*arrays)
        tall, smol = m_bs.shape, m_ss.shape
        hy.assume(hn.tall(m_bs))
        hy.assume(np.max(np.abs(m_bs.imag)) > .01)
        hy.assume(np.max(np.abs(m_bs.real)) / np.max(np.abs(m_bs.imag)) < 1e3)

        expect = utn.trnsp(tall)
        self.assertArrayShape(m_bs.t, expect)
        self.assertArrayShape(m_bs.h, expect)
        self.assertArrayNotAllClose(m_bs.t, m_bs.h)
        self.assertArrayShape(m_ss.c, smol + (1, ))
        self.assertArrayShape(m_bs.c.uc, tall)
        expect = insert(smol)
        self.assertArrayShape(m_ss.r, expect)
        self.assertArrayShape(m_bs.r.ur, tall)
        self.assertArrayShape(m_ss.s, smol + (1, 1))
        self.assertArrayShape(m_bs.s.us, tall)

        # expect = smol[:1] + (1,) + smol[1:2] + (1,) + smol[2:]
        expect = insert(insert(smol, 2), 1)
        self.assertArrayShape(m_ss.expand_dims((1, 3)), expect)
        expect = tall[:1] + (np.prod(tall[1:4]), ) + tall[4:]
        self.assertArrayShape(m_bs.flattish(1, 4), expect)
        with self.assertRaisesRegex(ValueError, "repeated axis"):
            m_bs.expand_dims((m_bs.ndim - 1, -3))
        half = (m_bs.ndim + 2) // 2 + 1
        with self.assertRaises(ValueError):
            (m_bs.s).flattish(half, -half)
コード例 #2
0
    def test_qr_returns_expected_values_with_tall(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_n(m_bs)
        tall = unitary @ right
        eye = la.dagger(unitary) @ unitary
        id_s = np.identity(m_bs.shape[-1], 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_s, eye, cond=cond)
コード例 #3
0
    def test_lu_raw_returns_expected_values_tall(self, m_bs):
        tall = m_bs.shape
        hy.assume(hn.tall(m_bs))
        cond = np.linalg.cond(m_bs).max()

        tl_l, tl_u, tl_ip0 = gfl.lu_n(m_bs)
        tl_f, tl_ip = gfl.lu_rawn(m_bs)
        tl_f = la.transpose(tl_f)
        linds = (..., ) + np.tril_indices(tall[-2], -1, tall[-1])
        uinds = (..., ) + np.triu_indices(tall[-2], 0, tall[-1])
        # with self.subTest(msg="tall"):
        self.assertArrayAllClose(tl_f[linds], tl_l[linds], cond=cond)
        self.assertArrayAllClose(tl_f[uinds], tl_u[uinds], cond=cond)
        self.assertEqual(tl_ip, tl_ip0)
コード例 #4
0
    def test_lq_returns_expected_values_with_tall(self, m_bs):
        hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))
        cond = np.linalg.cond(m_bs).max()

        left, unitary = gfl.lq_n(m_bs)
        tall = left @ unitary
        eye = unitary @ la.dagger(unitary)
        eyet = la.dagger(unitary) @ unitary
        id_s = np.identity(m_bs.shape[-1], m_bs.dtype)
        # with self.subTest(msg='lq'):
        self.assertArrayAllClose(tall, m_bs, cond=cond)
        # with self.subTest(msg='Q Q^T'):
        self.assertArrayAllClose(id_s, eye, cond=cond)
        # with self.subTest(msg='Q^T Q'):
        self.assertArrayAllClose(id_s, eyet, cond=cond)
コード例 #5
0
    def test_rlstsq_qr_returns_expected_shape_tall(self, arrays, fun):
        m_ss, m_sb, m_bs = arrays
        hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))
        tall = m_bs.shape

        expect = utn.array_return_shape('(m,n),(p,n)->(m,p),(n,p)', m_ss, m_bs)
        tau = expect[1][:-2] + tau_len(m_bs, fun)
        result = fun(m_ss, m_bs)
        self.assertArrayShapesAre(result, expect + (tau, ))
        self.assertArrayShapesAre(unbroadcast_factors(m_bs, *result[1:]),
                                  (utn.trnsp(tall), tall[:-2] + tau[-1:]))
        with self.assertRaisesRegex(*utn.core_dim_err):
            fun(m_sb, m_bs)
        with self.assertRaisesRegex(*utn.broadcast_err):
            fun(*utn.make_bad_broadcast(m_ss, la.transpose(m_sb)))
コード例 #6
0
    def test_lu_basic_returns_expected_values_tall(self, m_bs):
        tall = m_bs.shape
        hy.assume(hn.tall(m_bs))
        cond = np.linalg.cond(m_bs).max()

        tl_l, tl_u, tl_ip = gfl.lu_n(m_bs)
        tal = gfl.rpivot(tl_l @ tl_u, tl_ip)
        tlp = gfl.pivot(m_bs, tl_ip)
        dinds = (..., ) + np.diag_indices(tall[-1], 2)  # to check l
        uinds = (..., ) + np.triu_indices(tall[-2], 1, tall[-1])  # to check l
        linds = (..., ) + np.tril_indices(tall[-1], -1, tall[-1])  # to check u
        # with self.subTest(msg="tall"):
        self.assertArrayAllClose(tl_l[dinds], 1.)
        self.assertArrayAllClose(tl_l[uinds], 0.)
        self.assertArrayAllClose(tl_u[linds], 0.)
        self.assertArrayAllClose(tl_l @ tl_u, tlp, cond=cond)
        self.assertArrayAllClose(tal, m_bs, cond=cond)
コード例 #7
0
    def test_pinv_returns_expected_values_tall(self, m_bs):
        hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))
        cond = np.linalg.cond(m_bs).max()

        # with self.subTest(msg='tall'):
        tall_p = gfl.pinv(m_bs)
        id_s = np.identity(m_bs.shape[-1], m_bs.dtype)
        self.assertArrayAllClose(tall_p @ m_bs, id_s, cond=cond)
        # with self.subTest(msg='tall,+qr'):
        tall_pq, tall_f, tall_tau = gfl.pinv_qrn(m_bs)
        qrf, tau = gfl.qr_rawn(m_bs)
        self.assertArrayAllClose(tall_pq, tall_p, cond=cond)
        self.assertArrayAllClose(tall_f, qrf, cond=cond)
        self.assertArrayAllClose(tall_tau, tau, cond=cond)
        # with self.subTest(msg='tall,-qr'):
        tall_qp = gfl.qr_pinv(tall_f, tall_tau)
        self.assertArrayAllClose(tall_qp, tall_p, cond=cond)
コード例 #8
0
    def test_rqr_lstsq_returns_expected_shape_tall(self, arrays, fun):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))

        _, x_f, tau = fun(m_ss, m_bs)
        expect = utn.array_return_shape('(n,m),(m,p)->(n,p)', x_f, m_bb)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_bb), expect)
        expect = utn.array_return_shape('(m,n),(n,p)->(m,p)', m_ss, x_f)
        self.assertArrayShape(gfl.rqr_lstsq(m_ss, x_f, tau), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.qr_lstsq(x_f, tau, m_sb)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rqr_lstsq(m_sb, x_f, tau)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.qr_lstsq(x_f, *utn.make_bad_broadcast(tau, m_bb, (1, 2)))
        x_f, tau = unbroadcast_factors(m_bs, x_f, tau)
        expect = utn.array_return_shape('(m,n),(m,p)->(n,p)', m_bs, m_bb)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_bb), expect)
コード例 #9
0
    def test_qr_rawn_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()

        rrr = gfl.qr_n(m_bs)[1]
        num = rrr.shape[-1]
        ht_bs, tau = gfl.qr_rawn(m_bs)
        h_bs = la.transpose(ht_bs)
        vecs = np.tril(h_bs, -1)
        vecs[(..., ) + np.diag_indices(num)] = 1
        vnorm = gfb.norm(la.row(tau) * vecs, axis=-2)**2
        right = np.triu(h_bs)
        # with self.subTest(msg='raw_n'):
        self.assertArrayAllClose(right[..., :num, :], 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_n'):
        self.assertArrayAllClose(right, m_bs, cond=cond)
コード例 #10
0
    def test_lq_rawn_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()

        llo = gfl.lq_n(m_bs)[0]
        num = llo.shape[-1]
        ht_bs, tau = gfl.lq_rawn(m_bs)
        h_bs = la.transpose(ht_bs)
        vecs = np.triu(h_bs, 1)
        vecs[(..., ) + np.diag_indices(num)] = 1
        vnorm = gfb.norm(la.col(tau) * vecs[..., :num, :], axis=-1)**2
        left = np.tril(h_bs)
        # with self.subTest(msg='raw_n'):
        self.assertArrayAllClose(left, llo, cond=cond)
        # with self.subTest(msg='tau_n'):
        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])
            left -= ttt.conj() * (left @ la.dagger(vvv)) * vvv
        # with self.subTest(msg='h_n'):
        self.assertArrayAllClose(left, m_bs, cond=cond)
コード例 #11
0
    def test_lnarray_operations_return_expected_values(self, arrays):
        m_bs, m_ss, vec = view_as(*arrays)
        m_bs_m = hn.core_only(m_bs)
        vec = hn.core_only(vec, dims=1)
        hy.assume(hn.tall(m_bs))
        hy.assume(m_ss.ndim != 3)  # causes np..solve's broadcasting issue
        hy.assume(hn.all_well_behaved(m_ss, m_bs_m))

        expect = utn.array_return_shape('(a,b),(b,c)->(a,c)', m_bs, m_ss)
        ts_o = np.empty(expect, m_ss.dtype)
        ts_r = la.matmul(m_bs, m_ss, ts_o)
        self.assertArrayAllClose(ts_r, ts_o)
        self.assertArrayAllClose(m_bs @ m_ss, np.matmul(m_bs, m_ss))
        self.assertArrayAllClose(m_bs @ m_ss, np.matmul(m_bs, m_ss))
        self.assertArrayAllClose(m_bs @ vec, np.matmul(m_bs, vec))
        cond = np.linalg.cond(m_ss).max()
        self.assertArrayAllClose(gf.solve(m_ss, vec),
                                 nl.solve(m_ss, vec.c).uc,
                                 cond=cond)
        cond = np.linalg.cond(m_bs_m).max()
        self.assertArrayAllClose(gf.lstsq(m_bs_m.t, vec),
                                 nl.lstsq(m_bs_m.t, vec, rcond=None)[0],
                                 cond=cond)
        self.assertArrayAllClose(gf.rmatmul(m_ss, m_bs), np.matmul(m_bs, m_ss))
コード例 #12
0
    def test_lstsq_qr_raises_with_low_rank(self, ones_bs):
        hy.assume(hn.tall(ones_bs))

        with self.assertRaisesRegex(*utn.invalid_err):
            gfl.lstsq_qrn(ones_bs, ones_bs[..., :2])