Cython and numpy: 'cfunc.to_py: 65: 25:' ndarray 'is not an identifier of type'

I have a cdef function that takes two numpy.ndarrays arguments as argument (1). This gives me "cfunc.to_py: 65: 25:" ndarray "is not a type identifier error. When I replace cdef with def (python) (2) or cpdef (3), the error disappears. Is this an error? How can I do this solve the code below.

(1) cdef float F(np.ndarray xx, np.ndarray y_non_labor, float wage, float gamma, float one_by_gamma, float l, float one_minus_l, int lenx):

(2) def F(np.ndarray xx, np.ndarray y_non_labor, float wage, float gamma, float one_by_gamma, float l, float one_minus_l, int lenx):

(3) cpdef float F(np.ndarray xx, np.ndarray y_non_labor, float wage, float gamma, float one_by_gamma, float l, float one_minus_l, int lenx):

# cython: profile=True
# cython: boundscheck=False
# cython: wraparound=False
cimport cython
from libc.stdlib cimport malloc
from scipy.optimize import minimize
cimport numpy as np
import numpy as np


# due to instabilities associated with "constraint optimization" with bounds
# we will transform the problem so that that it becomes an unconstraint optimization problem without bounds

def transform(x):
    x1 = x * x / (1 + x * x)
    s =  1 - sum(x1)
    return x1, s

# on the next cython update try cdef float F(..)
@cython.cdivision(True)
cdef float F(np.ndarray xx, np.ndarray y_non_labor, float wage, float gamma, float one_by_gamma, float l, float one_minus_l, int lenx):
    cdef float s
    cdef float *z_non_labor = <float *>malloc(lenx * sizeof(float))
    cdef float z_labor
    cdef float labor
    cdef float value
    cdef float summation
    cdef float non_labor
    cdef float x
    cdef float ynl

    for i from 0 <= i < lenx:
        x = xx[i]
        z_non_labor[i] = x * x / (1 + x * x)

    s = 0
    for i from 0 <= i < lenx:
        s += z_non_labor[i]

    z_labor = 1 - s

    summation = 0
    for i from 0 <= i < lenx:
        ynl = y_non_labor[i]
        summation += (z_non_labor[i] / ynl) ** gamma

    non_labor = summation ** one_by_gamma
    labor = z_labor / wage
    # labor and non-labor inputs together
    value = (labor ** l) * (non_labor ** one_minus_l)
    return - value

def optimization(np.ndarray seed_weights,
                 np.ndarray input_prices,
                 float wage,
                 float gamma,
                 float one_by_gamma,
                 float l,
                 float one_minus_l):

    cdef int lenx

    lenx = len(seed_weights)

    args=(input_prices,
          wage,
          gamma,
          one_by_gamma,
          l,
          one_minus_l,
          lenx)

    return minimize(F, seed_weights, args=args, method='Nelder-Mead')
+4
source share
1 answer

I think this is a rather obscure mistake. Explanation follows. The work round is at the end of the answer.

In Cython 0.21, I get another error

filename.pyx:73:21: Cannot convert 'float (ndarray, ndarray, float,
float, float, float, float, int)' to Python object.

, minimise Python , cdef C. [cpdef C, ]

Cython 0.22 0.23 .

@cname("__Pyx_CFunc_float____ndarray____ndarray____float____float____float____float____float____int___to_py")
cdef object __Pyx_CFunc_float____ndarray____ndarray____float____float____float____float____float____int___to_py(float (*f)(ndarray, ndarray, float, float, float, float, float, int) except *):
    def wrap(ndarray xx, ndarray y_non_labor, float wage, float gamma, float one_by_gamma, float l, float one_minus_l, int lenx):
                        ^
------------------------------------------------------------

cfunc.to_py:30:25: 'ndarray' is not a type identifier

, wrap, . wrap , , minimize, , Python , def ( minimize ). , -, , Cython 0.22.

, wrap ndarray, np.ndarray, ( !). ,

from numpy cimport ndarray

( ndarray ).

, minimize Python , cpdef ( Python , - ). cpdef - - C, Python ( ).

+11

Source: https://habr.com/ru/post/1605649/


All Articles