예제 #1
0
def comb(N,k,exact=0):
    """Combinations of N things taken k at a time.

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:
      - Array arguments accepted only for exact=0 case.
      - If k > N, N < 0, or k < 0, then a 0 is returned.
    """
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0L
        val = 1L
        for j in xrange(min(k, N-k)):
            val = (val*(N-j))//(j+1)
        return val
    else:
        from scipy import special
        k,N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N+1) - lgam(N-k+1) - lgam(k+1))
        sv = special.errprint(sv)
        return where(cond, vals, 0.0)
예제 #2
0
def comb(N,k,exact=0):
    """Combinations of N things taken k at a time.

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:    
      - Array arguments accepted only for exact=0 case.
      - If k > N, N < 0, or k < 0, then a 0 is returned.
    """
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0L
        N,k = map(long,(N,k))
        top = N
        val = 1L
        while (top > (N-k)):
            val *= top
            top -= 1
        n = 1L
        while (n < k+1L):
            val /= n
            n += 1
        return val
    else:
        k,N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N+1) - lgam(N-k+1) - lgam(k+1))
        sv = special.errprint(sv)
        return where(cond, vals, 0.0)
예제 #3
0
def comb(N, k, exact=0):
    """Combinations of N things taken k at a time.

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:
      - Array arguments accepted only for exact=0 case.
      - If k > N, N < 0, or k < 0, then a 0 is returned.
    """
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0L
        val = 1L
        for j in xrange(min(k, N - k)):
            val = (val * (N - j)) // (j + 1)
        return val
    else:
        from scipy import special
        k, N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N + 1) - lgam(N - k + 1) - lgam(k + 1))
        sv = special.errprint(sv)
        return where(cond, vals, 0.0)
예제 #4
0
def factorial(n,exact=0):
    """n! = special.gamma(n+1)

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:    
      - Array argument accepted only for exact=0 case.
      - If n<0, the return value is 0.
    """
    if exact:
        if n < 0:
            return 0L
        n = long(n)
        val = 1L
        k = 1L
        while (k < n+1L):
            val = val*k
            k += 1
        return val
    else:
        n = asarray(n)
        sv = special.errprint(0)
        vals = special.gamma(n+1)
        sv = special.errprint(sv)
        return where(n>=0,vals,0)
예제 #5
0
 def wrapper(*a, **kw):
     old_filters = list(getattr(warnings, 'filters', []))
     old_errprint = sc.errprint(1)
     warnings.filterwarnings("error", category=sc.SpecialFunctionWarning)
     try:
         return func(*a, **kw)
     finally:
         sc.errprint(old_errprint)
         setattr(warnings, 'filters', old_filters)
예제 #6
0
def test_errprint():
    flag = sc.errprint(True)
    try:
        assert_(isinstance(flag, bool))
        with warnings.catch_warnings(record=True) as w:
            sc.loggamma(0)
            assert_(w[-1].category is sc.SpecialFunctionWarning)
    finally:
        sc.errprint(flag)
예제 #7
0
 def wrapper(*a, **kw):
     old_filters = list(getattr(warnings, 'filters', []))
     old_errprint = sc.errprint(1)
     warnings.filterwarnings("error", category=sc.SpecialFunctionWarning)
     try:
         return func(*a, **kw)
     finally:
         sc.errprint(old_errprint)
         setattr(warnings, 'filters', old_filters)
예제 #8
0
def test_errprint():
    flag = sc.errprint(True)
    try:
        assert_(isinstance(flag, bool))
        with warnings.catch_warnings(record=True) as w:
            sc.loggamma(0)
            assert_(w[-1].category is sc.SpecialFunctionWarning)
    finally:
        sc.errprint(flag)
