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_pinvarray_in_functions(self, arrays): m_sb, high, m_bs = view_as(*arrays) # hy.assume(hn.tall(m_bs)) hy.assume(hn.all_well_behaved(m_bs)) cond = np.linalg.cond(m_bs).max() self.assertArrayAllClose(gf.matmul(m_bs.pinv, high), gf.lstsq(m_bs, high), cond=cond) self.assertArrayAllClose(gf.matmul(m_sb, m_bs.pinv.t), gf.rlstsq(m_sb, m_bs.t), cond=cond) xpout = la.pinvarray(np.empty_like(m_bs)) m_bs_p = np.multiply(m_bs.pinv, 2, out=xpout) self.assertArrayAllClose(m_bs_p.pinv, xpout.pinv) self.assertArrayAllClose(m_bs_p.pinv, m_bs / 2) with self.assertRaises(TypeError): gf.matmul(m_bs.pinv, m_sb.pinv) self.assertArrayAllClose(gf.lstsq(m_sb.pinv, high), gf.matmul(m_sb, high)) with self.assertRaises(TypeError): gf.lstsq(high, m_sb.pinv) self.assertArrayAllClose(gf.rlstsq(high.t, m_sb.t.pinv), gf.matmul(high.t, m_sb.t)) with self.assertRaises(TypeError): gf.rlstsq(m_sb.t.pinv, high.t) with self.assertRaises(TypeError): gf.rmatmul(m_sb.pinv, m_bs.pinv) with self.assertRaises(TypeError): gf.solve(m_sb.pinv, high) with self.assertRaises(TypeError): gf.rsolve(m_sb, m_bs.pinv)
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)
def test_pinvarray_operators(self, arrays): m_sb, high, m_bs, scal = view_as(*arrays) scal[np.abs(scal) < 1e-5] += 1. scal = scal.s # hy.assume(hn.tall(m_bs)) hy.assume(hn.all_well_behaved(m_bs)) cond = np.linalg.cond(m_bs).max() self.assertArrayAllClose(m_bs.pinv @ high, gf.lstsq(m_bs, high), cond=cond) self.assertArrayAllClose(m_bs.pinv() @ high, gf.lstsq(m_bs, high), cond=cond) self.assertArrayAllClose(m_sb @ m_bs.pinv.t, gf.rlstsq(m_sb, m_bs.t), cond=cond) with self.assertRaises(TypeError): m_bs.pinv @ m_sb.pinv # pylint: disable=pointless-statement self.assertArrayAllClose((m_bs.pinv * 3.5).pinv, m_bs / 3.5) self.assertArrayAllClose((2.4 * m_bs.pinv).pinv, m_bs / 2.4) self.assertArrayAllClose((m_bs.pinv / 3.564).pinv, m_bs * 3.564) with self.assertRaises(TypeError): 65 / m_bs.pinv # pylint: disable=pointless-statement self.assertArrayAllClose((m_bs.pinv * scal).pinv, m_bs / scal) self.assertArrayAllClose((scal * m_bs.pinv).pinv, m_bs / scal) self.assertArrayAllClose((m_bs.pinv / scal).pinv, m_bs * scal) with self.assertRaises(TypeError): scal / m_bs.pinv # pylint: disable=pointless-statement with self.assertRaises(TypeError): scal.pinv * m_bs.pinv # pylint: disable=pointless-statement with self.assertRaises(TypeError): m_bs.pinv + m_sb # pylint: disable=pointless-statement told = 1. * m_bs m_bs_p = m_bs.pinv m_bs_p *= 2 self.assertArrayAllClose(m_bs, told / 2)
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))