Finding optimal directions based on sum of squares