Python | sympy.rewrite() method

With the help of sympy.rewrite() method, we can represent any mathematical function in terms of another function.

Syntax: expression.rewrite(function)

Parameters:
expression – It is mathematical expression which is to be represented by another function.
function – It is the mathematical function used to rewrite the given expression.



Returns: Returns a mathematical expression corresponding to the input in terms of specified function.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
x = symbols('x')
expr = tan(x)
print("Expression = {}".format(expr))
   
# Use sympy.rewrite() method 
expr_in_terms_of_sin = expr.rewrite(sin)  
      
print("Expression in terms of sin() : {}".format(expr_in_terms_of_sin))  

chevron_right


Output:

Expression = tan(x)
Expression in terms of sin() : 2*sin(x)**2/sin(2*x)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
x = symbols('x')
expr = factorial(x)
print("Expression = {}".format(expr))
   
# Use sympy.rewrite() method 
expr_in_terms_of_gamma = expr.rewrite(gamma)  
      
print("Expression in terms of gamma() : {}".format(expr_in_terms_of_gamma))  

chevron_right


Output:

Expression = factorial(x)
Expression in terms of gamma() : gamma(x + 1)


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.