def test_rqr_lstsq_flexible_signature_with_vectors_vv(self, v_s, v_b, fun): hy.assume(len(v_s) != len(v_b)) hy.assume(la.norm(v_s) > 0.) _, x_f, tau = fun(v_s, v_s) self.assertArrayShape(gfl.qr_lstsq(x_f, tau, v_s), ()) self.assertArrayShape(gfl.rqr_lstsq(v_s, x_f, tau), ()) with self.assertRaisesRegex(*utn.core_dim_err): gfl.qr_lstsq(x_f, tau, v_b) with self.assertRaisesRegex(*utn.core_dim_err): gfl.rqr_lstsq(v_b, x_f, tau)
def test_qr_lstsq_returns_expected_shape_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)) _, x_f, tau = fun(m_sb, m_ss) 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_bb) with self.assertRaisesRegex(*utn.core_dim_err): gfl.rqr_lstsq(m_ss, 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),(p,n)->(m,p)', m_bb, m_sb) self.assertArrayShape(gfl.rqr_lstsq(m_bb, x_f, tau), expect)
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])
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])
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)