Ejemplo n.º 1
0
 def __call__(self, x, y):
     f1, f2 = expand(self.fs)
     if x is y and f1 is f2:
         return LowRank(uprank(f1(uprank(x))))
     else:
         return LowRank(left=uprank(f1(uprank(x))),
                        right=uprank(f2(uprank(y))))
Ejemplo n.º 2
0
def test_dtype_wb_promotion():
    wb = LowRank(B.ones(int, 5, 5)) + Diagonal(B.ones(int, 5))
    assert B.dtype(wb) == np.int64
    wb = LowRank(B.ones(float, 5, 5)) + Diagonal(B.ones(int, 5))
    assert B.dtype(wb) == np.float64
    wb = LowRank(B.ones(int, 5, 5)) + Diagonal(B.ones(float, 5))
    assert B.dtype(wb) == np.float64
Ejemplo n.º 3
0
def test_dtype_lr_promotion():
    lr = LowRank(B.ones(int, 5, 2), B.ones(int, 5, 2), B.ones(int, 2, 2))
    assert B.dtype(lr) == np.int64
    lr = LowRank(B.ones(float, 5, 2), B.ones(int, 5, 2), B.ones(int, 2, 2))
    assert B.dtype(lr) == np.float64
    lr = LowRank(B.ones(int, 5, 2), B.ones(float, 5, 2), B.ones(int, 2, 2))
    assert B.dtype(lr) == np.float64
    lr = LowRank(B.ones(int, 5, 2), B.ones(int, 5, 2), B.ones(float, 2, 2))
    assert B.dtype(lr) == np.float64
Ejemplo n.º 4
0
def test_lowrank_attributes():
    # Check default right and middle factor.
    left = B.ones(3, 2)
    lr = LowRank(left)
    assert lr.left is left
    assert lr.right is left
    approx(lr.middle, B.eye(2))
    assert lr.rank == 2

    # Check given identical right.
    lr = LowRank(left, left)
    assert lr.left is left
    assert lr.right is left
    approx(lr.middle, B.eye(2))
    assert lr.rank == 2

    # Check given identical right and middle.
    middle = B.ones(2, 2)
    lr = LowRank(left, left, middle=middle)
    assert lr.left is left
    assert lr.right is left
    approx(lr.middle, B.ones(2, 2))
    assert lr.rank == 2

    # Check given other right and middle factor.
    right = 2 * B.ones(3, 2)
    lr = LowRank(left, right, middle=middle)
    assert lr.left is left
    assert lr.right is right
    approx(lr.middle, B.ones(2, 2))
    assert lr.rank == 2

    # Check rank in non-square case.
    assert LowRank(B.ones(3, 2), B.ones(3, 2), B.ones(2, 2)).rank == 2
    assert LowRank(B.ones(3, 2), B.ones(3, 1), B.ones(2, 1)).rank == 1
Ejemplo n.º 5
0
def test_lowrank_formatting():
    assert (str(LowRank(B.ones(3, 1), 2 * B.ones(
        3, 1))) == "<low-rank matrix: shape=3x3, dtype=float64, rank=1>")
    assert (repr(LowRank(B.ones(3, 2), 2 * B.ones(
        3, 2))) == "<low-rank matrix: shape=3x3, dtype=float64, rank=2\n"
            " left=[[1. 1.]\n"
            "       [1. 1.]\n"
            "       [1. 1.]]\n"
            " right=[[2. 2.]\n"
            "        [2. 2.]\n"
            "        [2. 2.]]>")
    assert (repr(LowRank(B.ones(
        3, 2))) == "<low-rank matrix: shape=3x3, dtype=float64, rank=2\n"
            " left=[[1. 1.]\n"
            "       [1. 1.]\n"
            "       [1. 1.]]>")
    assert (repr(LowRank(B.ones(3, 2), middle=B.ones(
        2, 2))) == "<low-rank matrix: shape=3x3, dtype=float64, rank=2\n"
            " left=[[1. 1.]\n"
            "       [1. 1.]\n"
            "       [1. 1.]]\n"
            " middle=[[1. 1.]\n"
            "         [1. 1.]]>")
