예제 #1
0
    def test_functions_lstsq(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays[:-1]
        v_b = hn.core_only(arrays[-1], dims=1)
        hy.assume(hn.wide(m_sb))
        cond_sb = np.linalg.cond(m_sb).max()
        cond_bs = np.linalg.cond(m_bs).max()

        # with self.subTest('lstsq'):
        self.assertArrayAllClose(la.lstsq(m_bs, m_bb),
                                 gf.lstsq(m_bs, m_bb),
                                 cond=cond_bs)
        self.assertArrayAllClose(la.lstsq(m_sb, m_ss),
                                 gf.lstsq(m_sb, m_ss),
                                 cond=cond_sb)
        lsq_sh = utn.array_return_shape('(a,b),(a,c)->(b,c)', m_bs, m_bb)
        lsq_out = np.empty(lsq_sh, m_bs.dtype)
        lsq_r = la.lstsq(m_bs, m_bb, out=lsq_out)
        self.assertArrayAllClose(lsq_out, lsq_r)
        # with self.subTest('rlstsq'):
        self.assertArrayAllClose(la.rlstsq(m_ss, m_bs),
                                 gf.rlstsq(m_ss, m_bs),
                                 cond=cond_bs)
        self.assertArrayAllClose(la.rlstsq(v_b, m_sb),
                                 gf.rlstsq(v_b, m_sb),
                                 cond=cond_sb)
예제 #2
0
    def test_lqr(self, arrays):
        m_sb, m_bs = arrays
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_sb, m_bs))
        box = np.s_[..., :m_sb.shape[-2], :]
        cond_sb = np.linalg.cond(m_sb).max()
        cond_bs = np.linalg.cond(m_bs).max()

        # with self.subTest("reduced"):
        unitary, right = la.lqr(m_bs, 'reduced')
        self.assertArrayAllClose(unitary @ right, m_bs, cond=cond_bs)
        left, unitary = la.lqr(m_sb, 'reduced')
        self.assertArrayAllClose(left @ unitary, m_sb, cond=cond_sb)
        # with self.subTest("complete"):
        unitary, right = la.lqr(m_bs, 'complete')
        self.assertArrayAllClose(unitary @ right, m_bs, cond=cond_bs)
        left, unitary = la.lqr(m_sb, 'complete')
        self.assertArrayAllClose(left @ unitary, m_sb, cond=cond_sb)
        # with self.subTest("r/l/raw"):
        right = la.lqr(m_bs, 'r')
        hhold, _ = la.lqr(m_bs, 'raw')
        self.assertArrayAllClose(right,
                                 np.triu(la.transpose(hhold))[box],
                                 cond=cond_bs)
        left = la.lqr(m_sb, 'r')
        hhold, _ = la.lqr(m_sb, 'raw')
        self.assertArrayAllClose(left,
                                 np.tril(la.transpose(hhold))[box[:-1]],
                                 cond=cond_sb)
예제 #3
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)
예제 #4
0
    def test_lu(self, arrays):
        m_ss, m_sb, m_bs = arrays
        box = np.s_[..., :m_sb.shape[-2], :]
        hy.assume(hn.wide(m_sb))

        # with self.subTest("square"):
        cond = np.linalg.cond(m_ss).max()
        lower, upper, piv = la.lu(m_ss, 'separate')
        luf, piv = la.lu(m_ss, 'raw')
        luf = la.transpose(luf)
        self.assertArrayAllClose(lower @ upper, gf.pivot(m_ss, piv), cond=cond)
        self.assertArrayAllClose(tril(lower), tril(luf), cond=cond)
        self.assertArrayAllClose(upper, np.triu(luf), cond=cond)
        # with self.subTest("wide"):
        cond = np.linalg.cond(m_bs).max()
        lower, upper, piv = la.lu(m_bs, 'separate')
        luf, piv = la.lu(m_bs, 'raw')
        luf = la.transpose(luf)
        self.assertArrayAllClose(tril(lower), tril(luf), cond=cond)
        self.assertArrayAllClose(upper, np.triu(luf)[box], cond=cond)
        # with self.subTest("wide"):
        cond = np.linalg.cond(m_sb).max()
        lower, upper, piv = la.lu(m_sb, 'separate')
        luf, piv = la.lu(m_sb, 'raw')
        luf = la.transpose(luf)
        self.assertArrayAllClose(tril(lower), tril(luf)[box[:-1]], cond=cond)
        self.assertArrayAllClose(upper, np.triu(luf), cond=cond)
