예제 #1
0
    def test_rmatmul_returns_expected_shapes(self, arrays):
        m_sb, m_bs = arrays
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.nonsquare(m_bs))

        expect = utn.array_return_shape('(a,b),(b,c)->(a,c)', m_sb, m_bs)
        self.assertArrayShape(self.gfm.rmatmul(m_bs, m_sb), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            self.gfm.rmatmul(m_bs, m_bs)
        with self.assertRaisesRegex(*utn.broadcast_err):
            self.gfm.rmatmul(*utn.make_bad_broadcast(m_bs, m_sb))
예제 #2
0
    def test_rmatmul_flexible_signature_with_vectors(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        smol, wide, big, tall = [arr.shape for arr in arrays[:-2]]
        hy.assume(hn.nonsquare(m_sb))
        off_b, y_one = utn.make_off_by_one(m_sb, m_sb)

        # with self.subTest('matrix-vector'):
        self.assertArrayShape(self.gfm.rmatmul(v_s, m_bs), tall[:-1])
        self.assertArrayShape(self.gfm.rmatmul(v_b, m_sb), wide[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            self.gfm.rmatmul(v_b, m_bs)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as Mv:
            self.gfm.rmatmul(m_sb[y_one], m_sb[off_b])
        # w\ith self.subTest('vector-matrix'):
        self.assertArrayShape(self.gfm.rmatmul(m_ss, v_s), smol[:-1])
        self.assertArrayShape(self.gfm.rmatmul(m_bb, v_b), big[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            self.gfm.rmatmul(m_bs, v_s)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as vM:
            self.gfm.rmatmul(m_sb[off_b], m_sb[y_one])
        # with self.subTest('vector-vector'):
        self.assertArrayShape(self.gfm.rmatmul(v_b, v_b), ())
        with self.assertRaisesRegex(*utn.core_dim_err):
            self.gfm.rmatmul(v_b, v_s)
예제 #3
0
    def test_lstsq_flexible_signature_with_vectors_vm(self, arrays):
        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.nonsquare(m_sb))

        self.assertArrayShape(gfl.lstsq(v_s, m_sb), utn.drop(wide))
        self.assertArrayShape(gfl.lstsq(v_b, m_bs), utn.drop(tall))
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lstsq(v_s, m_bs)
    def test_rsolve_returns_expected_shapes(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_bb))

        expect = utn.array_return_shape('(a,b),(b,b)->(a,b)', m_sb, m_bb)
        self.assertArrayShape(gfl.rsolve(m_sb, m_bb), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve(m_bs, m_bb)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve(m_bs, m_sb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.rsolve(*utn.make_bad_broadcast(m_bs, m_ss))
    def test_rsolve_lu_flexible_signature_with_vectors(self, arrays):
        m_ss, m_sb, m_bb = arrays[:-1]
        v_s = hn.core_only(arrays[-1], dims=1)
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_ss))

        # with self.subTest('rsolve_lu'):
        self.assertArrayShapesAre(gfl.rsolve_lu(
            v_s, m_ss), (m_ss.shape[:-1], m_ss.shape, m_ss.shape[:-1]))
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve_lu(v_s, m_bb)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve_lu(v_s, m_sb)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as vM:
            gfl.rsolve_lu(m_sb, m_ss)
    def test_solve_flexible_signature_with_vectors(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays[:-1]
        v_s = hn.core_only(arrays[-1], dims=1)
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_ss))
        off_b, y_one = utn.make_off_by_one(m_bb, m_sb)

        # with self.subTest('solve'):
        self.assertArrayShape(gfl.solve(m_ss, v_s), m_ss.shape[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.solve(m_bb, v_s)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.solve(m_bs, v_s)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as Mv:
            gfl.solve(m_bb[off_b], m_sb[y_one])
    def test_rsolve_lu_returns_expected_shapes(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_bb))

        expect = utn.array_return_shape('(a,b),(b,b)->(a,b)', m_sb, m_bb)
        expect_f = expect[:-2] + m_bb.shape[-2:]
        result = gfl.rsolve_lu(m_sb, m_bb)
        self.assertArrayShapesAre(result, (expect, expect_f, expect_f[:-1]))
        self.assertArrayShapesAre(unbroadcast_factors(m_bb, *result[1:]),
                                  (m_bb.shape, m_bb.shape[:-1]))
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve_lu(m_bs, m_bb)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rsolve_lu(m_bs, m_sb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.rsolve_lu(*utn.make_bad_broadcast(m_bs, m_ss))
    def test_rlu_solve_flexible_signature_with_vectors(self, arrays):
        m_ss, m_sb = arrays[:-2]
        v_s, v_b = hn.core_only(*arrays[-2:], dims=1)
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_ss))
        off_b, y_one = utn.make_off_by_one(m_ss, m_sb)

        # with self.subTest('rlu_solve'):
        _, x_f, i_p = gfl.rsolve_lu(v_s, m_ss)
        self.assertArrayShape(gfl.rlu_solve(v_s, x_f, i_p), m_ss.shape[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlu_solve(v_b, x_f, i_p)
        with self.assertRaisesRegex(*utn.core_dim_err):
            # This would succeed/broadcast error if interpreted as vM:
            gfl.rlu_solve(m_sb[y_one], x_f[off_b], i_p[off_b])
        self.assertArrayShape(gfl.lu_solve(x_f, i_p, v_s), m_ss.shape[:-1])
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lu_solve(x_f, i_p, v_b)
    def test_rlu_solve_returns_expected_shapes(self, arrays):
        m_ss, m_sb, m_bb, m_bs = arrays
        hy.assume(hn.nonsquare(m_sb))
        hy.assume(hn.all_well_behaved(m_bb))

        _, x_f, i_p = gfl.rsolve_lu(m_sb, m_bb)
        expect = utn.array_return_shape('(a,b),(b,b)->(a,b)', m_sb, x_f)
        self.assertArrayShape(gfl.rlu_solve(m_sb, x_f, i_p), expect)
        expect = utn.array_return_shape('(a,a),(a,b)->(a,b)', x_f, m_bs)
        self.assertArrayShape(gfl.lu_solve(x_f, i_p, m_bs), expect)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.rlu_solve(m_ss, x_f, i_p)
        with self.assertRaisesRegex(*utn.core_dim_err):
            gfl.lu_solve(x_f, i_p, m_sb)
        _, x_f, i_p = gfl.rsolve_lu(m_sb, m_bb)
        with self.assertRaisesRegex(*utn.broadcast_err):
            gfl.rlu_solve(*utn.make_bad_broadcast(m_sb, x_f), i_p)
        x_f, i_p = unbroadcast_factors(m_bb, x_f, i_p)
        expect = utn.array_return_shape('(a,a),(a,b)->(a,b)', m_bb, m_bs)
        self.assertArrayShape(gfl.lu_solve(x_f, i_p, m_bs), expect)
    def test_pinvarray_shape_methods(self, array):
        m_bs = array.view(la.lnarray)
        hy.assume(hn.nonsquare(m_bs))
        hy.assume(hn.all_well_behaved(m_bs))
        m_bs_p = m_bs.pinv
        expect = utn.trnsp(m_bs.shape)

        self.assertEqual(m_bs_p.ndim, len(expect))
        self.assertEqual(m_bs_p.shape, expect)
        self.assertEqual(m_bs_p.size, np.prod(expect))
        self.assertArrayShape(m_bs_p(), expect)
        with self.assertRaises(ValueError):
            m_bs.inv  # pylint: disable=pointless-statement
        m_bs_p = m_bs.c.pinv
        expect = insert(m_bs.shape)
        now_expect = expect[1::-1] + expect[2:]
        self.assertArrayShape(m_bs_p.swapaxes(0, 1), now_expect)
        now_expect = expect[2::-1] + expect[3:]
        self.assertArrayShape(m_bs_p.swapaxes(0, 2), now_expect)
        now_expect = utn.trnsp(expect)
        self.assertArrayShape(m_bs_p.swapaxes(-1, -2), now_expect)