# core.LowRankPlusDiag

``` python
core.LowRankPlusDiag(factor, evals, diag)
```

A diagonal plus a low-rank term, $P = D + U \operatorname{diag}(s) U^\top$.

Here $D = \operatorname{diag}(d)$. Products and the diagonal work for any $d$; `solve`, `inverse_diagonal`, `logdet` and `cov_sqrt_mv` need $d > 0$, which a prior gives. They use the Woodbury identity on a thin QR of the scaled factor and a second QR of a $2r \times r$ matrix, so each costs $O(n r^2)$; the dense matrix is never formed. With $B = D^{-1/2} U \operatorname{diag}(s)^{1/2}$, the precision is $P = D^{1/2} (I + B B^\top) D^{1/2}$. The thin QR $B = Q R$ and the thin QR $[R^\top; I] = Q_2 T$ give $T^\top T = I + R R^\top$, so

$$P^{-1} = D^{-1/2} \big(I - Q (I - (T^\top T)^{-1}) Q^\top\big) D^{-1/2}, \qquad
\log \det P = \sum_i \log d_i + 2 \sum_j \log |T_{jj}|.$$

Through those four, an `evals` entry that is exactly zero gets no gradient (its column is dropped), and gradients may be NaN when the scaled factor has exactly dependent columns (the derivative of QR). Repeated singular values are fine.

## Parameters

| Name   | Type   | Description                             | Default    |
|--------|--------|-----------------------------------------|------------|
| factor | Tensor | $U$, `[n, r]`.                          | *required* |
| evals  | Tensor | $s$, `[r]`, non-negative.               | *required* |
| diag   | Tensor | $d$, `[n]`; zeros for a bare curvature. | *required* |

## Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `factor` is not 2-D, or `evals` or `diag` has the wrong shape for it. |

## Methods