예제 #5
0
    def test_rlstsq_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))

        self.assertArrayShape(gfl.rlstsq(m_sb, v_b), wide[:-1])
        self.assertArrayShape(gfl.rlstsq(m_bs, v_s), tall[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlstsq(m_bs, v_b)
예제 #6
0
    def test_rlstsq_qr_flexible_signature_with_vectors_mv(self, arrays, fun):
        m_sb, m_bs = arrays[:-1]
        wide = m_sb.shape
        v_b = hn.core_only(arrays[-1], dims=1)
        hy.assume(hn.wide(m_sb))
        hy.assume(la.norm(v_b) > 0.)

        tau = m_sb.shape[:-2] + tau_len_vec(v_b, fun)
        self.assertArrayShapesAre(fun(m_sb, v_b),
                                  (wide[:-1], utn.drop(wide), tau))
        with self.assertRaisesRegex(*utn.core_dim_err):
            fun(m_bs, v_b)
    def test_lu_raw_returns_expected_shapes(self, arrays):
        m_sb, m_bb, m_bs = arrays
        wide, big, tall = [arr.shape for arr in arrays]
        hy.assume(hn.wide(m_sb))

        # with self.subTest(msg="square"):
        self.assertArrayShapesAre(gfl.lu_rawm(m_bb), (big, big[:-1]))
        # with self.subTest(msg="wide"):
        self.assertArrayShapesAre(gfl.lu_rawm(m_sb),
                                  (utn.trnsp(wide), wide[:-1]))
        # with self.subTest(msg="tall"):
        self.assertArrayShapesAre(gfl.lu_rawn(m_bs),
                                  (utn.trnsp(tall), utn.drop(tall)))
예제 #8
0
    def test_lq_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()

        left, unitary = gfl.lq_m(m_sb)
        wide = left @ unitary
        eye = unitary @ la.dagger(unitary)
        id_s = np.identity(m_sb.shape[-2], m_sb.dtype)
        # with self.subTest(msg='lq'):
        self.assertArrayAllClose(wide, m_sb, cond=cond)
        # with self.subTest(msg='Q Q^T'):
        self.assertArrayAllClose(id_s, eye, cond=cond)
예제 #9
0
    def test_rlstsq_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.wide(m_sb))
        off_b, y_one = utn.make_off_by_one(m_sb, m_bs)

        self.assertArrayShape(gfl.rlstsq(v_b, m_sb), wide[:-1])
        self.assertArrayShape(gfl.rlstsq(v_s, m_bs), tall[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlstsq(v_b, m_bs)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as vM:
            gfl.rlstsq(m_bs[y_one], m_sb[off_b])
    def test_lu_raw_returns_expected_values_wide(self, m_sb):
        wide = m_sb.shape
        hy.assume(hn.wide(m_sb))
        cond = np.linalg.cond(m_sb).max()

        wd_l, wd_u, wd_ip0 = gfl.lu_m(m_sb)
        wd_f, wd_ip = gfl.lu_rawm(m_sb)
        wd_f = la.transpose(wd_f)
        linds = (..., ) + np.tril_indices(wide[-2], -1, wide[-1])
        uinds = (..., ) + np.triu_indices(wide[-2], 0, wide[-1])
        # with self.subTest(msg="wide"):
        self.assertArrayAllClose(wd_f[linds], wd_l[linds], cond=cond)
        self.assertArrayAllClose(wd_f[uinds], wd_u[uinds], cond=cond)
        self.assertEqual(wd_ip, wd_ip0)
예제 #11
0
    def test_lq_l_returns_expected_values(self, arrays):
        m_sb, m_bs = arrays
        hy.assume(hn.all_well_behaved(m_bs, m_sb))
        hy.assume(hn.wide(m_sb))

        # with self.subTest(msg='l_m'):
        cond = np.linalg.cond(m_sb).max()
        left = gfl.lq_lm(m_sb)
        llo = gfl.lq_m(m_sb)[0]
        self.assertArrayAllClose(left, llo, cond=cond)
        # with self.subTest(msg='l_n'):
        cond = np.linalg.cond(m_bs).max()
        left = gfl.lq_ln(m_bs)
        llo = gfl.lq_n(m_bs)[0]
        self.assertArrayAllClose(left, llo, cond=cond)
예제 #12
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)
예제 #13
0
    def test_rlstsq_qr_returns_expected_shape_wide(self, arrays, fun):
        _, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_sb))
        wide = m_sb.shape

        expect = utn.array_return_shape('(m,n),(p,n)->(m,p),(n,p)', m_bb, m_sb)
        tau = expect[1][:-2] + tau_len(m_sb, fun)
        result = fun(m_bb, m_sb)
        self.assertArrayShapesAre(result, expect + (tau, ))
        self.assertArrayShapesAre(unbroadcast_factors(m_sb, *result[1:]),
                                  (utn.trnsp(wide), wide[:-2] + tau[-1:]))
        with self.assertRaisesRegex(*utn.core_dim_err):
            fun(m_bs, m_sb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            fun(*utn.make_bad_broadcast(m_bb, la.transpose(m_bs)))
예제 #14
0
    def test_functions_matrdiv(self, arrays):
        m_ss, m_sb, m_bs = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        smol, wide, tall = [arr.shape for arr in arrays[:-2]]
        hy.assume(hn.all_well_behaved(m_ss))
        hy.assume(hn.wide(m_sb))

        # with self.subTest('rsolve'):
        expect = gf.return_shape('(a,b),(c,b)->(a,c)', tall, smol)
        self.assertArrayShape(la.matrdiv(m_bs, m_ss), expect)
        self.assertArrayShape(la.matrdiv(v_s, m_ss), smol[:-1])
        # with self.subTest('rlstsq'):
        expect = gf.return_shape('(a,b),(c,b)->(a,c)', smol, tall)
        self.assertArrayShape(la.matrdiv(m_ss, m_bs), expect)
        self.assertArrayShape(la.matrdiv(v_b, m_sb), wide[:-1])
        self.assertArrayShape(la.matrdiv(m_ss, v_s), smol[:-1])
        self.assertArrayShape(la.matrdiv(v_s, m_bs), tall[:-1])
    def test_lu_basic_returns_expected_values_wide(self, m_sb):
        wide = m_sb.shape
        hy.assume(hn.wide(m_sb))
        cond = np.linalg.cond(m_sb).max()

        wd_l, wd_u, wd_ip = gfl.lu_m(m_sb)
        wid = gfl.rpivot(wd_l @ wd_u, wd_ip)
        wdp = gfl.pivot(m_sb, wd_ip)
        dinds = (..., ) + np.diag_indices(wide[-2], 2)  # to check l
        uinds = (..., ) + np.triu_indices(wide[-2], 1, wide[-2])  # to check l
        linds = (..., ) + np.tril_indices(wide[-2], -1, wide[-1])  # to check u
        # with self.subTest(msg="wide"):
        self.assertArrayAllClose(wd_l[dinds], 1.)
        self.assertArrayAllClose(wd_l[uinds], 0.)
        self.assertArrayAllClose(wd_u[linds], 0.)
        self.assertArrayAllClose(wd_l @ wd_u, wdp, cond=cond)
        self.assertArrayAllClose(wid, m_sb, cond=cond)
예제 #16
0
    def test_functions_matldiv(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays[:-1]
        v_s = hn.core_only(arrays[-1], dims=1)
        smol, wide, big, tall = [arr.shape for arr in arrays[:-1]]
        hy.assume(hn.all_well_behaved(m_ss))
        hy.assume(hn.wide(m_sb))

        # with self.subTest('solve'):
        expect = gf.return_shape('(a,b),(a,c)->(b,c)', smol, wide)
        self.assertArrayShape(la.matldiv(m_ss, m_sb), expect)
        self.assertArrayShape(la.matldiv(m_ss, v_s), smol[:-1])
        # with self.subTest('lstsq'):
        expect = gf.return_shape('(a,b),(a,c)->(b,c)', tall, big)
        self.assertArrayShape(la.matldiv(m_bs, m_bb), expect)
        expect = gf.return_shape('(a,b),(a,c)->(b,c)', wide, smol)
        self.assertArrayShape(la.matldiv(m_sb, m_ss), expect)
        self.assertArrayShape(la.matldiv(m_sb, v_s), utn.drop(wide))
        self.assertArrayShape(la.matldiv(v_s, m_ss), smol[:-1])
예제 #17
0
    def test_rlstsq_returns_expected_shape(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.wide(m_sb))

        # with self.subTest('underconstrained'):
        expect = utn.array_return_shape('(m,n),(p,n)->(m,p)', m_ss, m_bs)
        self.assertArrayShape(gfl.rlstsq(m_ss, m_bs), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlstsq(m_sb, m_bs)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.rlstsq(*utn.make_bad_broadcast(m_ss, la.transpose(m_sb)))
        # with self.subTest('overconstrained'):
        expect = utn.array_return_shape('(m,n),(p,n)->(m,p)', m_bb, m_sb)
        self.assertArrayShape(gfl.rlstsq(m_bb, m_sb), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlstsq(m_bs, m_sb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.rlstsq(*utn.make_bad_broadcast(m_bb, la.transpose(m_bs)))
예제 #18
0
    def test_lu(self, arrays):
        m_ss, m_sb, m_bs = arrays
        smol, wide, tall = [arr.shape for arr in arrays]
        hy.assume(hn.wide(m_sb))

        # with self.subTest("separate"):
        self.assertArrayShapesAre(la.lu(m_ss, 'separate'),
                                  (smol, smol, smol[:-1]))
        self.assertArrayShapesAre(la.lu(m_bs, 'separate'),
                                  (tall, utn.chop(tall), utn.drop(tall)))
        self.assertArrayShapesAre(la.lu(m_sb, 'separate'),
                                  (utn.chop(wide), wide, wide[:-1]))
        # with self.subTest("raw"):
        self.assertArrayShapesAre(la.lu(m_ss, 'raw'), (smol, smol[:-1]))
        self.assertArrayShapesAre(la.lu(m_bs, 'raw'),
                                  (utn.trnsp(tall), utn.drop(tall)))
        self.assertArrayShapesAre(la.lu(m_sb, 'raw'),
                                  (utn.trnsp(wide), wide[:-1]))
예제 #19
0
    def test_rqr_lstsq_returns_expected_shape_wide(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_sb))

        _, x_f, tau = fun(m_bb, m_sb)
        expect = utn.array_return_shape('(n,m),(m,p)->(n,p)', x_f, m_ss)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_ss), expect)
        expect = utn.array_return_shape('(m,n),(n,p)->(m,p)', m_bb, x_f)
        self.assertArrayShape(gfl.rqr_lstsq(m_bb, x_f, tau), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.qr_lstsq(x_f, tau, m_bs)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rqr_lstsq(m_bs, x_f, tau)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.qr_lstsq(x_f, *utn.make_bad_broadcast(tau, m_ss, (1, 2)))
        x_f, tau = unbroadcast_factors(m_sb, x_f, tau)
        expect = utn.array_return_shape('(m,n),(m,p)->(n,p)', m_sb, m_ss)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_ss), expect)
예제 #20
0
    def test_rlstsq_qr_flexible_signature_with_vectors_vm(self, arrays, fun):
        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))
        hy.assume(hn.all_well_behaved(m_sb, m_bs))
        off_b, y_one = utn.make_off_by_one(m_sb, m_bs)

        tau = m_sb.shape[:-2] + tau_len(m_sb, fun)
        self.assertArrayShapesAre(fun(v_b, m_sb),
                                  (wide[:-1], utn.trnsp(wide), tau))
        tau = m_bs.shape[:-2] + tau_len(m_bs, fun)
        self.assertArrayShapesAre(fun(v_s, m_bs),
                                  (tall[:-1], utn.trnsp(tall), tau))
        with self.assertRaisesRegex(*utn.core_dim_err):
            fun(v_b, m_bs)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as vM:
            fun(m_bs[y_one], m_sb[off_b])
