Scipy.linalg.solve() Function



The scipy.linalg.solve() function in SciPy accepts two inputs matrices and returns an output array thet represents the solution to the polynomial equation. The two input matrices correspond to the coefficients and constants of the polynomial equations, respectively. Let's take the example to solve the following equations.

Polynomial Equations:

1x+3y+5z=10
2x+5y+z=8
2x+3y+8z=3

The input matrices:

    1 3 5
a = 2 5 1
    2 3 8	
b= [5 8 3]

The output is also a matrix that provides the solution to the equation.

x = [-1.88, 2.36, -0.04]

Syntax

The following is the syntax for the Scipylinalg.solve function.

scipy.linalg.solve(a, b, sym_pos=False, lower=False, overwrite_a=False, overwrite_b=False, debug=None, check_finite=True, assume_a='gen', transposed=False)

Parameters

The parameters for the Scipy linalg() function are listed below −

  • a: This parameter accepts an array as input, representing a square matrix that contains the coefficients of the polynomial equation.

  • b: This parameter accepts an array as input, representing the constants of the equation, i.e., the right hand side of the equation.

  • sym_pos() This parameter accepts a Boolean data type. It assumes a is symmetric and positive definite. This key is deprecated, and the assume_a = pos keyword is recommended instead. The functionality remains the same but will be removed in the future.

  • lower: The parameter accepts a Boolean data type. If set to True, it only considers the data contained in the lower triangle of the input matrix. By default, it uses the upper triangle.

  • overwrite_a: This parameter accepts a Boolean data type that discards data in the input matrix a to improve performance. The default value is False.

  • overwrite_b: This parameter accepts a Boolean data type that discards data in the input matrix b to improve performance. The default value is False.

  • check_finite(): This parameter accepts a Boolean data type and checks whether the input matrix contains only finite numbers. Disabling the parameter improves performance, but if the input matrix contains infinities or NaNs, it can lead to problems such as crashes or non-termination. The default value is True.

  • transposed(): This parameter accepts a Boolean data type. If set to true, it solves the equation $\mathrm{a^Tx\:=\:b}$ for real matrices, but raises a NotImplementedError for complex matrices.

Return Value

The linalg.solve function accepts the above parameters and returns the solution to the equations as an array.

Exception

Following are the exceptions of the linalg.solve() function −

  • ValueError: If the input a is not a squared matrix or if size mismatches are detected.

  • LinAlgError: If the matrix is singular.

  • LinAlgWarning: If an ill- conditioned input a is detected.

  • NotImplementedError: If the transposed parameter is set to True and the input a is a complex matrix.

Example 1

In the example below, we pass two matrices to the linalg.solve() function, which returns the solution as an array. This code solves a systems of linear equations and prints the solution.

import numpy as np
from scipy import linalg

# Define matrices a and b
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

# Solve the system of linear equations
x = linalg.solve(a, b)

# Print the solution
print('The output solution is:', x)

Output

The result is generated as follows −

The output solution is: [ 2. -2.  9.]

Example 2

In the following example, we pass only the input matrices. Initially, we did not set the check_finite parameter in the linalg.solve() function, so it defaults to true. The input matrix contains NaNs, which result in a value error. The check_finite parameter checks for NaNs or infinities and returns a value error when set to True.

import numpy as np
from scipy import linalg

# Define matrices a and b, replacing np.nan with a valid number
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])  # Replaced np.nan with 1
b = np.array([2, 4, -1])

# Solve the system of linear equations
x = linalg.solve(a, b)

# Print the solution
print('The output solution is:', x)

Output

The code is generated as follows −

The output solution is: [nan nan nan]

Example 3

In the following example code, we pass both the a and b matrices, where a is a symmetric matrix. We set the assume_a parameter to 'sym' ti indicate this. The code then attempts to solve a system of linear equations using SciPy.

import numpy as np
from scipy import linalg

# Define the matrices a and b
a = np.array([[1, 1, -1], [1, 2, 0], [-1, 0, 5]])
b = np.array([2, 4, -1])

# Solve the system of linear equations
x = linalg.solve(a, b)

# Print the output solution
print('The output solution is:', x)

Output

The output is obtained as follows −

The ouput solution is: [-0.66666667  2.33333333 -0.33333333]
parent_file.htm
Advertisements