Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-193

Hilbert Matrix

Problem

Build the n x n Hilbert matrix:

H[i][j] = 1 / (i + j + 1)

The matrix is symmetric positive definite and famously ill-conditioned.

Hint

Use floating-point division so every entry is a float.

Test cases

case 1
in: [1]
expected: [[1]]
case 2
in: [2]
expected: [[1,0.5],[0.5,0.3333333333333333]]
case 3
in: [3]
expected: [[1,0.5,0.3333333333333333],[0.5,0.3333333333333333,0.25],[0.3333333333333333,0.25,0.2]]
case 4
in: [4]
expected: [[1,0.5,0.3333333333333333,0.25],[0.5,0.3333333333333333,0.25,0.2],[0.3333333333333333,0.25,0.2,0.16666666666666666],[0.25,0.2,0.16666666666666666,0.14285714285…
Starter codePython
def hilbert_matrix(n):
    # Your code here
    pass

4 test cases · run your code against them in the editor