예제 #21
0
    def test_pinv_returns_expected_values_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()

        id_s = np.identity(m_sb.shape[-2], m_sb.dtype)
        # with self.subTest(msg='wide'):
        wide_p = gfl.pinv(m_sb)
        self.assertArrayAllClose(m_sb @ wide_p, id_s, cond=cond)
        # with self.subTest(msg='wide,+qr'):
        wide_pq, wide_f, wide_tau = gfl.pinv_qrm(m_sb)
        # actually want lq here
        qrf, tau = gfl.lq_rawm(m_sb)
        # qrf = la.dagger(qrf)
        self.assertArrayAllClose(wide_pq, wide_p, cond=cond)
        self.assertArrayAllClose(wide_f, qrf, cond=cond)
        self.assertArrayAllClose(wide_tau, tau, cond=cond)
        # with self.subTest(msg='wide,-qr'):
        wide_qp = gfl.qr_pinv(wide_f, wide_tau)
        self.assertArrayAllClose(wide_qp, wide_p, cond=cond)
예제 #22
0
    def test_rlstsq_qr_returns_expected_values_with_wide(self, arrays, fun):
        m_ss, m_sb, m_bb = arrays
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_sb))
        cond = np.linalg.cond(m_sb).max()

        # overconstrained
        x0_bs = gfl.rlstsq(m_bb, m_sb)
        # overconstrained
        x_bs, x_f, tau = fun(m_bb, m_sb)
        # with self.subTest('rlstsq_qr(under,' + suffix):
        self.assertArrayAllClose(x_bs, x0_bs, cond=cond)
        # overconstrained
        xx_bs = gfl.rqr_lstsq(m_bb, x_f, tau)
        # with self.subTest('rqr_rlstsq(under,' + suffix):
        self.assertArrayAllClose(xx_bs, x0_bs, cond=cond)
        # underconstrained
        y_bs = gfl.qr_lstsq(x_f, tau, m_ss)
        # with self.subTest('qr_rlstsq(over,' + suffix):
        self.assertArrayAllClose(m_sb @ y_bs, m_ss, cond=cond)
