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.diag() Method:
Using the sympy.diag() method, we can generate a matrix with dimension nxn and filled with numbers in the diagonal.
Rest all the elements of the matrix are filled with zeroes.
Syntax:
sympy.diag()
Return Value:
A matrix filled with numbers in the diagonal is returned by the diag() function.
sympy.diag() Method in Python
For 2-Dimensional (2D) Matrix
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the elements of a diagonal matrix to the diag() function to generate a diagonal matrix and store it in a variable.
- Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it.
- Print the resultant diagonal matrix.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass the elements of a diagonal matrix to the diag() function to generate a # diagonal matrix and store it in a variable. # Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it. rslt_matrx = diag(5, 2) # Print the resultant diagonal matrix. print("The resultant 2-Dimensional diagonal matrix is:") print(rslt_matrx)
Output:
The resultant 2-Dimensional diagonal matrix is: Matrix([[5, 0], [0, 2]])
For 3-Dimensional (3D) Matrix
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the elements of a diagonal matrix to the diag() function to generate a diagonal matrix and store it in a variable.
- Here it creates a 3-Dimensional(3D) matrix since three numbers are passed as arguments to it.
- Print the resultant diagonal matrix.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass the elements of a diagonal matrix to the diag() function to generate a # diagonal matrix and store it in a variable. # Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it. rslt_matrx = diag(1, 4, 6) # Print the resultant diagonal matrix. print("The resultant 3-Dimensional diagonal matrix is:") print(rslt_matrx)
Output:
The resultant 3-Dimensional diagonal matrix is: Matrix([[1, 0, 0], [0, 4, 0], [0, 0, 6]])