Tensor Contraction with Einsum
Problem Statement
Use einsum for advanced tensor contractions: Frobenius inner product, row-wise dot products, and column sums.
Background
Indices that appear on the left but not the right are summed over. By controlling which indices appear in the output, you can express a variety of reductions and contractions.
Your Task
The starter code creates matrices A and B. Use einsum to compute three operations: the Frobenius inner product (element-wise multiply and sum all), row-wise dot products (sum along columns only), and column sums of A.
Output Format
Returns a dictionary with "frobenius", "row_dots", and "col_sums".
Example
None
{'frobenius': 70.0, 'row_dots': [17.0, 53.0], 'col_sums': [4.0, 6.0]}- Inputs: the function builds two fixed float matrices, and . Index picks the row and the column, so and sit in the same cell.
- Frobenius inner product: multiply matching cells and sum over both indices, . No index survives, so
frobeniusis a single scalar,70.0. - Row-wise dot products: keep and sum over only, . Row 0 gives , row 1 gives , so
row_dotsis[17.0, 53.0], one value per row. - Column sums of A: only is involved; keep and sum over , , giving and , so
col_sumsis[4.0, 6.0]. - Notice that the row dots add up to the Frobenius value (): summing out afterwards finishes the full contraction. The indices you keep decide the output's shape.
Constraints
- 'ij,ij->' for Frobenius inner product
- 'ij,ij->i' for row-wise dots
- 'ij->j' for column sums