PIXELBANKv9.1.0
Menu

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:

Input:
None
Output:
{'frobenius': 70.0, 'row_dots': [17.0, 53.0], 'col_sums': [4.0, 6.0]}
Reasoning:
  • We start by defining two 2x2 matrices A = [[1, 2], [3, 4]] and B = [[5, 6], [7, 8]].
  • The element-wise product sum (Frobenius inner product) is computed using 'ij,ij->' as (1∗5+2∗6)+(3∗7+4∗8)(1*5 + 2*6) + (3*7 + 4*8) = (5+12)+(21+32)(5 + 12) + (21 + 32) = 17+5317 + 53 = 70.070.0, which is the value for "frobenius".
  • For row-wise dot products ('ij,ij->i'), we calculate [1∗5+2∗6,3∗7+4∗8][1*5 + 2*6, 3*7 + 4*8] = [5+12,21+32][5 + 12, 21 + 32] = [17.0,53.0][17.0, 53.0], resulting in the list for "row_dots".
  • The column sums of A ('ij->j') are calculated as [1+3,2+4][1 + 3, 2 + 4] = [4.0,6.0][4.0, 6.0], giving us the list for "col_sums".

Constraints:

  • 'ij,ij->' for Frobenius inner product
  • 'ij,ij->i' for row-wise dots
  • 'ij->j' for column sums
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.