cython中“枚举”的等价性

时间:2017-09-20 09:37:44

标签: python c loops vector cython

如果在cython中定义一个指针向量,那么与python中的enumerate类似的函数或过程是什么,以便在一个循环内的循环中遍历指针数组中的元素的索引和值strong> C 声明类型函数?

test.pyx

#cython: wraparound=False
#cython: boundscheck=False
#cython: cdivision=True
#cython: nonecheck=False
from cpython cimport array
import cython
import numpy as np
import ctypes
cimport numpy as np
cdef extern from "math.h":
     cpdef double log(double x)
     cpdef double exp(double x)

def void initial(int *ns, int *m, double *emax, double* x, double* hx, double*
        hpx, int *lb, double *xlb, int *ub, double *xub, int* ifault, int* iwv,
        double* rwv):
      cdef int nn, ilow, ihigh, i
      cdef int iipt, iz, ihuz, iscum, ix, ihx, ihpx
      cdef bint horiz
      cdef double hulb, huub, eps, cu, alcu, huzmax

      eps = exp(-emax[0])
      ifault[0] = 0
      ilow = 0
      ihigh = 0
      nn = ns[0]+1

      if (m[0] < 1):
         ifault[0] = 1

      huzmax = hx[0]
      if not ub[0]:
         xub[0] = 0.0

      if not lb[0]:
         xlb[0] = 0.0

      hulb = (xlb[0]-x[0])*hpx[0] + hx[0]
      huub = (xub[0]-x[0])*hpx[0] + hx[0]
      if (ub[0] and lb[0]):
         huzmax = max(huub, hulb)
          cu = exp((huub+hulb)*0.5-huzmax)*(xub[0]-xlb[0])  
      else:
         cu = 0.0
         if (m[0] < 2):
             ifault[0] = 1

      if (cu > 0.0):
          alcu = log(cu)
      #set pointers
      iipt = 5
      iz = 8
      ihuz = nn+iz
      iscum = nn+ihuz
      ix = nn+iscum
      ihx = nn+ix
      ihpx = nn+ihx
      iwv[0] = ilow
      iwv[1] = ihigh
      iwv[2] = ns[0]
      iwv[3] = 1
      if lb[0]:
         iwv[4] = 1
      else:
         iwv[4] = 0

      if ub[0]:
         iwv[5] = 1
      else:
         iwv[5] = 0

      if ( ns[0] < m[0]):
         ifault[0] = 2

      iwv[iipt+1] = 0
      rwv[0] = hulb
      rwv[1] = huub
      rwv[2] = emax[0]
      rwv[3] = eps
      rwv[4] = cu
      rwv[5] = alcu
      rwv[6] = huzmax
      rwv[7] = xlb[0]
      rwv[8] = xub[0]
      rwv[iscum+1] = 1.0
      for i from 0 <= i < m[0]:
         rwv[ix+i] = x[i]
         rwv[ihx+i] = hx[i]
         rwv[ihpx+i] = hpx[i]
      i = 0
      while (i < m[0]):
            update(&iwv[3], &iwv[0], &iwv[1], &iwv[iipt+1], &rwv[iscum+1], &rwv[4],
                    &rwv[ix+1], &rwv[ihx+1], &rwv[ihpx+1], &rwv[iz+1],
                    &rwv[ihuz+1], &rwv[6], &rwv[2], lb, &rwv[7], &rwv[0], ub,
                    &rwv[8], &rwv[1], ifault, &rwv[3], &rwv[5])
            i = iwv[3]

def void update(int *n, int *ilow, int *ihigh, int* ipt, double* scum, double
        *cu, double* x, double* hx, const double* hpx, double* z, double* huz,
        double *huzmax, double *emax, int *lb, double *xlb, double *hulb, int *ub,
        double *xub, double *huub, int* ifault, double *eps, double *alcu):


      n[0] = n[0]+1
      print "number of points defining the hulls", n[0]
      print " values of x: " , x[0],x[1],x[n[0]] 
      print "index of the smallest x(i)", ilow[0] 
      print " values of x: " ,x[ilow[0]]                     
      print "Update z,huz and ipt   "                              

def foo(int ns, int m, double emax,
         np.ndarray[ndim=1, dtype=np.float64_t] x,
         np.ndarray[ndim=1, dtype=np.float64_t] hx,
         np.ndarray[ndim=1, dtype=np.float64_t] hpx,
         int num):

    cdef np.ndarray[ndim=1, dtype=np.float64_t] rwv
    cdef np.ndarray[ndim=1, dtype=np.int64_t] iwv
    # initializing arrays
    rwv = np.zeros(ns*6+15, dtype=np.float64)
    iwv = np.zeros(ns+7, dtype=np.int64)        
    cdef double xlb = np.min(x)
    cdef double xub = np.max(x)
    cdef int lb=0
    cdef int ub=0
    cdef int ifault = 0
    cdef double beta = 0.

    initial(&ns, &m, &emax,
            &x[0], 
            &hx[0], 
            &hpx[0], 
            &lb, 
            &xlb,
            &ub, 
            &xub, 
            &ifault, 
            <int *>(&iwv[0]), 
            &rwv[0] 
            )

python代码

import numpy as np
from test import foo

m = 3
ns = 100
emax = 64

x = np.zeros(10, float)
hx = np.zeros(10, float)
hpx = np.zeros(10, float)

x[0] = 0
x[1] = 1.0
x[2] = -1.0
print x
def normal(x):
    return -x*x*0.5,-x

hx[0], hpx[0] = normal(x[1])
hx[1], hpx[1] = normal(x[2])
hx[2], hpx[2] = normal(x[3])
print hpx


num = 20
foo(ns, m, emax, x, hx, hpx, num)

1 个答案:

答案 0 :(得分:3)

一个简单的for循环就足够了,就像这样:

for(int i = 0; i < vecSize; ++i)
    printf("Index = %d, Value = %d\n", i, vec[i]);

其中i是当前索引,vec[i]是当前元素的值。

PS:我假设vec为此示例存储int,如果vecSize则存储其大小。