SciPy - linalg.invhilbert() Function



The linalg.invhilbert() function in SciPy is used to compute the inverse of the Hilbert matrix of order n.

This function calculates the exact inverse of an n x n Hilbert matrix. It is especially designed to provide a more accurate inverse than a general-purpose matrix inversion function might.

Note : In the inverse of a Hilbert matrix, the entries are all integers. When the parameter n is greater than 14, some of the entries in inverse exceed the upper limit of 64 bit integers. Hence, the exact option allows to deal with large integers.

Syntax

The following is the syntax of the Scipy linalg.invhilbert function

scipy.linalg.invhilbert(n, exact=False)

Parameters

The following is the list of parameters that are accepted by the scipy linalg.invhilbert() function −

  • n − This parameter takes an integer data type as an input which represents the size of the array to be created.
  • exact : bool − This parameter controls how the inverse of the Hilbert matrix is calculated, trading off speed and memory for accuracy.
    • If exact = True the inverse is exactly calculated using rational numbers instead of regular decimal numbers. It uses the Python's Fraction object.
    • If exact = False the inverse is calculated using standard floating-point arithematic.

Return

The scipy linalg.invhilbert() function takes the above parameter and returns an array. The data type of the array is np.float64 if exact is false. And the data type is either np.int64 (for n<=14) or object (for n>14) when exact is true. The objects in the array are usually long integers.

Example 1

In the following example, we pass a parameter 'n' to the scipy linalg.invhilbert() function. In this case, we pass 'n' as a positive integer.

from scipy.linalg import invhilbert
x=invhilbert(5)
print('The inverse Hilbert matrix is:')
print(x)

The output for the above code is −

The inverse Hilbert matrix is:
[[ 2.500e+01 -3.000e+02  1.050e+03 -1.400e+03  6.300e+02]
 [-3.000e+02  4.800e+03 -1.890e+04  2.688e+04 -1.260e+04]
 [ 1.050e+03 -1.890e+04  7.938e+04 -1.176e+05  5.670e+04]
 [-1.400e+03  2.688e+04 -1.176e+05  1.792e+05 -8.820e+04]
 [ 6.300e+02 -1.260e+04  5.670e+04 -8.820e+04  4.410e+04]]

Output

In this example, the scipy.linalg.invhilbert() function is used and the parameter 'n' is passed with a positive integer greater than 14.

from scipy.linalg import invhilbert
x=invhilbert(18)[8,7]
print('The inverse Hilbert matrix is:')
print(x)

The output for the above code is −

The inverse Hilbert matrix is:
2.749039453248628e+22 

Note &colon; Since the exact parameter is not mentioned the function by default will use floating-point calculation.

Example 3

Below is the example of a case using scipy.linalg.invhilbert() function where the parameters 'n' is set greater than 14 and 'exact' is set true.

from scipy.linalg import invhilbert
x=invhilbert(18, exact=True)[8,8]
print('The inverse Hilbert matrix is:')
print(x)

The output for the above code is −

The inverse Hilbert matrix is:
27490394532486278250000

Example 4

In this case the function scipy.linalg.invhilbert is used by passing the 'n' parameter as negative number.

from scipy.linalg import invhilbert
x=invhilbert(-5)
print('The inverse Hilbert matrix is:')
print(x)

The output for the above code is liked an error because the invhilbert function required a positive integer as input, representing the order(size) of the Hilbert matrix. Providing a negative integer causes valueerror as given below −

ValueError                                Traceback (most recent call last)
Cell In[8], line 2
      1 from scipy.linalg import invhilbert
----> 2 x=invhilbert(-5)
      3 print('The inverse Hilbert matrix is:')
      4 print(x)

File /lib/python3.12/site-packages/scipy/linalg/_special_matrices.py:648, in invhilbert(n, exact)
    646 else:
    647     dtype = np.float64
--> 648 invh = np.empty((n, n), dtype=dtype)
    649 for i in range(n):
    650     for j in range(0, i + 1):

ValueError: negative dimensions are not allowed
Advertisements