Gram Matrix (AᵀA)

Easy
~12 min
code completion

The Gram Matrix

The Gram matrix of a matrix A is defined as:

This appears throughout ML:

  • In the normal equation for linear regression:
  • In kernel methods and covariance matrices
  • In PCA (related to the covariance matrix)
  • In NumPy:

    G = A.T @ A

    Your task:

    Implement gram_matrix(A) that computes .

    Example Tests

    Identity input gives identity output

    Input: {"A":[[1,0],[0,1]]}

    Expected: [[1,0],[0,1]]

    3x2 matrix gives 2x2 Gram matrix

    Input: {"A":[[1,2],[3,4],[5,6]]}

    Expected: [[35,44],[44,56]]

    Result is always square (n x n)

    Input: {"A":[[1,2,3],[4,5,6]]}

    Expected: [3,3]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.