Exemplo n.º 1
0
    def test_lstsq_returns_expected_values(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.wide(m_sb))
        cond_bs = np.linalg.cond(m_bs).max()
        cond_sb = np.linalg.cond(m_sb).max()

        # overconstrained
        x_sb = gfl.lstsq(m_bs, m_bb)
        m_bst = la.dagger(m_bs)
        # with self.subTest(msg='lstsq(over)'):
        self.assertArrayAllClose(m_bst @ m_bs @ x_sb,
                                 m_bst @ m_bb,
                                 cond=cond_bs)
        x_bs = gfl.rlstsq(m_bb, m_sb)
        m_sbt = la.dagger(m_sb)
        # with self.subTest(msg='rlstsq(over)'):
        self.assertArrayAllClose(x_bs @ m_sb @ m_sbt,
                                 m_bb @ m_sbt,
                                 cond=cond_sb)
        # underconstrained
        x_bs = gfl.lstsq(m_sb, m_ss)
        # with self.subTest(msg='lstsq(under)'):
        self.assertArrayAllClose(m_sb @ x_bs, m_ss, cond=cond_sb)
        x_sb = gfl.rlstsq(m_ss, m_bs)
        # with self.subTest(msg='rlstsq(under)'):
        self.assertArrayAllClose(x_sb @ m_bs, m_ss, cond=cond_bs)
Exemplo n.º 2
0
    def test_lstsq_flexible_signature_with_vectors_vm(self, arrays):
        m_sb, m_bs = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        wide, tall = [arr.shape for arr in arrays[:-2]]
        hy.assume(hn.nonsquare(m_sb))

        self.assertArrayShape(gfl.lstsq(v_s, m_sb), utn.drop(wide))
        self.assertArrayShape(gfl.lstsq(v_b, m_bs), utn.drop(tall))
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(v_s, m_bs)
Exemplo n.º 3
0
    def test_lstsq_returns_expected_shape(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.wide(m_sb))

        # with self.subTest('overconstrained'):
        expect = utn.array_return_shape('(m,n),(m,p)->(n,p)', m_bs, m_bb)
        self.assertArrayShape(gfl.lstsq(m_bs, m_bb), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(m_bs, m_sb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.lstsq(*utn.make_bad_broadcast(m_bs, m_bb))
        # with self.subTest('underconstrained'):
        expect = utn.array_return_shape('(m,n),(m,p)->(n,p)', m_sb, m_ss)
        self.assertArrayShape(gfl.lstsq(m_sb, m_ss), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(m_sb, m_bs)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.lstsq(*utn.make_bad_broadcast(m_sb, m_ss))
Exemplo n.º 4
0
    def test_lstsq_flexible_signature_with_vectors_mv(self, arrays):
        m_sb, m_bs = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        wide, tall = [arr.shape for arr in arrays[:-2]]
        hy.assume(hn.wide(m_sb))
        off_b, y_one = utn.make_off_by_one(m_sb, m_bs)

        self.assertArrayShape(gfl.lstsq(m_bs, v_b), utn.drop(tall))
        self.assertArrayShape(gfl.lstsq(m_sb, v_s), utn.drop(wide))
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(m_sb, v_b)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as Mv:
            gfl.lstsq(m_sb[off_b], m_bs[y_one])
Exemplo n.º 5
0
    def test_lstsq_qr_returns_expected_values_with_tall(self, arrays, fun):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_bs))
        cond = np.linalg.cond(m_bs).max()

        # overconstrained
        x0_sb = gfl.lstsq(m_bs, m_bb)
        # overconstrained
        x_sb, x_f, tau = fun(m_bs, m_bb)
        # with self.subTest('lstsq_qr(over,' + suffix):
        self.assertArrayAllClose(x_sb, x0_sb, cond=cond)
        # overconstrained
        xx_sb = gfl.qr_lstsq(x_f, tau, m_bb)
        # with self.subTest('qr_lstsq(over,' + suffix):
        self.assertArrayAllClose(xx_sb, x0_sb, cond=cond)
        # underconstrained
        y_sb = gfl.rqr_lstsq(m_ss, x_f, tau)
        # with self.subTest('rqr_lstsq(under,' + suffix):
        self.assertArrayAllClose(y_sb @ m_bs, m_ss, cond=cond)
Exemplo n.º 6
0
    def test_lstsq_flexible_signature_with_vectors_vv(self, v_s, v_b):
        hy.assume(len(v_s) != len(v_b))

        self.assertArrayShape(gfl.lstsq(v_s, v_s), ())
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(v_s, v_b)