import torch


def householder_matrix(v, size=None):
    """
    householder_matrix(Tensor, size=None) -> Tensor

    Arguments
        v: Tensor of size [Any,]
        size: `int` or `None`. The size of the resulting matrix.
            size >= v.size(0)
    Output
        I - 2 v^T * v / v*v^T: Tensor of size [size, size]
    """
    size = size or v.size(0)
    assert size >= v.size(0)
    v = torch.cat([torch.ones(size - v.size(0), device=v.device), v])
    I = torch.eye(size, device=v.device)
    outer = torch.ger(v, v)
    inner = torch.dot(v, v) + 1e-16
    return I - 2 * outer / inner