Ejemplo n.º 1
0
def bernstein(v, n):
    '''
    Returns the Bernstein polynomial
        :math:`B_{v,n}(x) = \\binom{n}{v}x^v(1 - x)^{n - v}`

    :raises: :exc:`ValueError` if *v* or *n* are negative or *v* is greater than *n*
    :rtype: :class:`pypol.Polynomial`

    **Examples**

    ::

        >>> bernstein(0, 0)
        + 1
        >>> bernstein(0, 1)
        - x + 1
        >>> bernstein(0, 2)
        + x^2 - 2x + 1
        >>> bernstein(1, 2)
        - 2x^2 + 2x
        >>> bernstein(-1, 2)
        Traceback (most recent call last):
          File "<pyshell#5>", line 1, in <module>
            bernstein(-1, 2)
          File "series.py", line 897, in bernstein
            raise ValueError('Bernstein polynomials only defined for v >= 0 and n >= 0')
        ValueError: Bernstein polynomials only defined for v >= 0 and n >= 0
        >>> bernstein(3, 2)
        Traceback (most recent call last):
          File "<pyshell#6>", line 1, in <module>
            bernstein(3, 2)
          File "series.py", line 899, in bernstein
            raise ValueError('v cannot be greater than n')
        ValueError: v cannot be greater than n
        >>> bernstein(3, 6)
        - 20x^6 + 60x^5 - 60x^4 + 20x^3
        >>> bernstein(13, 16)
        - 560x^16 + 1680x^15 - 1680x^14 + 560x^13
        >>> bernstein(18, 19)
        - 19x^19 + 19x^18

    **References**
    
    `MathWorld <http://mathworld.wolfram.com/BernsteinPolynomial.html>`_
    '''

    if v < 0 or n < 0:
        raise ValueError('Bernstein polynomials only defined for v >= 0 and n >= 0')
    if v > n:
        raise ValueError('v cannot be greater than n')
    if not v and not n:
        return ONE
    if v == n:
        return x ** v
    if n == (v + 1):
        return -n*x**n + n*x**v
    return bin_coeff(n, v) * x**v * (ONE - x) ** (n - v)
Ejemplo n.º 2
0
 def testBinCoeff(self):
     py.test.raises(ValueError, lambda: funcs.bin_coeff(1, 2))
     assert funcs.bin_coeff(2, 1) == 2
     assert funcs.bin_coeff(4, 3) == 4
     assert funcs.bin_coeff(12, 11) == 12
     assert funcs.bin_coeff(10, 3) == 120
     assert funcs.bin_coeff(20, 15) == 15504
     assert funcs.bin_coeff(49, 8) == 450978066
Ejemplo n.º 3
0
 def _sum(n):
     return sum((- 1) ** k * bin_coeff(n, k) * (x + k) ** m for k in xrange(n + 1))
Ejemplo n.º 4
0
 def b_c3():
     return bin_coeff(m + 3, m)
Ejemplo n.º 5
0
 def b_c(j):
     return bin_coeff(m + 3, m - 6 * j)
Ejemplo n.º 6
0
 def _sum(k):
     return sum((-1) ** v * fractions.Fraction.from_float(bin_coeff(k, v)) * fractions.Fraction(v ** m, k + 1) for v in xrange(k + 1))