Python SymPy Module:
SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.
Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.
SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.
Installation:
pip install sympy
Python sympy.det() Method:
We can determine the determinant of a matrix using the sympy.det() method.
Syntax:
sympy.det()
Return Value:
The determinant of a matrix is determined by the det() function
sympy.det() Method in Python
For 2-Dimensional (2D) Matrix
Approach:
- Import all the functions from sympy module using the import keyword
- Create a matrix(2-Dimensional) using the Matrix() function by passing some random 2D matrix as an argument to it and store it in a variable.
- Print the given matrix.
- Apply det() function on the above-given matrix to get the determinant of a given matrix and store it in another variable.
- Print the determinant of a given matrix.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Create a matrix(2-Dimensional) using the Matrix() function by passing # some random 2D matrix as an argument to it and store it in a variable gvn_matrx = Matrix([[10, 6], [4, 3]]) # Print the given matrix print("The given matrix is:\n", gvn_matrx) # Apply det() function on the above given matrix to get the determinant # of a given matrix and store it in another variable rslt_determinant = gvn_matrx.det() # Print the determinant of a given matrix. print("The determinant of a given matrix = ", rslt_determinant)
Output:
The given matrix is: Matrix([[10, 6], [4, 3]]) The determinant of a given matrix = 6
For 3-Dimensional (3D) Matrix
Approach:
- Import all the functions from sympy module using the import keyword
- Create a matrix(3-Dimensional) using the Matrix() function by passing some random 3D matrix as an argument to it and store it in a variable.
- Print the given matrix.
- Apply det() function on the above-given matrix to get the determinant of a given matrix and store it in another variable.
- Print the determinant of a given matrix.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Create a matrix(3-Dimensional) using the Matrix() function by passing # some random 3D matrix as an argument to it and store it in a variable gvn_matrx = Matrix([[4, 2, 1], [-2, 1, 6], [5, 3, 2]]) # Print the given matrix print("The given matrix is:\n", gvn_matrx) # Apply det() function on the above given matrix to get the determinant # of a given matrix and store it in another variable rslt_determinant = gvn_matrx.det() # Print the determinant of a given matrix. print("The determinant of a given matrix = ", rslt_determinant)
Output:
The given matrix is: Matrix([[4, 2, 1], [-2, 1, 6], [5, 3, 2]]) The determinant of a given matrix = -7