2017-09-20 17 views
0

ポインタベクトルをcythonに定義すると、のループ内のポインタ配列の要素のインデックスと値を両方とも通過するPythonのenumerateと同様の関数またはプロシージャとは何ですか? C型宣言された関数?cythonの "enumerate"の等価性

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

最初のステップはインデックス 'i'でポインタを得ることです。 –

+0

forループオーバーインデックス? – Netwave

+0

@DanielSanchezループ内のポインタ配列の値とインデックスの両方を取得したい。 – Dalek

答えて

3

十分でループのための簡単な、:iは電流が

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

をインデックス、vec[i]は、現在の要素の値です。

PS:vecSizeの場合、vecはこの例ではintを格納しているとします。