예제 #23
0
    def test_lq_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.lq_m(m_sb), (utn.chop(wide), wide))
        self.assertArrayShape(gfl.lq_lm(m_sb), utn.chop(wide))
        # with self.subTest(msg='tall'):
        self.assertArrayShapesAre(gfl.lq_n(m_bs), (tall, utn.chop(tall)))
        self.assertArrayShape(gfl.lq_ln(m_bs), tall)
        with self.assertRaisesRegex(*utn.invalid_err):
            gfl.lq_m(m_bs)
        # with self.subTest(msg='complete'):
        self.assertArrayShapesAre(gfl.lq_n(m_sb), (wide, utn.grow(wide)))
        # with self.subTest(msg='raw'):
        self.assertArrayShapesAre(gfl.lq_rawm(m_sb),
                                  (utn.trnsp(wide), wide[:-1]))
        self.assertArrayShapesAre(gfl.lq_rawn(m_bs),
                                  (utn.trnsp(tall), utn.drop(tall)))
예제 #24
0
    def test_functions_matrdiv(self, arrays):
        m_ss, m_sb, m_bs = arrays[:-1]
        v_b = hn.core_only(arrays[-1], dims=1)
        hy.assume(hn.all_well_behaved(m_ss))
        hy.assume(hn.wide(m_sb))

        # with self.subTest('rsolve'):
        cond = np.linalg.cond(m_ss).max()
        self.assertArrayAllClose(la.matrdiv(m_bs, m_ss),
                                 gf.rsolve(m_bs, m_ss),
                                 cond=cond)
        # with self.subTest('rlstsq'):
        cond = np.linalg.cond(m_bs).max()
        self.assertArrayAllClose(la.matrdiv(m_ss, m_bs),
                                 gf.rlstsq(m_ss, m_bs),
                                 cond=cond)
        cond = np.linalg.cond(m_sb).max()
        self.assertArrayAllClose(la.matrdiv(v_b, m_sb),
                                 gf.rlstsq(v_b, m_sb),
                                 cond=cond)
