romspline/greedy.py
def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None):
  """Greedy algorithm for building a reduced-order spline"""
  if verbose:
    print("\nSize", "\t", "Error")
    print("="*13)
  
  if rel:
    ymax = np.max(np.abs(y))
    assert ymax > 0., "All data samples are zero."
  else:
    ymax = 1.
  _tol = tol*ymax
  
  # Seed greedy algorithm
  indices, errors = _seed(x, deg=deg, seeds=seeds)
  args = indices[:]  # Keep track of unsorted indices
  
  # Greedy algorithm
  flag, ctr = 0, len(indices)+1
  while flag == 0 and ctr < len(x):	
    # Spline interpolant on current set of knots
    s = UnivariateSpline(x[indices], y[indices], k=deg, s=0)
    
    # L-infinity errors of current spline interpolant and data
    errs = np.abs( s(x)-y )
    
    # Get the index of the largest interpolation error on y
    imax = np.argmax( errs )
    
    # Update arrays with data point that gives largest interpolation error
    errors.append( errs[imax] )
    args = np.hstack([args, imax])
    indices = np.sort(np.hstack([indices, imax]))  # Indices sorted for spline interpolation
    
    # Print to screen, if requested
    if verbose:
      print(ctr, "\t", errors[-1])
    
    # Check if greedy error is below tolerance and exit if so
    if errors[-1] < _tol:
      flag = 1
      args = args[:-1]
      indices = np.sort(args)
      errors = np.array(errors[:-1])
    
    ctr += 1
  
  # Construct the spline from the reduced data
  spline = UnivariateSpline(x[indices], y[indices], k=deg, s=0)
  
  return spline, indices, args, errors, _tol
Greedy algorithm for building a reduced-order spline
Greedy algorithm for building a reduced-order spline
Greedy algorithm for building a reduced - order spline
def _greedy(x, y, tol=1e-6, rel=False, deg=5, verbose=False, seeds=None):
  if verbose:
    print("\nSize", "\t", "Error")
    print("="*13)
  if rel:
    ymax = np.max(np.abs(y))
    assert ymax > 0., "All data samples are zero."
  else:
    ymax = 1.
  _tol = tol*ymax
  indices, errors = _seed(x, deg=deg, seeds=seeds)
  args = indices[:]  
  flag, ctr = 0, len(indices)+1
  while flag == 0 and ctr < len(x):	
    s = UnivariateSpline(x[indices], y[indices], k=deg, s=0)
    errs = np.abs( s(x)-y )
    imax = np.argmax( errs )
    errors.append( errs[imax] )
    args = np.hstack([args, imax])
    indices = np.sort(np.hstack([indices, imax]))  
    if verbose:
      print(ctr, "\t", errors[-1])
    if errors[-1] < _tol:
      flag = 1
      args = args[:-1]
      indices = np.sort(args)
      errors = np.array(errors[:-1])
    ctr += 1
  spline = UnivariateSpline(x[indices], y[indices], k=deg, s=0)
  return spline, indices, args, errors, _tol
def _greedy ( x , y , tol = 1e-6 , rel = False , deg = 5 , verbose = False , seeds = None ) : if verbose : print ( "\nSize" , "\t" , "Error" ) print ( "=" * 13 ) if rel : ymax = np . max ( np . abs ( y ) ) assert ymax > 0. , "All data samples are zero." else : ymax = 1. _tol = tol * ymax indices , errors = _seed ( x , deg = deg , seeds = seeds ) args = indices [ : ] flag , ctr = 0 , len ( indices ) + 1 while flag == 0 and ctr < len ( x ) : s = UnivariateSpline ( x [ indices ] , y [ indices ] , k = deg , s = 0 ) errs = np . abs ( s ( x ) - y ) imax = np . argmax ( errs ) errors . append ( errs [ imax ] ) args = np . hstack ( [ args , imax ] ) indices = np . sort ( np . hstack ( [ indices , imax ] ) ) if verbose : print ( ctr , "\t" , errors [ - 1 ] ) if errors [ - 1 ] < _tol : flag = 1 args = args [ : - 1 ] indices = np . sort ( args ) errors = np . array ( errors [ : - 1 ] ) ctr += 1 spline = UnivariateSpline ( x [ indices ] , y [ indices ] , k = deg , s = 0 ) return spline , indices , args , errors , _tol
Greedy algorithm for building a reduced-order spline
Greedy algorithm for building a reduced - order spline
"""Greedy algorithm for building a reduced-order spline""" # Seed greedy algorithm # Keep track of unsorted indices # Greedy algorithm # Spline interpolant on current set of knots # L-infinity errors of current spline interpolant and data # Get the index of the largest interpolation error on y # Update arrays with data point that gives largest interpolation error # Indices sorted for spline interpolation # Print to screen, if requested # Check if greedy error is below tolerance and exit if so # Construct the spline from the reduced data
x
y
tol
rel
deg
verbose
seeds