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))))
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
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
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
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.]]>")
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.]]>>")
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])
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))
def construct(diag, left, right, middle): return Diagonal(diag) + LowRank(left, right, middle)
def construct(left, right, middle): return LowRank(left, right, middle)
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
def lr_pd(request): code_l, code_m = request.param return LowRank(generate(code_l), middle=generate(code_m))
def lr2(request): code_lr, code_m = request.param return LowRank(generate(code_lr), generate(code_lr), middle=generate(code_m))
def __call__(self, x, y): if x is y: return LowRank(uprank(x)) else: return LowRank(left=uprank(x), right=uprank(y))