code: def dot(a, b):
      r
      if isinstance(a, Mul):
          a = a.expand()
          avect = 1
          aivect = -1
          for ai, fact in enumerate(a.args):
              if isinstance(fact, Vector3D):
                  avect = fact
                  aivect = ai
                  break
  
          acoef = a.args[:aivect] + a.args[aivect+1:]
          acoef = Mul(*acoef)
          return acoef*dot(avect, b)
  
      if isinstance(b, Mul):
          b = b.expand()
          bvect = 1
          bivect = -1
          for bi, fact in enumerate(b.args):
              if isinstance(fact, Vector3D):
                  bvect = fact
                  bivect = bi
                  break
  
          bcoef = b.args[:bivect] + b.args[bivect+1:]
          bcoef = Mul(*bcoef)
          return bcoef*dot(a, bvect)
  
      if isinstance(a, Vector3D) and isinstance(b, Vector3D):
          return DotProduct(a, b)
  
      if hasattr(a, "shape") and hasattr(b, "shape"):
          return cartesian_dot_product(a, b)
  
      print a, b, type(a), type(b),
      print isinstance(a, Vector3D), isinstance(b, Vector3D)
      raise NotImplementedError("could not catch these instances in dot!")
docs: r"""Dot product of two 3d vectors.