'Rewrite functions in terms of other functions.
Rewrites expression containing applications of functions
of one kind in terms of functions of different kind. For
example you can rewrite trigonometric functions as complex
exponentials or combinatorial functions as gamma function.
As a pattern this function accepts a list of functions to
to rewrite (instances of DefinedFunction class). As rule
you can use string or a destination function instance (in
this case rewrite() will use the str() function).
There is also the possibility to pass hints on how to rewrite
the given expressions. For now there is only one such hint
defined called \'deep\'. When \'deep\' is set to False it will
forbid functions to rewrite their contents.
Examples
>>> from sympy import sin, exp
>>> from sympy.abc import x
Unspecified pattern:
>>> sin(x).rewrite(exp)
-I*(exp(I*x) - exp(-I*x))/2
Pattern as a single function:
>>> sin(x).rewrite(sin, exp)
-I*(exp(I*x) - exp(-I*x))/2
Pattern as a list of functions:
>>> sin(x).rewrite([sin, ], exp)
-I*(exp(I*x) - exp(-I*x))/2'

def rewrite(self, *args, **hints):

    if (not args):
        return self
    else:
        pattern = args[:(-1)]
        if isinstance(args[(-1)], string_types):
            rule = ('_eval_rewrite_as_' + args[(-1)])
        else:
            try:
                rule = ('_eval_rewrite_as_' + args[(-1)].__name__)
            except:
                rule = ('_eval_rewrite_as_' + args[(-1)].__class__.__name__)
        if (not pattern):
            return self._eval_rewrite(None, rule, **hints)
        else:
            if iterable(pattern[0]):
                pattern = pattern[0]
            pattern = [p for p in pattern if self.has(p)]
            if pattern:
                return self._eval_rewrite(tuple(pattern), rule, **hints)
            else:
                return self
