optimized code with numpy