code: def newton(f, x, verbose=False, tol=1e-6, maxit=5, jactype='serial'):
  
  
      
      if verbose:
          print = lambda txt: old_print(txt)
      else:
          print = lambda txt: None
  
      it = 0
      error = 10
      converged = False
      maxbacksteps = 30
  
      x0 = x
  
      if jactype == 'sparse':
          from scipy.sparse.linalg import spsolve as solve
      elif jactype == 'full':
          from numpy.linalg import solve
      else:
          solve = serial_solve
  
      while it<maxit and not converged:
  
          [v,dv] = f(x)
  
          # TODO: rewrite starting here
  
  #        print("Time to evaluate {}".format(ss-tt)0)
  
          error_0 = abs(v).max()
  
          if error_0 < tol:
  
              if verbose:
                  print("> System was solved after iteration {}. Residual={}".format(it,error_0))
              converged = True
  
          else:
  
              it += 1
  
              dx = solve(dv, v)
  
              # norm_dx = abs(dx).max()
  
              for bck in range(maxbacksteps):
                  xx = x - dx*(2**(-bck))
                  vm = f(xx)[0]
                  err = abs(vm).max()
                  if err < error_0:
                      break
  
              x = xx
  
              if verbose:
                  print("\t> {} | {} | {}".format(it, err, bck))
  
      if not converged:
          import warnings
          warnings.warn("Did not converge")
      return [x, it]
docs: Solve nonlinear system using safeguarded Newton iterations
  
  
      Parameters
      ----------
  
      Return
      ------