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.Mul() Method:
Using the Mul() method of the sympy module, we can multiply two variables and generate a mathematical expression.
Syntax:
sympy.Mul()
Return Value:
The multiplication/product of two variables is returned by the Mul() function.
sympy.Mul() Method in Python
Example1
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
- Pass the two above variables to the Mul() function to multiply both the variables and form a mathematical expression.
- Store it in another variable.
- Print the result mathematical expression after the multiplication of two variables.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass the symbols to be used as arguments to the symbols() function # and store them in corresponding variables. a, b = symbols('a b') # Pass the two above variables to the Mul() function to multiply both the variables # and form a mathematical expression. # Store it in another variable. rslt_expresn = Mul(a, b) # Print the result mathematical expression after the multiplication of two variables print(rslt_expresn)
Output:
a*b
Example2
Here we multiply the variables using the Mul() function and add the variables using the ‘+’ operator.
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
- Pass the two above variables to be multiplied to the Mul() function to multiply both the variables and form a resultant mathematical expression.
- Store it in another variable.
- Print the result mathematical expression after the multiplication of two variables.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Pass the symbols to be used as arguments to the symbols() function # and store them in corresponding variables. a, b = symbols('a b') # Pass the two above variables to be multiplied to the Mul() function to # multiply both the variables and form a resultant mathematical expression. # Store it in another variable. rslt_expresn = Mul(a, a) + Mul(b, b) # Print the result mathematical expression after the multiplication of two variables print(rslt_expresn)
Output:
a**2 + b**2