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_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_matrdiv(self, arrays): m_ss, m_sb, m_bs = arrays[:-1] v_b = hn.core_only(arrays[-1], dims=1) hy.assume(hn.all_well_behaved(m_ss)) hy.assume(hn.wide(m_sb)) # with self.subTest('rsolve'): cond = np.linalg.cond(m_ss).max() self.assertArrayAllClose(la.matrdiv(m_bs, m_ss), gf.rsolve(m_bs, m_ss), cond=cond) # with self.subTest('rlstsq'): cond = np.linalg.cond(m_bs).max() self.assertArrayAllClose(la.matrdiv(m_ss, m_bs), gf.rlstsq(m_ss, m_bs), cond=cond) cond = np.linalg.cond(m_sb).max() self.assertArrayAllClose(la.matrdiv(v_b, m_sb), gf.rlstsq(v_b, m_sb), cond=cond)
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_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)