Exemplo n.º 1
0
    def test__solve_triangular_banded(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            chol_bm = gen_chol_factor_BandMat(size, transposed=False)
            chol_data = chol_bm.data
            depth = chol_bm.l + chol_bm.u
            lower = (chol_bm.u == 0)
            if size > 0 and rand_bool() and rand_bool():
                badFrame = randint(size)
                chol_data[0 if lower else depth, badFrame] = 0.0
            else:
                badFrame = None
            transposed = rand_bool()
            overwrite_b = rand_bool()
            chol_full = chol_bm.full()

            b_arg = b.copy()
            if badFrame is not None:
                msg = (
                    'singular matrix: resolution failed at diagonal %d' %
                    badFrame
                )
                msgRe = '^' + re.escape(msg) + '$'
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    bla._solve_triangular_banded(
                        chol_data, b_arg, transposed=transposed, lower=lower,
                        overwrite_b=overwrite_b
                    )
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
            else:
                x = bla._solve_triangular_banded(
                    chol_data, b_arg, transposed=transposed, lower=lower,
                    overwrite_b=overwrite_b
                )
                if transposed:
                    assert_allclose(bm.dot_mv(chol_bm.T, x), b)
                else:
                    assert_allclose(bm.dot_mv(chol_bm, x), b)
                if size == 0:
                    x_good = np.zeros((size,))
                else:
                    x_good = sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
                assert_allclose(x, x_good)
                assert not np.may_share_memory(x, chol_data)
                if size > 0:
                    self.assertEquals(
                        np.may_share_memory(x, b_arg),
                        overwrite_b
                    )

            if not overwrite_b:
                assert np.all(b_arg == b)
Exemplo n.º 2
0
    def test__solve_triangular_banded(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            chol_bm = gen_chol_factor_BandMat(size, transposed=False)
            chol_data = chol_bm.data
            depth = chol_bm.l + chol_bm.u
            lower = (chol_bm.u == 0)
            if size > 0 and rand_bool() and rand_bool():
                badFrame = randint(size)
                chol_data[0 if lower else depth, badFrame] = 0.0
            else:
                badFrame = None
            transposed = rand_bool()
            overwrite_b = rand_bool()
            chol_full = chol_bm.full()

            b_arg = b.copy()
            if badFrame is not None:
                msg = (
                    'singular matrix: resolution failed at diagonal %d' %
                    badFrame
                )
                msgRe = '^' + re.escape(msg) + '$'
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    bla._solve_triangular_banded(
                        chol_data, b_arg, transposed=transposed, lower=lower,
                        overwrite_b=overwrite_b
                    )
                with self.assertRaisesRegexp(la.LinAlgError, msgRe):
                    sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
            else:
                x = bla._solve_triangular_banded(
                    chol_data, b_arg, transposed=transposed, lower=lower,
                    overwrite_b=overwrite_b
                )
                if transposed:
                    assert_allclose(bm.dot_mv(chol_bm.T, x), b)
                else:
                    assert_allclose(bm.dot_mv(chol_bm, x), b)
                if size == 0:
                    x_good = np.zeros((size,))
                else:
                    x_good = sla.solve_triangular(
                        chol_full, b, trans=transposed, lower=lower
                    )
                assert_allclose(x, x_good)
                assert not np.may_share_memory(x, chol_data)
                if size > 0:
                    self.assertEquals(
                        np.may_share_memory(x, b_arg),
                        overwrite_b
                    )

            if not overwrite_b:
                assert np.all(b_arg == b)
Exemplo n.º 3
0
def smooth(a, Phat, N, lambda_1, lambda_2):
    """
    a: (N,) number of measurements at that timestep
    Phat: (N, 3) sum of measurements at that timestep
    N: num time steps
    lambda_1, lambda_2: regularization parameters

    solves the optimization problem (over P \in R^{Tx3}):
    minimize ||diag(a)*P-Phat||^2 + lambda_1/N*||D_2*P||^2 + lambda_2/N*||D_3*P||^2

    returns:
        - P: (N, 3) matrix with full trajectory
    """
    # A in Banded Matrix form
    A = bm.diag(1. * a)

    # D_2 and D_3 in Banded Matrix form transposed
    D_2_bm_T = bm.BandMat(
        1, 1,
        np.hstack([
            np.zeros((3, 1)),
            np.repeat([[1.], [-2.], [1.]], N - 2, axis=1),
            np.zeros((3, 1))
        ]))
    D_3_bm_T = bm.BandMat(
        2, 2,
        np.hstack([
            np.zeros((5, 2)),
            np.repeat([[-1.], [2.], [0.], [-2.], [1.]], N - 4, axis=1),
            np.zeros((5, 2))
        ]))

    # XP=B normal equations
    X = bm.dot_mm(A, A) + lambda_1 / N * bm.dot_mm(
        D_2_bm_T, D_2_bm_T.T) + lambda_2 / N * bm.dot_mm(D_3_bm_T, D_3_bm_T.T)
    l_and_u = (X.l, X.u)  # lower and upper band bounds
    B = np.hstack([
        np.expand_dims(bm.dot_mv(A, Phat[:, 0]), -1),
        np.expand_dims(bm.dot_mv(A, Phat[:, 1]), -1),
        np.expand_dims(bm.dot_mv(A, Phat[:, 2]), -1)
    ])

    # solve normal equations
    P = solve_banded(l_and_u, X.data, B)

    return P
Exemplo n.º 4
0
    def test_cho_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            chol_bm = gen_chol_factor_BandMat(size)
            depth = chol_bm.l + chol_bm.u
            lower = (chol_bm.u == 0)
            chol_lower_bm = chol_bm if lower else chol_bm.T
            chol_full = chol_bm.full()

            x = bla.cho_solve(chol_bm, b)
            assert_allclose(
                bm.dot_mv(chol_lower_bm, bm.dot_mv(chol_lower_bm.T, x)), b)
            if size == 0:
                x_good = np.zeros((size, ))
            else:
                x_good = sla.cho_solve((chol_full, lower), b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, chol_bm.data)
            assert not np.may_share_memory(x, b)
Exemplo n.º 5
0
    def test_dot_mv(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b = randn(size)
            a_full = a_bm.full()

            c = bm.dot_mv(a_bm, b)
            c_good = np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert not np.may_share_memory(c, a_bm.data)
            assert not np.may_share_memory(c, b)
Exemplo n.º 6
0
    def test_dot_mv(self, its=100):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            a_bm = gen_BandMat(size)
            b = randn(size)
            a_full = a_bm.full()

            c = bm.dot_mv(a_bm, b)
            c_good = np.dot(a_full, b)
            assert_allclose(c, c_good)
            assert not np.may_share_memory(c, a_bm.data)
            assert not np.may_share_memory(c, b)
Exemplo n.º 7
0
    def test_cho_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            chol_bm = gen_chol_factor_BandMat(size)
            depth = chol_bm.l + chol_bm.u
            lower = (chol_bm.u == 0)
            chol_lower_bm = chol_bm if lower else chol_bm.T
            chol_full = chol_bm.full()

            x = bla.cho_solve(chol_bm, b)
            assert_allclose(
                bm.dot_mv(chol_lower_bm, bm.dot_mv(chol_lower_bm.T, x)),
                b
            )
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.cho_solve((chol_full, lower), b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, chol_bm.data)
            assert not np.may_share_memory(x, b)
Exemplo n.º 8
0
    def test_solveh(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            a_bm = gen_pos_def_BandMat(size)
            a_full = a_bm.full()

            x = bla.solveh(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b, sym_pos=True)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)
Exemplo n.º 9
0
    def test_solveh(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            a_bm = gen_pos_def_BandMat(size)
            a_full = a_bm.full()

            x = bla.solveh(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b, sym_pos=True)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)
Exemplo n.º 10
0
    def test_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            # the below tries to ensure the matrix is well-conditioned
            a_bm = gen_BandMat(size) + bm.diag(np.ones((size,)) * 10.0)
            a_full = a_bm.full()

            x = bla.solve(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)
Exemplo n.º 11
0
    def test_solve(self, its=50):
        for it in range(its):
            size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
            b = randn(size)
            # the below tries to ensure the matrix is well-conditioned
            a_bm = gen_BandMat(size) + bm.diag(np.ones((size,)) * 10.0)
            a_full = a_bm.full()

            x = bla.solve(a_bm, b)
            assert_allclose(bm.dot_mv(a_bm, x), b)
            if size == 0:
                x_good = np.zeros((size,))
            else:
                x_good = sla.solve(a_full, b)
            assert_allclose(x, x_good)
            assert not np.may_share_memory(x, a_bm.data)
            assert not np.may_share_memory(x, b)