romspline/helpers.py
def integrate(y, x, weight=None, partial=False):
  """Trapezoidal rule for integration.
  
  Inputs
  ======
    y       -- data values or responses
    x       -- data samples
    weight  -- Weighting factor in the integrand
               (default is None)
    partial -- Output partial sums instead of the integral
               (default is False)
  
  Outputs
  =======
    Scalar equal to the numerical integration of y
  over x using the trapezoidal quadrature rule. If
  partial is True then the output is an array of 
  partial sums for each element of y over the previous
  x values.
  
  Comments
  ========
    The trapezoidal rule is second-order convergent in 
  the uniform increment of x.
  
  """
  
  # Check that the data arrays have the same length
  assert len(y) == len(x), "Expecting arrays `y` and `x` to have equal lengths."
  
  # Check that the samples are uniformly spaced
  dx = x[1]-x[0]
  #assert np.all([np.isclose(xx, dx) for xx in np.diff(x)]), "Expecting input sample array `x` to be uniformly spaced."
  
  # Build quadrature weights
  quad_weights = np.ones(len(x))*dx
  quad_weights[0] *= 0.5
  quad_weights[-1] *= 0.5
  if weight is not None:
    assert len(x) == len(weight), "Expecting `weight` option to be an array with the same length as `x`."
    quad_weights /= weight
  
  # Compute partial sum, if requested
  if partial:
    ans = np.zeros_like(y)
    for ii in range(len(ans[:-1])):
      ans[ii+1] = ans[ii] + quad_weights[ii]*y[ii]
  # Otherwise, compute the full integral
  else:
    ans = np.sum(quad_weights*y)
  
  return ans
Trapezoidal rule for integration.
  
  Inputs
  ======
    y       -- data values or responses
    x       -- data samples
    weight  -- Weighting factor in the integrand
               (default is None)
    partial -- Output partial sums instead of the integral
               (default is False)
  
  Outputs
  =======
    Scalar equal to the numerical integration of y
  over x using the trapezoidal quadrature rule. If
  partial is True then the output is an array of 
  partial sums for each element of y over the previous
  x values.
  
  Comments
  ========
    The trapezoidal rule is second-order convergent in 
  the uniform increment of x.
  
  
Trapezoidal rule for integration.
Inputs



Outputs

Scalar equal to the numerical integration of y
over x using the trapezoidal quadrature rule. If
partial is True then the output is an array of
partial sums for each element of y over the previous
x values.

Comments

The trapezoidal rule is second-order convergent in
the uniform increment of x.
Trapezoidal rule for integration . Inputs Outputs Scalar equal to the numerical integration of y over x using the trapezoidal quadrature rule . If partial is True then the output is an array of partial sums for each element of y over the previous x values . Comments The trapezoidal rule is second - order convergent in the uniform increment of x .
def integrate(y, x, weight=None, partial=False):
  assert len(y) == len(x), "Expecting arrays `y` and `x` to have equal lengths."
  dx = x[1]-x[0]
  quad_weights = np.ones(len(x))*dx
  quad_weights[0] *= 0.5
  quad_weights[-1] *= 0.5
  if weight is not None:
    assert len(x) == len(weight), "Expecting `weight` option to be an array with the same length as `x`."
    quad_weights /= weight
  if partial:
    ans = np.zeros_like(y)
    for ii in range(len(ans[:-1])):
      ans[ii+1] = ans[ii] + quad_weights[ii]*y[ii]
  else:
    ans = np.sum(quad_weights*y)
  return ans
def integrate ( y , x , weight = None , partial = False ) : assert len ( y ) == len ( x ) , "Expecting arrays `y` and `x` to have equal lengths." dx = x [ 1 ] - x [ 0 ] quad_weights = np . ones ( len ( x ) ) * dx quad_weights [ 0 ] *= 0.5 quad_weights [ - 1 ] *= 0.5 if weight is not None : assert len ( x ) == len ( weight ) , "Expecting `weight` option to be an array with the same length as `x`." quad_weights /= weight if partial : ans = np . zeros_like ( y ) for ii in range ( len ( ans [ : - 1 ] ) ) : ans [ ii + 1 ] = ans [ ii ] + quad_weights [ ii ] * y [ ii ] else : ans = np . sum ( quad_weights * y ) return ans
Trapezoidal rule for integration.
Trapezoidal rule for integration .
"""Trapezoidal rule for integration.
  
  Inputs
  ======
    y       -- data values or responses
    x       -- data samples
    weight  -- Weighting factor in the integrand
               (default is None)
    partial -- Output partial sums instead of the integral
               (default is False)
  
  Outputs
  =======
    Scalar equal to the numerical integration of y
  over x using the trapezoidal quadrature rule. If
  partial is True then the output is an array of 
  partial sums for each element of y over the previous
  x values.
  
  Comments
  ========
    The trapezoidal rule is second-order convergent in 
  the uniform increment of x.
  
  """ # Check that the data arrays have the same length # Check that the samples are uniformly spaced #assert np.all([np.isclose(xx, dx) for xx in np.diff(x)]), "Expecting input sample array `x` to be uniformly spaced." # Build quadrature weights # Compute partial sum, if requested # Otherwise, compute the full integral
y
x
weight
partial