implement the complex arithmetic-geometric mean (code is still messy)