Skip to content
DeepForgeby svx
All problems
MediumCalculusca-328

Elementwise Square Jacobian

Problem

For the elementwise square y_i = x_i^2, the Jacobian is the diagonal matrix with entries 2 x_i.

Given the vector x, return the n x n Jacobian as nested rows.

Hint

Each output only depends on the matching input, so off-diagonal entries vanish.

Test cases

case 1
in: [[1,2,3]]
expected: [[2,0,0],[0,4,0],[0,0,6]]
case 2
in: [[0,-1]]
expected: [[0,0],[0,-2]]
case 3
in: [[2]]
expected: [[4]]
Starter codePython
def elementwise_square_jacobian(x):
    # Your code here
    pass

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