optimized sum function and FastMath for trig