
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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 : 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