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.expand_log() Method:
We can use expand_log() function of the sympy module to simplify the log terms in the mathematical equation by employing the properties stated below:
- log(x*y)=log(x)+log(y)
- log(x**n)=nlog(x)
Syntax:
sympy.expand_log()
Return Value:
The simplified mathematical expression is returned by the expand_log() function.
sympy.expand_log() Method in Python
Example1
Here it gives the result based on the log(x*y)=log(x)+log(y) property.
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the symbols to be used and set positive as True as arguments to the symbols() function and store them in corresponding variables.
- Give the mathematical expression as static input and store it in a variable.
- Pass the above-given expression as an argument to the expand_log() method to simplify the log terms in the given expression based on the properties given.
- Here it gives the result based on the log(x*y)=log(x)+log(y) property.
- Store it in another variable.
- Print the result after the simplification of log terms in the given expression.
- 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 and set positive as True as arguments to the # symbols() function and store them in corresponding variables. a, b, c = symbols('a b c', positive = True) # Give the mathematical expression as static input and store it in a variable. gvn_expression = log(a * b) # Pass the above given expression as an argument to the expand_log() method # to simplify the log terms in the given expression based on the properties given. # Store it in another variable. rslt = expand_log(gvn_expression) # Print the result after the simplification of log terms in the given expression. print(rslt)
Output:
log(a) + log(b)
Example2
Here it gives the result based on the log(x**n)=nlog(x) property.
Approach:
- Import all the functions from sympy module using the import keyword
- Pass the symbols to be used and set positive as True as arguments to the symbols() function and store them in corresponding variables.
- Give the mathematical expression as static input and store it in a variable.
- Pass the above-given expression as an argument to the expand_log() method to simplify the log terms in the given expression based on the properties given.
- Here it gives the result based on the log(x**n)=nlog(x) property.
- Store it in another variable.
- Print the result after the simplification of log terms in the given expression.
- 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 and set set positive as True as arguments to the # symbols() function and store them in corresponding variables. a, b, c = symbols('a b c', positive = True) # Give the mathematical expression as static input and store it in a variable. gvn_expression = log(b**2) # Pass the above given expression as an argument to the expand_log() method # to simplify the log terms in the given expression based on the properties given. # Here it gives the result based on the log(x**n)=nlog(x) property. # Store it in another variable. rslt = expand_log(gvn_expression) # Print the result after the simplification of log terms in the given expression. print(rslt)
Output:
2*log(b)