All problems
Bilinear Form Partial Gradients
Problem
For the scalar function f(p, q) = p^T A q with constant matrix A, the gradient with respect to p is A q and with respect to q is A^T p.
Given the vectors p and q and the matrix A as nested rows, return [A q, A^T p].
Hint
Treat A as constant and differentiate the scalar product in each vector.
Test cases
case 1
in: [[1,2],[3,4],[[1,0],[0,1]]]
expected: [[3,4],[1,2]]
case 2
in: [[1,0],[1,1],[[2,3],[4,5]]]
expected: [[5,9],[2,3]]
case 3
in: [[2],[1,-1],[[1,2]]]
expected: [[-1],[2,4]]
Starter codePython
def bilinear_form_partial_gradients(p, q, a):
# Your code here
pass3 test cases · run your code against them in the editor