added vectorized impl of gradient calculations