Skip to content
DeepForgeby svx
All problems
MediumCalculusca-237

Trace AXB Gradient

Problem

For the scalar function f(X) = trace(A X B) with constant 2x2 matrices A and B, the gradient with respect to X is A^T B^T.

Given the entries of A (a, b, c, d) and B (e, f, g, h), return the 2x2 gradient as a nested list.

Hint

Write the trace as a sum over indices and differentiate entry by entry.

Test cases

case 1
in: [1,0,0,1,1,0,0,1]
expected: [[1,0],[0,1]]
case 2
in: [1,2,3,4,5,6,7,8]
expected: [[23,31],[34,46]]
case 3
in: [0,0,0,0,1,2,3,4]
expected: [[0,0],[0,0]]
case 4
in: [1,0,0,2,0,1,1,0]
expected: [[0,1],[2,0]]
Starter codePython
def trace_axb_gradient(a, b, c, d, e, f, g, h):
    # Return the 2x2 gradient
    # Your code here
    pass

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