Skip to content
DeepForgeby svx
All problems
MediumOptimizationop-285

Projection Onto a Box

Problem

The Euclidean projection of a point onto the box [lo, hi] clips each coordinate to the interval.

Given the point and the lower and upper bounds (lo <= hi), return the projected point.

Hint

Clamp each coordinate independently.

Test cases

case 1
in: [[2,-1,5],0,3]
expected: [2,0,3]
case 2
in: [[0.5,0.5],0.4,0.6]
expected: [0.5,0.5]
case 3
in: [[-2,-2],-1,1]
expected: [-1,-1]
Starter codePython
def projection_onto_box(x, lo, hi):
    # Your code here
    pass

3 test cases · run your code against them in the editor