# Binning¶

This page explains the mathematics behind the binning functionality found in
`bins`

. The binning functions are purely mathematical in nature though
they do have application in other parts of pyne that deal more directly physics
with physics.

## Pointwise Linear Collapse¶

The `pointwise_linear_collapse()`

function takes an array of pointwise
data y that has the independent variable x and collapses this into
G groups as defined by the bin boundaries x_g where g
indexes G. Both x and x_g must be monotonic and in the
same direction. Say that there are N points in x and y.
Let n index N. Then for all points internal to a group, the
collapsed value y_g is:

y_g = \frac{1}{x_{g+1} - x_g} \sum_{n|x_g \le x_n}^{x_n \le x_{g+1}} \frac{y_{n+1} + y_n}{2} * (x_{n+1} - x_n)

The term (y_{n+1} + y_n)/2 is the center (average) value of a linear interpolation between the two points. Therefore, y_g is the x-weighted average of y over the entire group.

In the event that the line between y_n and y_{n+1} crosses either the lower or upper bin boundary (or both) then their values are adjusted via a linear interpolation to the value at the bin boundary.

For a lower boundary crossing, the following substitutions are made to the equation above:

x_n \to x_g

y_n \to \frac{y_{n+1} - y_n}{x_{n+1} - x_n} (x_g - x_n) + y_n

For an upper boundary crossing:

x_{n+1} \to x_{g+1}

y_{n+1} \to \frac{y_{n+1} - y_n}{x_{n+1} - x_n} (x_{g+1} - x_n) + y_n