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)
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)
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)))
def test_lu_raw_returns_expected_values_square(self, m_bb): sq_l, sq_u, sq_ip0 = gfl.lu_m(m_bb) sq_f, sq_ip = gfl.lu_rawm(m_bb) sq_f = la.transpose(sq_f) linds = (..., ) + np.tril_indices(m_bb.shape[-1], -1) uinds = (..., ) + np.triu_indices(m_bb.shape[-1], 0) # with self.subTest(msg="square"): cond = np.linalg.cond(m_bb).max() self.assertArrayAllClose(sq_f[linds], sq_l[linds], cond=cond) self.assertArrayAllClose(sq_f[uinds], sq_u[uinds], cond=cond) self.assertEqual(sq_ip, sq_ip0)
def test_lu_raw_returns_expected_values_tall(self, m_bs): tall = m_bs.shape hy.assume(hn.tall(m_bs)) cond = np.linalg.cond(m_bs).max() tl_l, tl_u, tl_ip0 = gfl.lu_n(m_bs) tl_f, tl_ip = gfl.lu_rawn(m_bs) tl_f = la.transpose(tl_f) linds = (..., ) + np.tril_indices(tall[-2], -1, tall[-1]) uinds = (..., ) + np.triu_indices(tall[-2], 0, tall[-1]) # with self.subTest(msg="tall"): self.assertArrayAllClose(tl_f[linds], tl_l[linds], cond=cond) self.assertArrayAllClose(tl_f[uinds], tl_u[uinds], cond=cond) self.assertEqual(tl_ip, tl_ip0)
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)
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)))
def test_qr_rawn_returns_expected_values(self, m_bs): hy.assume(hn.tall(m_bs)) hy.assume(hn.all_well_behaved(m_bs)) cond = np.linalg.cond(m_bs).max() rrr = gfl.qr_n(m_bs)[1] num = rrr.shape[-1] ht_bs, tau = gfl.qr_rawn(m_bs) h_bs = la.transpose(ht_bs) vecs = np.tril(h_bs, -1) vecs[(..., ) + np.diag_indices(num)] = 1 vnorm = gfb.norm(la.row(tau) * vecs, axis=-2)**2 right = np.triu(h_bs) # with self.subTest(msg='raw_n'): self.assertArrayAllClose(right[..., :num, :], 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_n'): self.assertArrayAllClose(right, m_bs, cond=cond)
def test_lq_rawn_returns_expected_values(self, m_bs): hy.assume(hn.tall(m_bs)) hy.assume(hn.all_well_behaved(m_bs)) cond = np.linalg.cond(m_bs).max() llo = gfl.lq_n(m_bs)[0] num = llo.shape[-1] ht_bs, tau = gfl.lq_rawn(m_bs) h_bs = la.transpose(ht_bs) vecs = np.triu(h_bs, 1) vecs[(..., ) + np.diag_indices(num)] = 1 vnorm = gfb.norm(la.col(tau) * vecs[..., :num, :], axis=-1)**2 left = np.tril(h_bs) # with self.subTest(msg='raw_n'): self.assertArrayAllClose(left, llo, cond=cond) # with self.subTest(msg='tau_n'): 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]) left -= ttt.conj() * (left @ la.dagger(vvv)) * vvv # with self.subTest(msg='h_n'): self.assertArrayAllClose(left, m_bs, cond=cond)
def test_functions_shape(self, array): shape = array.shape self.assertArrayShape(la.transpose(array), utn.trnsp(shape)) self.assertArrayShape(la.row(array), shape[:-1] + (1, ) + shape[-1:]) self.assertArrayShape(la.col(array), shape + (1, )) self.assertArrayShape(la.scalar(array), shape + (1, 1))