Variational algorithms for linear algebra