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_invarray_operators(self, arrays): m_ss, m_bs, m_sb, mini, 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_ss, mini)) cond = np.linalg.cond(m_ss).max() self.assertArrayAllClose(m_ss.inv @ m_sb, gf.solve(m_ss, m_sb), cond=cond) self.assertArrayAllClose(m_ss.inv() @ m_sb, gf.solve(m_ss, m_sb), cond=cond) self.assertArrayAllClose(m_bs @ m_ss.inv, gf.rsolve(m_bs, m_ss), cond=cond) self.assertArrayAllClose((m_ss.inv @ mini.inv).inv, mini @ m_ss) self.assertArrayAllClose((m_ss.inv * 3.5).inv, m_ss / 3.5) self.assertArrayAllClose((2.4 * m_ss.inv).inv, m_ss / 2.4) self.assertArrayAllClose((m_ss.inv / 3.564).inv, m_ss * 3.564) with self.assertRaises(TypeError): 45.564 / m_ss.inv # pylint: disable=pointless-statement self.assertArrayAllClose((mini.inv * scal).inv, mini / scal) self.assertArrayAllClose((scal * mini.inv).inv, mini / scal) self.assertArrayAllClose((mini.inv / scal).inv, mini * scal) with self.assertRaises(TypeError): scal / mini.inv # pylint: disable=pointless-statement with self.assertRaises(TypeError): scal.inv * mini.inv # pylint: disable=pointless-statement with self.assertRaises(TypeError): m_ss + mini.inv # pylint: disable=pointless-statement mini_ss = m_ss @ mini mini_i = mini.inv mini_i @= m_ss.inv self.assertArrayAllClose(mini, mini_ss)
def test_functions_solve(self, arrays): m_ss, m_sb, m_bs = arrays hy.assume(hn.all_well_behaved(m_ss)) cond = np.linalg.cond(m_ss).max() # with self.subTest('solve'): self.assertArrayAllClose(la.solve(m_ss, m_sb), gf.solve(m_ss, m_sb), cond=cond) 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.solve(m_ss, m_sb, out=slv_out) self.assertArrayAllClose(slv_out, slv_r) # with self.subTest('rsolve'): self.assertArrayAllClose(la.rsolve(m_bs, m_ss), gf.rsolve(m_bs, m_ss), cond=cond)
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_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))
def test_invarray_in_functions(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)) cond = np.linalg.cond(m_ss).max() self.assertArrayAllClose(gf.matmul(m_ss.inv, m_sb), gf.solve(m_ss, m_sb), cond=cond) self.assertArrayAllClose(gf.matmul(m_bs, m_ss.inv), gf.rsolve(m_bs, m_ss), cond=cond) self.assertArrayAllClose( gf.matmul(m_ss.inv, mini.inv).inv, mini @ m_ss) self.assertArrayAllClose(gf.solve(m_ss.inv, m_sb), gf.matmul(m_ss, m_sb)) self.assertArrayAllClose( gf.solve(mini, m_ss.inv).inv, gf.matmul(m_ss, mini)) self.assertArrayAllClose(gf.solve(mini.inv, m_ss.inv), gf.rsolve(mini, m_ss), cond=cond) self.assertArrayAllClose(gf.rsolve(m_ss, mini.inv), gf.matmul(m_ss, mini)) self.assertArrayAllClose( gf.rsolve(mini.inv, m_ss).inv, gf.matmul(m_ss, mini)) cond = np.linalg.cond(mini).max() self.assertArrayAllClose(gf.rsolve(mini.inv, m_ss.inv), gf.solve(mini, m_ss), cond=cond) self.assertArrayAllClose(gf.rmatmul(m_ss, mini.inv), gf.solve(mini, m_ss), cond=cond) self.assertArrayAllClose(gf.rmatmul(mini.inv, m_ss), gf.rsolve(m_ss, mini), cond=cond) self.assertArrayAllClose( gf.rmatmul(mini.inv, m_ss.inv).inv, mini @ m_ss)