수색…


사용자 정의 C 라이브러리의 함수 사용

사용자 정의 배포판에서 난수를 생성하는 my_random 이라는 C 라이브러리가 있습니다. set_seed(long seed)rand() (그리고 더 많은 것들을 필요로하지 않는다 set_seed(long seed) 와 같이 우리가 사용하기를 원하는 두 가지 기능을 제공한다. Cython에서 이들을 사용하려면

  1. .pxd 파일에 인터페이스를 정의하고
  2. .pyx 파일에서 함수를 호출하십시오.

암호

test_extern.pxd

# extern blocks define interfaces for Cython to C code
cdef extern from "my_random.h":
    double rand()
    void c_set_seed "set_seed" (long seed) # rename C version of set_seed to c_set_seed to avoid naming conflict

test_extern.pyx

def set_seed (long seed):
    """Pass the seed on to the c version of set_seed in my_random."""
    c_set_seed(seed)

cpdef get_successes (int x, double threshold):
    """Create a list with x results of rand <= threshold
    
    Use the custom rand function from my_random.
    """
    cdef:
        list successes = []
        int i
    for i in range(x):
        if rand() <= threshold:
            successes.append(True)
        else:
            successes.append(False)
    return successes


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow