예제 #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)
    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)
    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_lstsq(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        smol, wide, big, tall = [arr.shape for arr in arrays[:-2]]

        # with self.subTest('lstsq'):
        expect = gf.return_shape('(a,b),(a,c)->(b,c)', tall, big)
        self.assertArrayShape(la.lstsq(m_bs, m_bb), expect)
        expect = gf.return_shape('(a,b),(a,c)->(b,c)', wide, smol)
        self.assertArrayShape(la.lstsq(m_sb, m_ss), expect)
        self.assertArrayShape(la.lstsq(m_sb, v_s), utn.drop(wide))
        self.assertArrayShape(la.lstsq(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.rlstsq(m_ss, m_bs), expect)
        expect = gf.return_shape('(a,b),(c,b)->(a,c)', big, wide)
        self.assertArrayShape(la.rlstsq(m_bb, m_sb), expect)
        self.assertArrayShape(la.rlstsq(v_b, m_sb), wide[:-1])
        self.assertArrayShape(la.rlstsq(m_ss, v_s), smol[:-1])
        self.assertArrayShape(la.rlstsq(v_s, m_bs), tall[:-1])