예제 #9
0
def comb(N,k,exact=0):
    """
    The number of combinations of N things taken k at a time.

    This is often expressed as "N choose k".

    Parameters
    ----------
    N : int, ndarray
        Number of things.
    k : int, ndarray
        Number of elements taken.
    exact : int, optional
        If `exact` is 0, then floating point precision is used, otherwise
        exact long integer is computed.

    Returns
    -------
    val : int, ndarray
        The total number of combinations.

    Notes
    -----
    - Array arguments accepted only for exact=0 case.
    - If k > N, N < 0, or k < 0, then a 0 is returned.

    Examples
    --------
    >>> k = np.array([3, 4])
    >>> n = np.array([10, 10])
    >>> sc.comb(n, k, exact=False)
    array([ 120.,  210.])
    >>> sc.comb(10, 3, exact=True)
    120L

    """
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0
        val = 1
        for j in xrange(min(k, N-k)):
            val = (val*(N-j))//(j+1)
        return val
    else:
        from scipy import special
        k,N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N+1) - lgam(N-k+1) - lgam(k+1))
        sv = special.errprint(sv)
        return where(cond, vals, 0.0)
예제 #10
0
def comb(N, k, exact=0):
    """
    The number of combinations of N things taken k at a time.

    This is often expressed as "N choose k".

    Parameters
    ----------
    N : int, ndarray
        Number of things.
    k : int, ndarray
        Number of elements taken.
    exact : int, optional
        If `exact` is 0, then floating point precision is used, otherwise
        exact long integer is computed.

    Returns
    -------
    val : int, ndarray
        The total number of combinations.

    Notes
    -----
    - Array arguments accepted only for exact=0 case.
    - If k > N, N < 0, or k < 0, then a 0 is returned.

    Examples
    --------
    >>> k = np.array([3, 4])
    >>> n = np.array([10, 10])
    >>> sc.comb(n, k, exact=False)
    array([ 120.,  210.])
    >>> sc.comb(10, 3, exact=True)
    120L

    """
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0
        val = 1
        for j in xrange(min(k, N - k)):
            val = (val * (N - j)) // (j + 1)
        return val
    else:
        from scipy import special
        k, N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N + 1) - lgam(N - k + 1) - lgam(k + 1))
        sv = special.errprint(sv)
        return where(cond, vals, 0.0)
예제 #11
0
def test_errprint():
    with suppress_warnings() as sup:
        sup.filter(DeprecationWarning, "`errprint` is deprecated!")
        flag = sc.errprint(True)
    try:
        assert_(isinstance(flag, bool))
        with warnings.catch_warnings(record=True) as w:
            sc.loggamma(0)
            assert_(w[-1].category is sc.SpecialFunctionWarning)
    finally:
        with suppress_warnings() as sup:
            sup.filter(DeprecationWarning, "`errprint` is deprecated!")
            sc.errprint(flag)
예제 #12
0
def test_errprint():
    with suppress_warnings() as sup:
        sup.filter(DeprecationWarning, "`errprint` is deprecated!")
        flag = sc.errprint(True)

    try:
        assert_(isinstance(flag, bool))
        with pytest.warns(sc.SpecialFunctionWarning):
            sc.loggamma(0)
    finally:
        with suppress_warnings() as sup:
            sup.filter(DeprecationWarning, "`errprint` is deprecated!")
            sc.errprint(flag)
예제 #13
0
def factorial(n,exact=0):
    """
    The factorial function, n! = special.gamma(n+1).

    If exact is 0, then floating point precision is used, otherwise
    exact long integer is computed.

    - Array argument accepted only for exact=0 case.
    - If n<0, the return value is 0.

    Parameters
    ----------
    n : int or array_like of ints
        Calculate ``n!``.  Arrays are only supported with `exact` set
        to False.  If ``n < 0``, the return value is 0.
    exact : bool, optional
        The result can be approximated rapidly using the gamma-formula
        above.  If `exact` is set to True, calculate the
        answer exactly using integer arithmetic. Default is False.

    Returns
    -------
    nf : float or int
        Factorial of `n`, as an integer or a float depending on `exact`.

    Examples
    --------
    >>> arr = np.array([3,4,5])
    >>> sc.factorial(arr, exact=False)
    array([   6.,   24.,  120.])
    >>> sc.factorial(5, exact=True)
    120L

    """
    if exact:
        if n < 0:
            return 0L
        val = 1L
        for k in xrange(1,n+1):
            val *= k
        return val
    else:
        from scipy import special
        n = asarray(n)
        sv = special.errprint(0)
        vals = special.gamma(n+1)
        sv = special.errprint(sv)
        return where(n>=0,vals,0)
