Implement Bilinear Form with Einsum
Problem Statement
Use einsum to compute a batched bilinear form x^T A y.
Background
A bilinear form computes x^T A y where x and y are vectors and A is a matrix. With einsum, you can express this contraction over multiple indices in a single call.
Your Task
The starter code creates x (3x4), A (4x4), and y (3x4). Use einsum to compute the batched bilinear form x^T A y, producing one scalar per batch element.
Output Format
Returns a dictionary with "results" (one scalar per batch), "shape", and "first_result".
Example
None
{'results': [1.0, 6.0, 136.0], 'shape': [3], 'first_result': 1.0}- Inputs: and are batches of three 4-dimensional vectors (row is or ), and is a single matrix with entries to , shared by every batch element.
- Math: for each , the bilinear form is . Both and are summed away; only survives.
- : (the first unit vector), so only the term survives, giving .
- : picks out , giving .
- : all-ones vectors keep every term, so the result is the sum of all entries of : row sums .
- Output: one scalar per batch element, so
resultsis[1.0, 6.0, 136.0],shapeis[3], andfirst_resultis1.0. Notice that with unit vectors simply reads entry of .
Constraints
- Use 'bi,ij,bj->b' einsum notation
- Batched computation
- Fixed input values for determinism