| Name | Description |
|----|----|
| [add_diagonal](#laplacekit.core.LowRankPlusDiag.add_diagonal) | $P + \operatorname{diag}(d')$: the argument $d'$ added to $d$. |
| [add_isotropic](#laplacekit.core.LowRankPlusDiag.add_isotropic) | $P + \lambda I$: $\lambda$ added to $d$. |
| [add_same](#laplacekit.core.LowRankPlusDiag.add_same) | The exact sum: the factors and evals concatenated, the diagonals summed. |
| [cov_sqrt_mv](#laplacekit.core.LowRankPlusDiag.cov_sqrt_mv) | $L v$ with $L = D^{-1/2} (I - Q Q^\top + Q T^{-1} Q^\top)$. |
| [diagonal](#laplacekit.core.LowRankPlusDiag.diagonal) | $d + (U \odot U) s$. |
| [inverse_diagonal](#laplacekit.core.LowRankPlusDiag.inverse_diagonal) | The diagonal of $P^{-1}$ from the Woodbury factors, without forming $P^{-1}$. |
| [logdet](#laplacekit.core.LowRankPlusDiag.logdet) | $\sum_i \log d_i + 2 \sum_j \log \|T_{jj}\|$. |
| [mv](#laplacekit.core.LowRankPlusDiag.mv) | $D v + U \operatorname{diag}(s) U^\top v$. |
| [scaled](#laplacekit.core.LowRankPlusDiag.scaled) | $s P$: $s d$ and $s$ times `evals`. |
| [solve](#laplacekit.core.LowRankPlusDiag.solve) | $P^{-1} v$ by the Woodbury identity, with two triangular solves in $T$. |
| [truncated](#laplacekit.core.LowRankPlusDiag.truncated) | The same `diag`, with the low-rank part cut to its top `rank` eigenpairs. |

### add_diagonal

``` python
core.LowRankPlusDiag.add_diagonal(d, /)
```

$P + \operatorname{diag}(d')$: the argument $d'$ added to $d$.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| d | Tensor | The diagonal $d$, shaped like a vector of the operator. | *required* |

#### Returns

| Name | Type | Description                   |
|------|------|-------------------------------|
|      | Self | An operator of the same type. |

### add_isotropic

``` python
core.LowRankPlusDiag.add_isotropic(precision, /)
```

$P + \lambda I$: $\lambda$ added to $d$.

#### Parameters

| Name      | Type   | Description                              | Default    |
|-----------|--------|------------------------------------------|------------|
| precision | Tensor | The precision $\lambda$, a 0-dim tensor. | *required* |

#### Returns

| Name | Type | Description                   |
|------|------|-------------------------------|
|      | Self | An operator of the same type. |

### add_same

``` python
core.LowRankPlusDiag.add_same(other, /)
```

The exact sum: the factors and evals concatenated, the diagonals summed.

The width grows with each sum, as for a `LowRankGGN` posterior used as the prior of a `LowRankGGN` fit; `lk.priors.LowRankSum` truncates it back to a fixed rank instead.

#### Parameters

| Name  | Type               | Description        | Default    |
|-------|--------------------|--------------------|------------|
| other | Operator\[Tensor\] | The operator $P'$. | *required* |

#### Returns

| Name | Type | Description                   |
|------|------|-------------------------------|
|      | Self | An operator of the same type. |

#### Raises

| Name | Type | Description |
|----|----|----|
|  | StructureError | `other` is not a `LowRankPlusDiag`, or its size $n$ differs. |

### cov_sqrt_mv

``` python
core.LowRankPlusDiag.cov_sqrt_mv(v, /)
```

$L v$ with $L = D^{-1/2} (I - Q Q^\top + Q T^{-1} Q^\top)$.

#### Parameters

| Name | Type   | Description          | Default    |
|------|--------|----------------------|------------|
| v    | Tensor | A vector `[..., P]`. | *required* |

#### Returns

| Name | Type   | Description             |
|------|--------|-------------------------|
|      | Tensor | $L v$, shaped like `v`. |

### diagonal

``` python
core.LowRankPlusDiag.diagonal()
```

$d + (U \odot U) s$.

#### Returns

| Name | Type   | Description     |
|------|--------|-----------------|
|      | Tensor | A vector `[P]`. |

### inverse_diagonal

``` python
core.LowRankPlusDiag.inverse_diagonal()
```

The diagonal of $P^{-1}$ from the Woodbury factors, without forming $P^{-1}$.

Entry $i$ is $\big(1 - \lVert Q_{i,:} \rVert^2 + \lVert M_{i,:} \rVert^2\big) / d_i$, with $M = Q T^{-1}$.

#### Returns

| Name | Type   | Description     |
|------|--------|-----------------|
|      | Tensor | A vector `[P]`. |

### logdet

``` python
core.LowRankPlusDiag.logdet()
```

$\sum_i \log d_i + 2 \sum_j \log |T_{jj}|$.

#### Returns

| Name | Type   | Description     |
|------|--------|-----------------|
|      | Tensor | A 0-dim tensor. |

### mv

``` python
core.LowRankPlusDiag.mv(v, /)
```

$D v + U \operatorname{diag}(s) U^\top v$.

#### Parameters

| Name | Type   | Description          | Default    |
|------|--------|----------------------|------------|
| v    | Tensor | A vector `[..., P]`. | *required* |

#### Returns

| Name | Type   | Description             |
|------|--------|-------------------------|
|      | Tensor | $P v$, shaped like `v`. |

### scaled

``` python
core.LowRankPlusDiag.scaled(s, /)
```

$s P$: $s d$ and $s$ times `evals`.

#### Parameters

| Name | Type   | Description     | Default    |
|------|--------|-----------------|------------|
| s    | Tensor | A 0-dim factor. | *required* |

#### Returns

| Name | Type | Description                   |
|------|------|-------------------------------|
|      | Self | An operator of the same type. |

### solve

``` python
core.LowRankPlusDiag.solve(v, /)
```

$P^{-1} v$ by the Woodbury identity, with two triangular solves in $T$.

#### Parameters

| Name | Type   | Description          | Default    |
|------|--------|----------------------|------------|
| v    | Tensor | A vector `[..., P]`. | *required* |

#### Returns

| Name | Type   | Description                  |
|------|--------|------------------------------|
|      | Tensor | $P^{-1} v$, shaped like `v`. |

### truncated

``` python
core.LowRankPlusDiag.truncated(rank, /)
```

The same `diag`, with the low-rank part cut to its top `rank` eigenpairs.

The eigenpairs of $U \operatorname{diag}(s) U^\top$ below the top `rank` are dropped, so their precision is lost. Below the width, the columns whose `evals` entry is zero are dropped first, and the top `rank` are taken from the rest. It costs a thin QR $U \operatorname{diag}(s)^{1/2} = Q R$ and an $r \times r$ `eigh` of $R R^\top$.

#### Parameters

| Name | Type | Description                                   | Default    |
|------|------|-----------------------------------------------|------------|
| rank | int  | The number of eigenpairs to keep, at least 1. | *required* |

#### Returns

| Name | Type | Description |
|----|----|----|
|  | Self | The operator unchanged (exact) when `rank` is at or above the width. Otherwise one with `min(rank, nonzero, n)` columns, with `nonzero` the count of nonzero `evals` and `n` the number of rows, so it can be narrower than `rank`. Its eigenvalues are clamped at zero. |

#### Raises

| Name | Type       | Description        |
|------|------------|--------------------|
|      | ValueError | `rank` is below 1. |
