import torch


def inv_quad_chol_lower(x,L,y):
    """
        Computes x (L L^T) y
        x : (m x n)
        L : (n x n)
        y : (n x k)
    """
    m = x.shape[0]
    if m == 1:
        xy = torch.cat([x.reshape(-1,1),y],dim=1)
        return torch.triangular_solve(xy,L,upper=False)[0].prod(dim=1).sum(dim=0).reshape(1,1)
    else:
        z1 = torch.triangular_solve(x.transpose(1,0),L,upper=False)[0].transpose(1,0) #m x n
        z2 = torch.triangular_solve(y,L,upper=False)[0] #n x k
        return torch.matmul(z1,z2)