Ejemplo n.º 6
0
def test_woodbury_formatting():
    diag = Diagonal(B.ones(3))
    lr = LowRank(B.ones(3, 1), 2 * B.ones(3, 1))
    assert str(Woodbury(diag,
                        lr)) == "<Woodbury matrix: shape=3x3, dtype=float64>"
    assert (repr(Woodbury(
        diag, lr)) == "<Woodbury matrix: shape=3x3, dtype=float64\n"
            " diag=<diagonal matrix: shape=3x3, dtype=float64\n"
            "       diag=[1. 1. 1.]>\n"
            " lr=<low-rank matrix: shape=3x3, dtype=float64, rank=1\n"
            "     left=[[1.]\n"
            "           [1.]\n"
            "           [1.]]\n"
            "     right=[[2.]\n"
            "            [2.]\n"
            "            [2.]]>>")
Ejemplo n.º 7
0
def compute_K_z(model):
    """Covariance matrix :math:`K_z` of :math:`z_m` for :math:`m=0,\\ldots,2M`.

    Args:
        model (:class:`.gprv.GPRV`): Model.

    Returns:
        matrix: :math:`K_z`.
    """
    # Compute harmonic frequencies.
    m = model.ms - B.cast(model.dtype, model.ms > model.m_max) * model.m_max
    omega = 2 * B.pi * m / (model.b - model.a)

    # Compute the parameters of the kernel matrix.
    lam_t = 1
    alpha = 0.5 * (model.b - model.a) / psd_matern_12(omega, model.lam, lam_t)
    alpha = alpha + alpha * B.cast(model.dtype, model.ms == 0)
    beta = 1 / (lam_t**0.5) * B.cast(model.dtype, model.ms <= model.m_max)

    return Diagonal(alpha) + LowRank(left=beta[:, None])
Ejemplo n.º 8
0
def test_lowrank_shape_checks():
    # Check that matrices must be given.
    with pytest.raises(AssertionError):
        LowRank(B.ones(3))
    with pytest.raises(AssertionError):
        LowRank(B.ones(3, 1), B.ones(3))
    with pytest.raises(AssertionError):
        LowRank(B.ones(3, 1), B.ones(3, 1), B.ones(1))

    # Check that needs need to be compatible
    with pytest.raises(AssertionError):
        LowRank(B.ones(3, 1), B.ones(3, 2))
    with pytest.raises(AssertionError):
        LowRank(B.ones(3, 1), B.ones(3, 2), B.ones(1, 1))
    with pytest.raises(AssertionError):
        LowRank(B.ones(3, 1), middle=B.ones(2, 1))
Ejemplo n.º 9
0
 def construct(diag, left, right, middle):
     return Diagonal(diag) + LowRank(left, right, middle)
Ejemplo n.º 10
0
 def construct(left, right, middle):
     return LowRank(left, right, middle)
Ejemplo n.º 11
0
def test_woodbury_attributes():
    diag = Diagonal(B.ones(3))
    lr = LowRank(B.ones(3, 1), 2 * B.ones(3, 1))
    wb = Woodbury(diag, lr)
    assert wb.diag is diag
    assert wb.lr is lr
Ejemplo n.º 12
0
def lr_pd(request):
    code_l, code_m = request.param
    return LowRank(generate(code_l), middle=generate(code_m))
Ejemplo n.º 13
0
def lr2(request):
    code_lr, code_m = request.param
    return LowRank(generate(code_lr),
                   generate(code_lr),
                   middle=generate(code_m))
Ejemplo n.º 14
0
 def __call__(self, x, y):
     if x is y:
         return LowRank(uprank(x))
     else:
         return LowRank(left=uprank(x), right=uprank(y))