HILBERT
Constructs an n-by-n Hilbert matrix. A Hilbert matrix is a square matrix with entries H_{i,j} = \frac{1}{i+j-1}. Hilbert matrices are notorious for being highly ill-conditioned.
Excel Usage
=HILBERT(n)
n(int, required): The order of the matrix.
Returns (list[list]): 2D array representing the Hilbert matrix.
Example 1: Hilbert matrix of order 3
Inputs:
| n |
|---|
| 3 |
Excel formula:
=HILBERT(3)
Expected output:
| Result | ||
|---|---|---|
| 1 | 0.5 | 0.333333 |
| 0.5 | 0.333333 | 0.25 |
| 0.333333 | 0.25 | 0.2 |
Example 2: Hilbert matrix of order 1
Inputs:
| n |
|---|
| 1 |
Excel formula:
=HILBERT(1)
Expected output:
1
Example 3: Hilbert matrix of order 4
Inputs:
| n |
|---|
| 4 |
Excel formula:
=HILBERT(4)
Expected output:
| Result | |||
|---|---|---|---|
| 1 | 0.5 | 0.333333 | 0.25 |
| 0.5 | 0.333333 | 0.25 | 0.2 |
| 0.333333 | 0.25 | 0.2 | 0.166667 |
| 0.25 | 0.2 | 0.166667 | 0.142857 |
Example 4: Hilbert matrix from single-cell range input
Inputs:
| n |
|---|
| 5 |
Excel formula:
=HILBERT({5})
Expected output:
| Result | ||||
|---|---|---|---|---|
| 1 | 0.5 | 0.333333 | 0.25 | 0.2 |
| 0.5 | 0.333333 | 0.25 | 0.2 | 0.166667 |
| 0.333333 | 0.25 | 0.2 | 0.166667 | 0.142857 |
| 0.25 | 0.2 | 0.166667 | 0.142857 | 0.125 |
| 0.2 | 0.166667 | 0.142857 | 0.125 | 0.111111 |
Python Code
import numpy as np
from scipy.linalg import hilbert as scipy_hilbert
def hilbert(n):
"""
Construct a Hilbert matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.hilbert.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): The order of the matrix.
Returns:
list[list]: 2D array representing the Hilbert matrix.
"""
try:
if isinstance(n, list):
if not n or not isinstance(n[0], list) or not n[0]:
return "Error: n must be a scalar or single-cell range"
n = n[0][0]
try:
n_val = int(n)
except (TypeError, ValueError):
return "Error: n must be an integer"
if n_val <= 0:
return "Error: n must be a positive integer"
res = scipy_hilbert(n_val)
return res.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
The order of the matrix.