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.zeros() Method:
We can generate a matrix with dimension nxm filled with zeros by using the zeros() method of the sympy module. Here n and m are passed as arguments.
where n = no of rows
m = no of columns
Syntax:
sympy.zeros()
Return Value:
A zero matrix is returned by the zeros() function.
sympy.zeros() Method in Python
Method #1: Using zeros Function (Static Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Pass number of rows and columns as arguments to zeros() function to generate a zero matrix of the given number of rows and columns.
- Here it generates a zero matrix with 3 rows and 3 columns.
- Print the zero matrix of the given number of rows and columns.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass number of rows and columns as arguments to zeros() function to # generate a zero matrix of the given number of rows and columns. # Here it generates a zero matrix with 3 rows and 3 columns. zero_matx = zeros(3, 3) # Print the zero matrix of the given number of rows and columns print("The zero matrix with 3 rows and 3 columns:") print(zero_matx)
Output:
The zero matrix with 3 rows and 3 columns: Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
Method #2: Using zeros Function (User Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give the number of rows as user input using the int(input()) function and store it in a variable.
- Give the number of columns as user input using the int(input()) function
and store it in another variable. - Pass the above-given number of rows and columns as arguments to zeros() function to generate a zero matrix with the given number of rows and columns.
- Print the zero matrix with the given number of rows and columns.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Give the number of rows as user input using the int(input()) function # and store it in a variable. gvn_rows = int(input("Enter some random no of rows = ")) # Give the number of columns as user input using the int(input()) function # and store it in another variable. gvn_columns = int(input("Enter some random no of columns = ")) # Pass the above given number of rows and columns as arguments to zeros() function # to generate a zero matrix with the given number of rows and columns. zero_matx = zeros(gvn_rows, gvn_columns) # Print the zero matrix with the given number of rows and columns. print("The zero matrix with {", gvn_rows, "} rows and{", gvn_columns,"}columns:") print(zero_matx)
Output:
Enter some random no of rows = 2 Enter some random no of columns = 4 The zero matrix with { 2 } rows and{ 4 }columns: Matrix([[0, 0, 0, 0], [0, 0, 0, 0]])