code: def rewrite(e, Omega, x, wsym):
      """e(x) ... the function
      Omega ... the mrv set
      wsym ... the symbol which is going to be used for w
  
      Returns the rewritten e in terms of w and log(w). See test_rewrite1()
      for examples and correct results.
      """
  
      from sympy import AccumBounds
  
      if not isinstance(Omega, SubsSet):
          raise TypeError("Omega should be an instance of SubsSet")
      if len(Omega) == 0:
          raise ValueError("Length cannot be 0")
      # all items in Omega must be exponentials
      for t in Omega.keys():
          if not isinstance(t, exp):
              raise ValueError("Value should be exp")
      rewrites = Omega.rewrites
      Omega = list(Omega.items())
  
      nodes = build_expression_tree(Omega, rewrites)
      Omega.sort(key=lambda x: nodes[x[1]].ht(), reverse=True)
  
      # make sure we know the sign of each exp() term; after the loop,
      # g is going to be the "w" - the simplest one in the mrv set
      for g, _ in Omega:
          sig = sign(g.exp, x)
          if sig != 1 and sig != -1 and not sig.has(AccumBounds):
              raise NotImplementedError(
                  "Result depends on the sign of %s" % sig
              )
      if sig == 1:
          wsym = 1 / wsym  # if g goes to oo, substitute 1/w
      # O2 is a list, which results by rewriting each item in Omega using "w"
      O2 = []
      denominators = []
      for f, var in Omega:
          c = limitinf(f.exp / g.exp, x)
          if c.is_Rational:
              denominators.append(c.q)
          arg = f.exp
          if var in rewrites:
              if not isinstance(rewrites[var], exp):
                  raise ValueError("Value should be exp")
              arg = rewrites[var].args[0]
          O2.append((var, exp((arg - c * g.exp).expand()) * wsym**c))
  
      # Remember that Omega contains subexpressions of "e". So now we find
      # them in "e" and substitute them for our rewriting, stored in O2
  
      # the following powsimp is necessary to automatically combine exponentials,
      # so that the .xreplace() below succeeds:
      # TODO this should not be necessary
      from sympy.simplify.powsimp import powsimp
  
      f = powsimp(e, deep=True, combine="exp")
      for a, b in O2:
          f = f.xreplace({a: b})
  
      for _, var in Omega:
          assert not f.has(var)
  
      # finally compute the logarithm of w (logw).
      logw = g.exp
      if sig == 1:
          logw = -logw  # log(w)->log(1/w)=-log(w)
  
      # Some parts of SymPy have difficulty computing series expansions with
      # non-integral exponents. The following heuristic improves the situation:
      exponent = reduce(ilcm, denominators, 1)
      f = f.subs({wsym: wsym**exponent})
      logw /= exponent
  
      # bottom_up function is required for a specific case - when f is
      # -exp(p/(p + 1)) + exp(-p**2/(p + 1) + p). No current simplification
      # methods reduce this to 0 while not expanding polynomials.
      f = bottom_up(f, lambda w: getattr(w, "normal", lambda: w)())
      f = expand_mul(f)
  
      return f, logw
  
imports: from functools import reduce
  from sympy.core import Basic, S, Mul, PoleError, expand_mul
  from sympy.core.cache import cacheit
  from sympy.core.intfunc import ilcm
  from sympy.core.numbers import I, oo
  from sympy.core.symbol import Dummy, Wild
  from sympy.core.traversal import bottom_up
  from sympy.functions import log, exp, sign
  from sympy.series.order import Order
  from sympy.utilities.exceptions import SymPyDeprecationWarning
  from sympy.utilities.misc import debug_decorator
  from sympy.utilities.timeutils import timethis
label_desc: Use image node_id 12 for calling a global function with example usage: rewrite(e, Omega, x, wsym) and returns: f,  logw