def test_good_p_invarray_combos_in_lstsq(self, arrays):
        m_ss, m_bs, m_sb, mini = view_as(*arrays)
        # hy.assume(hn.tall(m_bs))
        hy.assume(hn.all_well_behaved(m_ss, mini, m_bs, m_sb))

        self.assertArrayAllClose(la.lstsq(mini.inv, m_sb),
                                 la.matmul(mini, m_sb))
        self.assertArrayAllClose(la.rlstsq(m_bs, mini.inv),
                                 la.matmul(m_bs, mini))
        cond = np.linalg.cond(m_bs).max()
        self.assertArrayAllClose(la.lstsq(mini.inv, m_bs.pinv),
                                 la.rlstsq(mini, m_bs),
                                 cond=cond)
        cond = np.linalg.cond(mini).max()
        self.assertArrayAllClose(la.rlstsq(mini.inv, m_sb.pinv),
                                 la.solve(mini, m_sb),
                                 cond=cond)
        cond = np.linalg.cond(m_ss).max()
        self.assertArrayAllClose(la.lstsq(mini.inv, m_ss.inv),
                                 la.rsolve(mini, m_ss),
                                 cond=cond)
        cond = np.linalg.cond(mini).max()
        self.assertArrayAllClose(la.rlstsq(mini.inv, m_ss.inv),
                                 la.solve(mini, m_ss),
                                 cond=cond)
        cond = np.linalg.cond(m_ss).max()
        self.assertArrayAllClose(la.lstsq(m_bs.pinv, m_ss.inv),
                                 la.rsolve(m_bs, m_ss),
                                 cond=cond)
        cond = np.linalg.cond(m_sb).max()
        self.assertArrayAllClose(la.rlstsq(m_sb.pinv, m_ss.inv),
                                 la.lstsq(m_sb, m_ss),
                                 cond=cond)
Пример #2
0
    def test_functions_matmul(self, arrays):
        m_sb, m_bs = arrays

        # with self.subTest('matmul'):
        self.assertArrayAllClose(la.matmul(m_bs, m_sb), gf.matmul(m_bs, m_sb))
        # with self.subTest('rmatmul'):
        self.assertArrayAllClose(gf.rmatmul(m_bs, m_sb), gf.matmul(m_sb, m_bs))
    def test_return_array_types(self, arrays):
        m_sb_n, m_bs_n = arrays[:2]
        m_sb, m_bs, m_ss, m_bb = view_as(*arrays)
        m_bs_m, m_ss_m, m_bb_m = hn.core_only(m_bs, m_ss, m_bb)
        hy.assume(hn.all_well_behaved(m_ss, m_bb))
        hy.assume(m_sb.ndim != m_ss.ndim - 1)  # np..solve's broadcasting issue

        self.assertIsInstance(m_sb @ m_bs, la.lnarray)
        self.assertIsInstance(m_sb_n @ m_bs, la.lnarray)
        expect = utn.array_return_shape('(a,b),(b,c)->(a,c)', m_bs, m_sb)
        tw_o = np.empty(expect, m_bs.dtype)
        tw_r = la.matmul(m_bs, m_sb_n, tw_o)
        self.assertIsInstance(tw_r, np.ndarray)
        self.assertIsInstance(tw_o, np.ndarray)
        self.assertIsInstance(np.matmul(m_bs, m_sb_n), np.ndarray)
        self.assertIsInstance(la.solve(m_ss, m_sb_n), la.lnarray)
        self.assertIsInstance(nl.solve(m_ss, m_sb_n), np.ndarray)
        self.assertIsInstance(la.lstsq(m_bs, m_bb), la.lnarray)
        self.assertIsInstance(
            nl.lstsq(m_bs_m, m_bb_m, rcond=None)[0], np.ndarray)
        self.assertIsInstance(la.lu(m_ss)[0], la.lnarray)
        self.assertIsInstance(la.lu(m_bs_n)[0], np.ndarray)
        self.assertIsInstance(la.qr(m_ss)[0], la.lnarray)
        self.assertIsInstance(la.qr(m_bs_n)[0], np.ndarray)
        self.assertIsInstance(la.lq(m_ss)[0], la.lnarray)
        self.assertIsInstance(la.lq(m_bs_n)[0], np.ndarray)
        self.assertIsInstance(la.lqr(m_ss)[0], la.lnarray)
        self.assertIsInstance(la.lqr(m_bs_n)[0], np.ndarray)
        self.assertIsInstance(nl.qr(m_ss_m)[0], np.ndarray)
Пример #4
0
    def test_functions_matmul(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]]

        # with self.subTest('matmul'):
        expect = gf.return_shape('(a,b),(b,c)->(a,c)', tall, wide)
        self.assertArrayShape(la.matmul(m_bs, m_sb), expect)
        self.assertArrayShape(la.matmul(m_bs, v_s), tall[:-1])
        self.assertArrayShape(la.matmul(v_b, m_bs), utn.drop(tall))
        self.assertArrayShape(la.matmul(v_s, v_s), ())
        # with self.subTest('rmatmul'):
        expect = gf.return_shape('(a,b),(b,c)->(a,c)', wide, tall)
        self.assertArrayShape(gf.rmatmul(m_bs, m_sb), expect)
        self.assertArrayShape(gf.rmatmul(m_sb, v_s), utn.drop(wide))
        self.assertArrayShape(gf.rmatmul(v_b, m_sb), wide[:-1])
        self.assertArrayShape(gf.rmatmul(v_b, v_b), ())
    def test_bad_p_invarray_combos_in_functions(self, arrays):
        m_ss, m_bs, m_sb, mini = view_as(*arrays)
        # hy.assume(hn.tall(m_bs))

        with self.assertRaises(TypeError):
            la.solve(m_sb.pinv, mini)
        with self.assertRaises(TypeError):
            la.rsolve(mini, m_bs.pinv)
        with self.assertRaises(TypeError):
            la.solve(mini, m_bs.pinv)
        with self.assertRaises(TypeError):
            la.rsolve(m_sb.pinv, mini)
        with self.assertRaises(TypeError):
            la.solve(m_sb.pinv, m_bs.pinv)
        with self.assertRaises(TypeError):
            la.rsolve(m_sb.pinv, m_bs.pinv)
        with self.assertRaises(TypeError):
            la.matmul(m_ss.inv, m_bs.pinv)
        with self.assertRaises(TypeError):
            la.matmul(m_sb.pinv, mini.inv)
        with self.assertRaises(TypeError):
            la.solve(m_bs.pinv, mini.inv)
        with self.assertRaises(TypeError):
            la.rsolve(mini.inv, m_sb.pinv)
    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))