예제 #14
0
파일: common.py 프로젝트: alexleach/scipy
def factorial(n,exact=0):
    """
    The factorial function, n! = special.gamma(n+1).

    If exact is 0, then floating point precision is used, otherwise
    exact long integer is computed.

    - Array argument accepted only for exact=0 case.
    - If n<0, the return value is 0.

    Parameters
    ----------
    n : int or array_like of ints
        Calculate ``n!``.  Arrays are only supported with `exact` set
        to False.  If ``n < 0``, the return value is 0.
    exact : bool, optional
        The result can be approximated rapidly using the gamma-formula
        above.  If `exact` is set to True, calculate the
        answer exactly using integer arithmetic. Default is False.

    Returns
    -------
    nf : float or int
        Factorial of `n`, as an integer or a float depending on `exact`.

    Examples
    --------
    >>> arr = np.array([3,4,5])
    >>> sc.factorial(arr, exact=False)
    array([   6.,   24.,  120.])
    >>> sc.factorial(5, exact=True)
    120L

    """
    if exact:
        if n < 0:
            return 0L
        val = 1L
        for k in xrange(1,n+1):
            val *= k
        return val
    else:
        from scipy import special
        n = asarray(n)
        sv = special.errprint(0)
        vals = special.gamma(n+1)
        sv = special.errprint(sv)
        return where(n>=0,vals,0)
예제 #15
0
def comb(N, k, exact = 0):
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0
        val = 1
        for j in xrange(min(k, N-k)):
            val = (val*(N-j))//(j+1)
        return val
    else:
        from scipy import special
        
        k,N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N + 1) - lgam(N - k + 1) - lgam(k + 1))
        sv = special.errprint(sv)
        
        return where(cond, vals, 0.0)
예제 #16
0
def comb(N, k, exact=0):
    if exact:
        if (k > N) or (N < 0) or (k < 0):
            return 0
        val = 1
        for j in xrange(min(k, N - k)):
            val = (val * (N - j)) // (j + 1)
        return val
    else:
        from scipy import special

        k, N = asarray(k), asarray(N)
        lgam = special.gammaln
        cond = (k <= N) & (N >= 0) & (k >= 0)
        sv = special.errprint(0)
        vals = exp(lgam(N + 1) - lgam(N - k + 1) - lgam(k + 1))
        sv = special.errprint(sv)

        return where(cond, vals, 0.0)
예제 #17
0
def factorial(n, exact=0):
    """n! = special.gamma(n+1)

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:
      - Array argument accepted only for exact=0 case.
      - If n<0, the return value is 0.
    """
    if exact:
        if n < 0:
            return 0L
        val = 1L
        for k in xrange(1, n + 1):
            val *= k
        return val
    else:
        from scipy import special
        n = asarray(n)
        sv = special.errprint(0)
        vals = special.gamma(n + 1)
        sv = special.errprint(sv)
        return where(n >= 0, vals, 0)
예제 #18
0
def factorial(n,exact=0):
    """n! = special.gamma(n+1)

    If exact==0, then floating point precision is used, otherwise
    exact long integer is computed.

    Notes:
      - Array argument accepted only for exact=0 case.
      - If n<0, the return value is 0.
    """
    if exact:
        if n < 0:
            return 0L
        val = 1L
        for k in xrange(1,n+1):
            val *= k
        return val
    else:
        from scipy import special
        n = asarray(n)
        sv = special.errprint(0)
        vals = special.gamma(n+1)
        sv = special.errprint(sv)
        return where(n>=0,vals,0)
