def grad_likelihood(*X, Y=0, W=1):
    """Gradient of the log-likelihood of NMF assuming Gaussian error model.

    Args:
        X:  tuple of (A,S) matrix factors
        Y:  target matrix
        W: (optional weight matrix MxN)

    Returns:
        grad_A f, grad_S f
    """
    A, S = X
    D = W * (A.dot(S) - Y)
    return D.dot(S.T), A.T.dot(D)