generic conjugate gradient implementation