docstr: Double the treesize by extending the tree in the given direction.
  
  If direction is larger than 0, extend it to the right, otherwise
  extend it to the left.
  
  Return a tuple `(diverging, turning)` of type (DivergenceInfo, bool).
  `diverging` indicates, that the tree extension was aborted because
  the energy change exceeded `self.Emax`. `turning` indicates that
  the tree extension was stopped because the termination criterior
  was reached (the trajectory is turning back).
docstr_tok: ["Double","the","treesize","by","extending","the","tree","in","the","given","direction",".","If","direction","is","larger","than","0",",","extend","it","to","the","right",",","otherwise","extend","it","to","the","left",".","Return","a","tuple","`","(","diverging",",","turning",")","`","of","type","(","DivergenceInfo",",","bool",")",".","`","diverging","`","indicates",",","that","the","tree","extension","was","aborted","because","the","energy","change","exceeded","`","self.Emax","`",".","`","turning","`","indicates","that","the","tree","extension","was","stopped","because","the","termination","criterior","was","reached","(","the","trajectory","is","turning","back",")","."]
code: def extend(self, direction):
      """Double the treesize by extending the tree in the given direction.
  
      If direction is larger than 0, extend it to the right, otherwise
      extend it to the left.
  
      Return a tuple `(diverging, turning)` of type (DivergenceInfo, bool).
      `diverging` indicates, that the tree extension was aborted because
      the energy change exceeded `self.Emax`. `turning` indicates that
      the tree extension was stopped because the termination criterior
      was reached (the trajectory is turning back).
      """
      if direction > 0:
          tree, diverging, turning = self._build_subtree(
              self.right, self.depth, floatX(np.asarray(self.step_size))
          )
          leftmost_begin, leftmost_end = self.left, self.right
          rightmost_begin, rightmost_end = tree.left, tree.right
          leftmost_p_sum = self.p_sum.copy()
          rightmost_p_sum = tree.p_sum
          self.right = tree.right
      else:
          tree, diverging, turning = self._build_subtree(
              self.left, self.depth, floatX(np.asarray(-self.step_size))
          )
          leftmost_begin, leftmost_end = tree.right, tree.left
          rightmost_begin, rightmost_end = self.left, self.right
          leftmost_p_sum = tree.p_sum
          rightmost_p_sum = self.p_sum.copy()
          self.left = tree.right
  
      self.depth += 1
  
      if diverging or turning:
          return diverging, turning
  
      size1, size2 = self.log_size, tree.log_size
      if logbern(size2 - size1):
          self.proposal = tree.proposal
  
      self.log_size = np.logaddexp(self.log_size, tree.log_size)
      self.p_sum[:] += tree.p_sum
  
      # Additional turning check only when tree depth > 0 to avoid redundant work
      if self.depth > 0:
          left, right = self.left, self.right
          p_sum = self.p_sum
          turning = (p_sum.dot(left.v) <= 0) or (
              p_sum.dot(right.v) <= 0
          )
          p_sum1 = leftmost_p_sum + rightmost_begin.p.data
          turning1 = (p_sum1.dot(leftmost_begin.v) <= 0) or (
              p_sum1.dot(rightmost_begin.v) <= 0
          )
          p_sum2 = leftmost_end.p.data + rightmost_p_sum
          turning2 = (p_sum2.dot(leftmost_end.v) <= 0) or (
              p_sum2.dot(rightmost_end.v) <= 0
          )
          turning = turning | turning1 | turning2
  
      return diverging, turning
  
imports: from __future__ import annotations
  from collections import namedtuple
  import numpy
  from pymc.math import logbern
  from pymc.pytensorf import floatX
  from pymc.stats.convergence import SamplerWarning
  from pymc.step_methods.compound import Competence
  from pymc.step_methods.hmc import integration
  from pymc.step_methods.hmc.base_hmc import BaseHMC, DivergenceInfo, HMCStepData
  from pymc.step_methods.hmc.integration import IntegrationError, State
  from pymc.vartypes import continuous_types
label_desc: Use image node_id 2 for calling the _Tree obj's underlying member method code with example usage: obj.extend(direction) and returns: diverging,  turning, diverging,  turning