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 &colon; 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&colon;')
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&colon;
[[  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&colon;')
print(res)

The output for the above code is as follows −

The inverse pascal matrix is&colon;
[[  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&colon;')
print(res)

The output of the above code is as follows −

The inverse pascal matrix is&colon;
[[ 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 &colon; 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&colon;')
print(res)

The output of the above code is as follows −

The inverse pascal matrix is&colon;
[[ 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 &colon; 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&colon;')
print(res)

Output for the above code is −

The pascal matrix is&colon;
[[ 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&colon;')
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&colon;')
      4 print(res)

~\Desktop\lib\site-packages\scipy\linalg\special_matrices.py in pascal(n, kind, exact)
    843             L_n.fill(0)
    844         else&colon;
--> 845             L_n = np.zeros((n, n), dtype=np.uint64)
    846         for i in range(n)&colon;
    847             for j in range(i + 1)&colon;

ValueError&colon; negative dimensions are not allowed
Advertisements