예제 #25
0
    def test_rqr_lstsq_flexible_signature_with_vectors_vm(self, arrays, fun):
        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))
        hy.assume(hn.all_well_behaved(m_bs, m_sb))
        off_b, y_one = utn.make_off_by_one(m_sb, m_bs)

        _, x_f, tau = fun(v_s, m_bs)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, v_b), utn.drop(tall))
        self.assertArrayShape(gfl.rqr_lstsq(v_s, x_f, tau), tall[:-1])

        _, x_f, tau = fun(v_b, m_sb)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, v_s), utn.drop(wide))
        self.assertArrayShape(gfl.rqr_lstsq(v_b, x_f, tau), wide[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rqr_lstsq(v_s, x_f, tau)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as Mv:
            gfl.rqr_lstsq(m_bs[y_one], x_f[off_b], tau[off_b])
예제 #26
0
    def test_functions_matldiv(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.all_well_behaved(m_ss))
        hy.assume(hn.wide(m_sb))

        # with self.subTest('solve'):
        self.assertArrayAllClose(la.matldiv(m_ss, m_sb), gf.solve(m_ss, m_sb))
        slv_sh = utn.array_return_shape('(a,a),(a,b)->(a,b)', m_ss, m_sb)
        slv_out = np.empty(slv_sh, m_ss.dtype)
        slv_r = la.matldiv(m_ss, m_sb, out=slv_out)
        cond = np.linalg.cond(m_ss).max()
        self.assertArrayAllClose(slv_out, slv_r, cond=cond)
        # with self.subTest('lstsq'):
        cond = np.linalg.cond(m_bs).max()
        self.assertArrayAllClose(la.matldiv(m_bs, m_bb),
                                 gf.lstsq(m_bs, m_bb),
                                 cond=cond)
        cond = np.linalg.cond(m_sb).max()
        self.assertArrayAllClose(la.matldiv(m_sb, m_ss),
                                 gf.lstsq(m_sb, m_ss),
                                 cond=cond)
예제 #27
0
    def test_rqr_lstsq_flexible_signature_with_vectors_mv(self, arrays, fun):
        m_sb, m_bs = hn.core_only(*arrays[:-1])
        v_s = hn.core_only(arrays[-1], dims=1)
        wide, tall = [arr.shape[-2:] for arr in arrays[:-1]]
        hy.assume(hn.wide(m_sb))
        hy.assume(la.norm(v_s) > 0.)

        _, x_f, tau = fun(m_bs, v_s)
        expect = utn.array_return_shape('(m,n),(n,p)->(m,p)', m_bs, m_sb)[:-1]
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_sb), expect)
        self.assertArrayShape(gfl.rqr_lstsq(m_bs, x_f, tau), tall[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.qr_lstsq(x_f, tau, m_bs)
        self.assertArrayShape(gfl.rqr_lstsq(v_s, x_f, tau), tall[:-2])

        m_sb, m_bs = arrays[:-1]
        wide, tall = [arr.shape for arr in arrays[:-1]]

        _, x_f, tau = fun(m_bs, v_s)
        x_f, tau = unbroadcast_factors(v_s, x_f, tau)
        self.assertArrayShape(gfl.qr_lstsq(x_f, tau, m_sb), utn.drop(wide))
        self.assertArrayShape(gfl.rqr_lstsq(m_bs, x_f, tau), tall[:-1])
예제 #28
0
    def test_lq(self, arrays):
        m_sb, m_bs = arrays
        wide, tall = [arr.shape for arr in arrays]
        hy.assume(hn.wide(m_sb))

        # with self.subTest("reduced"):
        self.assertArrayShapesAre(la.lq(m_bs, 'reduced'),
                                  (tall, utn.chop(tall)))
        self.assertArrayShapesAre(la.lq(m_sb, 'reduced'),
                                  (utn.chop(wide), wide))
        # with self.subTest("complete"):
        self.assertArrayShapesAre(la.lq(m_bs, 'complete'),
                                  (tall, utn.chop(tall)))
        self.assertArrayShapesAre(la.lq(m_sb, 'complete'),
                                  (wide, utn.grow(wide)))
        # with self.subTest("l/raw"):
        self.assertArrayShape(la.lq(m_bs, 'l'), tall)
        self.assertArrayShape(la.lq(m_sb, 'l'), utn.chop(wide))
        self.assertArrayShapesAre(la.lq(m_bs, 'raw'),
                                  (utn.trnsp(tall), utn.drop(tall)))
        self.assertArrayShapesAre(la.lq(m_sb, 'raw'),
                                  (utn.trnsp(wide), wide[:-1]))
예제 #29
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)
예제 #30
0
    def test_pinv_returns_expected_shapes(self, arrays):
        m_sb, m_bs = arrays
        wide, tall = [arr.shape for arr in arrays]
        hy.assume(hn.wide(m_sb))
        hy.assume(hn.all_well_behaved(m_bs, m_sb))

        # with self.subTest(msg='wide'):
        self.assertArrayShape(gfl.pinv(m_sb), utn.trnsp(wide))
        # with self.subTest(msg='tall'):
        self.assertArrayShape(gfl.pinv(m_bs), utn.trnsp(tall))
        # with self.subTest(msg='wide,+qr'):
        self.assertArrayShapesAre(
            gfl.pinv_qrm(m_sb), (utn.trnsp(wide), utn.trnsp(wide), wide[:-1]))
        # with self.subTest(msg='tall,+qr'):
        self.assertArrayShapesAre(
            gfl.pinv_qrn(m_bs),
            (utn.trnsp(tall), utn.trnsp(tall), utn.drop(tall)))
        # with self.subTest(msg='wide,-qr'):
        _, m_sb_f, m_sb_tau = gfl.pinv_qrm(m_sb)
        self.assertArrayShape(gfl.qr_pinv(m_sb_f, m_sb_tau), utn.trnsp(wide))
        # with self.subTest(msg='tall,-qr'):
        _, m_bs_f, m_bs_tau = gfl.pinv_qrn(m_bs)
        self.assertArrayShape(gfl.qr_pinv(m_bs_f, m_bs_tau), utn.trnsp(tall))