trace & logdet optimizing function