All problems
Quadratic Form Value
Problem
Compute the quadratic form x^T A x for a square matrix A and vector x:
sum over i, j of x[i] * A[i][j] * x[j]
The result is a scalar; A and x may contain negative entries.
Hint
It is equivalent to the dot product of x with A*x.
Test cases
case 1
in: [[[2,1],[1,3]],[1,2]]
expected: 18
case 2
in: [[[1,0],[0,1]],[3,4]]
expected: 25
case 3
in: [[[0,1],[-1,0]],[1,2]]
expected: 0
case 4
in: [[[2,0],[0,5]],[0,0]]
expected: 0
Starter codePython
def quadratic_form(A, x):
# Your code here
pass4 test cases · run your code against them in the editor