Gram Matrix (AᵀA)

~12 mincode completion

Implement gram_matrix(A) that computes .

Examples

Identity input gives identity output

Input
gram_matrix([[1, 0], [0, 1]])
Output
[[1, 0], [0, 1]]

3x2 matrix gives 2x2 Gram matrix

Input
gram_matrix([[1, 2], [3, 4], [5, 6]])
Output
[[35, 44], [44, 56]]

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

Watch for this: computed A at A T instead.

Requirements

  • A: Array of shape (m, n)

  • Return Symmetric array of shape (n, n).

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 quant funds, 2 defense companies, 2 autonomy companies. Top match scores 93.

Python
import numpy as np

def gram_matrix(A: np.ndarray) -> np.ndarray:
    """
    Compute the Gram matrix G = A.T @ A.

    Args:
        A: Array of shape (m, n)

    Returns:
        Symmetric array of shape (n, n).
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.