
- 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.invpascal() Function
The linalg.invpascal() function in SciPy is used to compute the inverse of the Pascal Matrix, a matrix containing binomial coefficients which follows a specific pattern derived from Pascal's triangle and are arranged in a matrix format.
The function enables efficient solutions to various mathematical problems within the domains of linear algebra and combinatorics. Additionally, it provides a direct and efficient way to obtain the inverse, ditching the need for manual implementation of more matrix inversion algorithms.
Syntax
The following is the syntax of the Scipy linalg.invpascal function −
scipy.linalg.invpascal(n, kind=âsymmetricâ, exact=True)
Parameters
The following is the list of parameters that are accepted by the scipy linalg.invpascal() function −
- n − This parameter takes an integer data type, which represents the size of the matrix to be created. Thus, the result matrix is the size of n*n.
- kind: string (Optional) − This parameter takes a string data type, which represents the type of the matrix. The type of the matrix can be from any of these − 'symmetric', 'lower', and 'upper'.
- exact : bool (Optional) − It exact is True, the output is either an object array of Python long integers (if n <=35) or an array of numpy.uint64 data types. If exact is False, the coefficients in the matrix are computed using scipy.special.comb. This approach is significantly faster than exact = True, but the output will be a floating point array with values that are not the exact coefficients.
Return
The scipy linalg.invpascal() function takes the above parameters and returns a matrix which is the inverse of the n x n Pascal matrix.
Example 1
In the following code, we are passing only one parameter that is 'n' to the linalg.invpascal() function. Here the parameter is passed as a positive integer.
from scipy.linalg import invpascal res=invpascal(5) print('The inverse pascal matrix is:') print(res)
The above code returns the inversal pascal matrix with n x n as its size as shown below −
The inverse pascal matrix is: [[ 5 -10 10 -5 1] [-10 30 -35 19 -4] [ 10 -35 46 -27 6] [ -5 19 -27 17 -4] [ 1 -4 6 -4 1]]
Example 2
In the following example, we pass two parameters which are 'n' and 'kind' to the SciPy linalg.inpascal() function which returns the inverse of Pascal matrix. Here, we pass 'n' as a positive integer and 'kind' as symmetric.
from scipy.linalg import invpascal res=invpascal(5,kind= 'symmetric') print('The inverse pascal matrix is:') print(res)
The output for the above code is as follows −
The inverse pascal matrix is: [[ 5 -10 10 -5 1] [-10 30 -35 19 -4] [ 10 -35 46 -27 6] [ -5 19 -27 17 -4] [ 1 -4 6 -4 1]]
Example 3
For the following example, we pass two parameters, which are 'n' and 'kind' to the scipy.linalg.invpascal() function which returns the inverse of Pascal matrix. In this case, we pass the 'n' parameter as a positive integer and 'kind' as lower.
from scipy.linalg import invpascal res=invpascal(5,kind= 'upper') print('The inverse pascal matrix is:') print(res)
The output of the above code is as follows −
The inverse pascal matrix is: [[ 1 -1 1 -1 1] [ 0 1 -2 3 -4] [ 0 0 1 -3 6] [ 0 0 0 1 -4] [ 0 0 0 0 1]]
Example 4
In the provided example code, we call the scipy.linalg.invpascal() function, which takes two parameters : n and kind. While the 'n' parameter defines the size of the Pascal matrix and is set to positive integer. The 'kind' parameter is set to upper, which indicates to return an inverse matrix to be represented in upper-triangular form.
from scipy.linalg import invpascal res=invpascal(5,kind= 'upper') print('The inverse pascal matrix is:') print(res)
The output of the above code is as follows −
The inverse pascal matrix is: [[ 1 -1 1 -1 1] [ 0 1 -2 3 -4] [ 0 0 1 -3 6] [ 0 0 0 1 -4] [ 0 0 0 0 1]]
Example 5
In the provided example code, we use the linalg.invpascal() function, which accepts two parameters : n and exact. In this case, we pass 'n' as a positive number and set exact as False, which indicates that the function should perform calculations using floating-point arithmetic.
from scipy.linalg import invpascal res=pascal(5, exact=False) print('The pascal matrix is:') print(res)
Output for the above code is −
The pascal matrix is: [[ 1. 1. 1. 1. 1.] [ 1. 2. 3. 4. 5.] [ 1. 3. 6. 10. 15.] [ 1. 4. 10. 20. 35.] [ 1. 5. 15. 35. 70.]]
Example 6
In this example, we will pass only the 'n' parameter by setting it as a negative integer and see the result for the function linalg.invpascal.
from scipy.linalg import pascal res=pascal(-5) print('The inverse pascal matrix is:') print(res)
The above code likely produces an error output as trying to create a Pascal matrix with a negative dimension results in an error as shown below −
ValueError Traceback (most recent call last) <ipython-input-21-b0d98492ae66> in <module> 1 from scipy.linalg import pascal ----> 2 res=pascal(-5) 3 print('The inverse pascal matrix is:') 4 print(res) ~\Desktop\lib\site-packages\scipy\linalg\special_matrices.py in pascal(n, kind, exact) 843 L_n.fill(0) 844 else: --> 845 L_n = np.zeros((n, n), dtype=np.uint64) 846 for i in range(n): 847 for j in range(i + 1): ValueError: negative dimensions are not allowed