code: def jtheta(ctx, n, z, q, derivative=0):
      if derivative:
          return ctx._djtheta(n, z, q, derivative)
  
      z = ctx.convert(z)
      q = ctx.convert(q)
  
      # Implementation note
      # If ctx._im(z) is close to zero, _jacobi_theta2 and _jacobi_theta3
      # are used,
      # which compute the series starting from n=0 using fixed precision
      # numbers;
      # otherwise  _jacobi_theta2a and _jacobi_theta3a are used, which compute
      # the series starting from n=n0, which is the largest term.
  
      # TODO: write _jacobi_theta2a and _jacobi_theta3a using fixed-point
  
      if abs(q) > ctx.THETA_Q_LIM:
          raise ValueError(
              "abs(q) > THETA_Q_LIM = %f" % ctx.THETA_Q_LIM
          )
  
      extra = 10
      if z:
          M = ctx.mag(z)
          if M > 5 or (n == 1 and M < -5):
              extra += 2 * abs(M)
      cz = 0.5
      extra2 = 50
      prec0 = ctx.prec
      try:
          ctx.prec += extra
          if n == 1:
              if ctx._im(z):
                  if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
                      ctx.dps += extra2
                      res = ctx._jacobi_theta2(z - ctx.pi / 2, q)
                  else:
                      ctx.dps += 10
                      res = ctx._jacobi_theta2a(z - ctx.pi / 2, q)
              else:
                  res = ctx._jacobi_theta2(z - ctx.pi / 2, q)
          elif n == 2:
              if ctx._im(z):
                  if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
                      ctx.dps += extra2
                      res = ctx._jacobi_theta2(z, q)
                  else:
                      ctx.dps += 10
                      res = ctx._jacobi_theta2a(z, q)
              else:
                  res = ctx._jacobi_theta2(z, q)
          elif n == 3:
              if ctx._im(z):
                  if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
                      ctx.dps += extra2
                      res = ctx._jacobi_theta3(z, q)
                  else:
                      ctx.dps += 10
                      res = ctx._jacobi_theta3a(z, q)
              else:
                  res = ctx._jacobi_theta3(z, q)
          elif n == 4:
              if ctx._im(z):
                  if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
                      ctx.dps += extra2
                      res = ctx._jacobi_theta3(z, -q)
                  else:
                      ctx.dps += 10
                      res = ctx._jacobi_theta3a(z, -q)
              else:
                  res = ctx._jacobi_theta3(z, -q)
          else:
              raise ValueError
      finally:
          ctx.prec = prec0
      return res
  
file_path: catboost/contrib/python/mpmath/py3/mpmath/functions/theta.py
imports: from .functions import defun, defun_wrapped
label_desc: Use image node_id 9 for calling a global function with example usage: jtheta(ctx, n, z, q, derivative) and returns: res, ctx