added vectorized function of constrain for speed improvements