docs: def piecewise_linear_scalar(params, xval):
      '''Piecewise linear function for a scalar variable xval (float).
  
      :param params:
          Piecewise linear parameters (numpy.ndarray) in the following form:
          [slope_i,... slope_n, turning_point_i, ..., turning_point_n, intercept]
          Length params === 2 * number_segments, e.g.
          [slope_1, slope_2, slope_3, turning_point1, turning_point_2, intercept]
      :param xval:
          Value for evaluation of function (float)
      :returns:
          Piecewise linear function evaluated at point xval (float)
      '''
      n_params = len(params)
      n_seg, remainder = divmod(n_params, 2)
      if remainder:
          raise ValueError(
              'Piecewise Function requires 2 * nsegments parameters')
  
      if n_seg == 1:
          return params[1] + params[0] * xval
  
      gradients = params[0:n_seg]
      turning_points = params[n_seg: -1]
      c_val = np.array([params[-1]])
  
      for iloc in range(1, n_seg):
          c_val = np.hstack(
              [c_val, (c_val[iloc - 1] + gradients[iloc - 1] *
                       turning_points[iloc - 1]) - (gradients[iloc] *
                                                    turning_points[iloc - 1])])
  
      if xval <= turning_points[0]:
          return gradients[0] * xval + c_val[0]
      elif xval > turning_points[-1]:
          return gradients[-1] * xval + c_val[-1]
      else:
          select = np.nonzero(turning_points <= xval)[0][-1] + 1
      return gradients[select] * xval + c_val[select]