def newton_cotes(rn, equal=<NUM_LIT:0>):
try:<EOL><INDENT>N = len(rn)-<NUM_LIT:1><EOL>if equal:<EOL><INDENT>rn = np.arange(N+<NUM_LIT:1>)<EOL><DEDENT>elif np.all(np.diff(rn) == <NUM_LIT:1>):<EOL><INDENT>equal = <NUM_LIT:1><EOL><DEDENT><DEDENT>except:<EOL><INDENT>N = rn<EOL>rn = np.arange(N+<NUM_LIT:1>)<EOL>equal = <NUM_LIT:1><EOL><DEDENT>if equal and N in _builtincoeffs:<EOL><INDENT>na, da, vi, nb, db = _builtincoeffs[N]<EOL>return na*np.array(vi,float)/da, float(nb)/db<EOL><DEDENT>if (rn[<NUM_LIT:0>] != <NUM_LIT:0>) or (rn[-<NUM_LIT:1>] != N):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>yi = rn / float(N)<EOL>ti = <NUM_LIT>*yi - <NUM_LIT:1><EOL>nvec = np.arange(<NUM_LIT:0>,N+<NUM_LIT:1>)<EOL>C = ti**nvec[:,np.newaxis]<EOL>Cinv = np.linalg.inv(C)<EOL>for i in range(<NUM_LIT:2>):<EOL><INDENT>Cinv = <NUM_LIT:2>*Cinv - Cinv.dot(C).dot(Cinv)<EOL><DEDENT>vec = <NUM_LIT> / (nvec[::<NUM_LIT:2>]+<NUM_LIT:1>)<EOL>ai = np.dot(Cinv[:,::<NUM_LIT:2>],vec) * N/<NUM_LIT:2><EOL>if (N % <NUM_LIT:2> == <NUM_LIT:0>) and equal:<EOL><INDENT>BN = N/(N+<NUM_LIT>)<EOL>power = N+<NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>BN = N/(N+<NUM_LIT>)<EOL>power = N+<NUM_LIT:1><EOL><DEDENT>BN = BN - np.dot(yi**power, ai)<EOL>p1 = power+<NUM_LIT:1><EOL>fac = power*math.log(N) - gammaln(p1)<EOL>fac = math.exp(fac)<EOL>return ai, BN*fac<EOL>
Return weights and error coefficient for Newton-Cotes integration.

Suppose we have (N+1) samples of f at the positions
x_0, x_1, ..., x_N.  Then an N-point Newton-Cotes formula for the
integral between x_0 and x_N is:

:math:`\\int_{x_0}^{x_N} f(x)dx = \\Delta x \\sum_{i=0}^{N} a_i f(x_i)
+ B_N (\\Delta x)^{N+2} f^{N+1} (\\xi)`

where :math:`\\xi \\in [x_0,x_N]` and :math:`\\Delta x = \\frac{x_N-x_0}{N}`
is the averages samples spacing.

If the samples are equally-spaced and N is even, then the error
term is :math:`B_N (\\Delta x)^{N+3} f^{N+2}(\\xi)`.

Parameters
----------
rn : int
    The integer order for equally-spaced data or the relative positions of
    the samples with the first sample at 0 and the last at N, where N+1 is
    the length of `rn`.  N is the order of the Newton-Cotes integration.
equal : int, optional
    Set to 1 to enforce equally spaced data.

Returns
-------
an : ndarray
    1-D array of weights to apply to the function at the provided sample
    positions.
B : float
    Error coefficient.

Notes
-----
Normally, the Newton-Cotes rules are used on smaller integration
regions and a composite rule is used to return the total integral.