code: def continued_fraction_reduce(cf):
      """
      Reduce a continued fraction to a rational or quadratic irrational.
  
      Compute the rational or quadratic irrational number from its
      terminating or periodic continued fraction expansion.  The
      continued fraction expansion (cf) should be supplied as a
      terminating iterator supplying the terms of the expansion.  For
      terminating continued fractions, this is equivalent to
      ``list(continued_fraction_convergents(cf))[-1]``, only a little more
      efficient.  If the expansion has a repeating part, a list of the
      repeating terms should be returned as the last element from the
      iterator.  This is the format returned by
      continued_fraction_periodic.
  
      For quadratic irrationals, returns the largest solution found,
      which is generally the one sought, if the fraction is in canonical
      form (all terms positive except possibly the first).
  
      Examples
      ========
  
      >>> from sympy.ntheory.continued_fraction import continued_fraction_reduce
      >>> continued_fraction_reduce([1, 2, 3, 4, 5])
      225/157
      >>> continued_fraction_reduce([-2, 1, 9, 7, 1, 2])
      -256/233
      >>> continued_fraction_reduce([2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8]).n(10)
      2.718281835
      >>> continued_fraction_reduce([1, 4, 2, [3, 1]])
      (sqrt(21) + 287)/238
      >>> continued_fraction_reduce([[1]])
      (1 + sqrt(5))/2
      >>> from sympy.ntheory.continued_fraction import continued_fraction_periodic
      >>> continued_fraction_reduce(continued_fraction_periodic(8, 5, 13))
      (sqrt(13) + 8)/5
  
      See Also
      ========
  
      continued_fraction_periodic
  
      """
      from sympy.solvers import solve
  
      period = []
      x = Dummy("x")
  
      def untillist(cf):
          for nxt in cf:
              if isinstance(nxt, list):
                  period.extend(nxt)
                  yield x
                  break
              yield nxt
  
      a = S.Zero
      for a in continued_fraction_convergents(untillist(cf)):
          pass
  
      if period:
          y = Dummy("y")
          solns = solve(continued_fraction_reduce(period + [y]) - y, y)
          solns.sort()
          pure = solns[-1]
          rv = a.subs(x, pure).radsimp()
      else:
          rv = a
      if rv.is_Add:
          rv = factor_terms(rv)
          if rv.is_Mul and rv.args[0] == -1:
              rv = rv.func(*rv.args)
      return rv
  
file_path: sympy/sympy/ntheory/continued_fraction.py
imports: from __future__ import annotations
  import itertools
  from sympy.core.exprtools import factor_terms
  from sympy.core.numbers import Integer, Rational
  from sympy.core.singleton import S
  from sympy.core.symbol import Dummy
  from sympy.core.sympify import _sympify
  from sympy.utilities.misc import as_int
label_desc: Use image node_id 3 for calling a global function with example usage: continued_fraction_reduce(cf) and returns: rv