예제 #19
0
def beambeam_Gaussian_grid(sigmax,
                           sigmay,
                           rangex=10.0,
                           rangey=10.0,
                           epsilon=1e-3,
                           gridpoints=10,
                           sdds_file=None):

    if abs(sigmax - sigmay) / (sigmax + sigmay) < epsilon:
        xlist = np.linspace(-rangex * sigmax,
                            rangex * sigmax,
                            num=int(rangex * gridpoints) + 1,
                            endpoint=True)
        ylist = np.linspace(-rangey * sigmay,
                            rangey * sigmay,
                            num=int(rangey * gridpoints) + 1,
                            endpoint=True)
        xlist, ylist = np.meshgrid(xlist, ylist)
        sigr = (sigmax + sigmay) / 2.0
        rabs = np.sqrt(xlist * xlist + ylist * ylist)
        mask = (rabs == 0)
        invmask = np.logical_not(mask)
        eabs = np.zeros_like(rabs)
        ex = np.zeros_like(rabs)
        ey = np.zeros_like(rabs)
        eabs[invmask] = (1 - np.exp(-rabs[invmask] * rabs[invmask] / 2 / sigr /
                                    sigr)) / rabs[invmask] / 2 / np.pi
        ex[invmask] = eabs[invmask] * xlist[invmask] / rabs[invmask]
        ey[invmask] = eabs[invmask] * ylist[invmask] / rabs[invmask]
        eabs[mask] = 0
        ex[mask] = 0
        ey[mask] = 0

    else:
        xlist = np.linspace(-rangex * sigmax,
                            rangex * sigmax,
                            num=int(rangex * gridpoints) + 1,
                            endpoint=True)
        ylist = np.linspace(-rangey * sigmay,
                            rangey * sigmay,
                            num=int(rangey * gridpoints) + 1,
                            endpoint=True)

        spefunc.errprint(1)
        if sigmax < sigmay:
            su = sigmay
            sv = sigmax
            ulist = ylist
            vlist = xlist
            ulist, vlist = np.meshgrid(ulist, vlist)
        else:
            su = sigmax
            sv = sigmay
            ulist = xlist
            vlist = ylist
            ulist, vlist = np.meshgrid(ulist, vlist)

        xmask = ulist > 0
        ymask = vlist > 0
        xmask = xmask * 2.0 - 1.0
        ymask = ymask * 2.0 - 1.0
        sqrtsigma = math.sqrt(2 * su * su - 2 * sv * sv)
        w1 = spefunc.wofz((np.abs(ulist) + 1j * np.abs(vlist)) / sqrtsigma)
        w2 = spefunc.wofz(
            (np.abs(ulist) * sv / su + 1j * np.abs(vlist) * su / sv) /
            sqrtsigma)
        expterm = np.exp(-ulist * ulist / 2.0 / su / su -
                         vlist * vlist / 2.0 / sv / sv)
        ecompx = (w1 - expterm * w2) / math.sqrt(
            np.pi) / 2.0 / 1.0j / sqrtsigma
        eu = np.real(ecompx)
        ev = -np.imag(ecompx)
        if sigmax < sigmay:
            ex = ev * xmask
            ey = eu * ymask
            xlist = vlist
            ylist = ulist
        else:
            ex = eu * xmask
            ey = ev * ymask
            xlist = ulist
            ylist = vlist
    #if sdds_file is not None:
    #    import SDDSIO.sdds as sdds
    #    output=sdds.SDDS(0)
    #    output.columnName=['x','y','Fx','Fy']
    #    output.columnDefinition[['', 'm', 'x', '', output.SDDS_DOUBLE, 0L],
    #                            ['', 'm', 'y', '', output.SDDS_DOUBLE, 0L],
    #                            ['', '', 'Fx', '', output.SDDS_DOUBLE, 0L],
    #                            ['', '', 'Fy', '', output.SDDS_DOUBLE, 0L],
    #    ]
    #    output.columnData[[xlist.tolist(),],[ylist.tolist(),][ex.tolist(),][ey.tolist(),]]
    #    output.save(sdds_file)
    return xlist, ylist, ex, ey