- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果在 cython
中定义了一个指针 vector ,那么与 python 中的 enumerate
类似的函数或过程是什么,用于遍历指针数组中元素的索引和值在 C 声明类型函数内的循环中?
测试.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)
最佳答案
一个简单的 for 循环就足够了,就像这样:
for(int i = 0; i < vecSize; ++i)
printf("Index = %d, Value = %d\n", i, vec[i]);
其中 i
是当前索引,vec[i]
是当前元素的值。
PS:我假设 vec
存储此示例的 int
及其大小(如果为 vecSize
)。
关于python - cython中 "enumerate"等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46318475/
c 不做边界检查。那么cython是如何检查是否编译成c的呢? %%cython --annotate cimport cython @cython.boundscheck(True) cpdef m
可以直接声明用于 Cython 构造函数? 据我了解,这是可能的: # Cython cdef int[3] li = [1, 2, 3] # C++ int[3] li = {1, 2, 3} 但
所以,如果你有一个头文件。 %%file test.h struct mystruct{ int i; int j; }; 然后你将它包装在 Cython 中: cdef extern fr
我正在构建一个独立于平台的 cython 项目,我想根据正在使用的编译器传递编译器参数。我可以猜测基于平台的编译器,或者假设它与用于 Python 的编译器相同,但不能保证匹配。通常我注入(injec
我使用诗歌构建我的 cython 包。我在所有函数和类中都有 NumPy 风格的文档字符串。我现在要做的是添加 Sphinx 自动文档并发布在 Read the Docs。 我已阅读此主题 How d
赛通 libcpp模块包含 priority_queue 的模板,这很好,除了一件事:我不能通过自定义比较器(或者,至少,我不知道如何)。 我需要这个,因为我需要 priority_queue做一个a
以下代码定义了一个简单的 Cython 函数(为方便起见,使用 Ipython 魔法)。 %load_ext cython %%cython def f(float x, float y=2):
我正在尝试使用 cython 进行复数计算。在示例代码中,我想计算复数的复指数函数。问题是我不知道如何将我的整数乘以虚数单位。python的虚数单位1.0j乘以cython执行时报错。 这是我的代码:
在这里停留在一些基本的 Cython 上 - 在 Cython 中定义字符串数组的规范且有效的方法是什么? 具体来说,我想定义一个定长常量数组char . (请注意,此时我不想引入 NumPy。) 在
是否有可能,如果是,如何确定 Cython 中整数数据类型的大小(以位为单位)? 我正在尝试做这样的事情,以获得整数大小: cdef WORD_BITS = 0 IF sizeof(unsigned
我只是想打印 cython 变量的地址,但我无法绕过错误消息: cdef int myvar print &myvar 抛出 Cannot convert 'int *' to Python obje
我有一个 C 头文件,它在宏中定义了一个函数。我需要从 Cython 调用它。有没有办法在 Cython 中使用宏并使其完全扩展?我已经有了 C 类型的参数。 我尝试像使用函数一样使用 cdef,我认
令人惊讶的是,我似乎找不到通过名称获取结构体元素的单个示例(无论是在网络上还是在 cython 示例中)。 所以我收到了一个指向 C 函数结构体的指针,并且想要一一访问这些元素并将它们重新打包到 py
我尝试围绕 C++ 库编写一个 Cython 包装器 http://primesieve.org/ 它包装了一个函数count。到目前为止,它可以正确安装 python setup.py instal
我正在尝试将 cython 模块 data.pyx 导入另一个 cython 模块 user.pyx。一切都编译得很好,但是当我尝试在 python 模块中调用 user.pyx 时,我收到错误“Im
更新:内存 View 获胜。Cython 使用类型化内存 View :0.0253449 特别感谢 lothario,他指出了几个关键的变化。 荒谬。当然现在的问题是,似乎不能对它们做太多算术(加法和
我有一个使用 memoryview 数组的 cython 模块,即... double[:,:] foo 我想使用多处理并行运行这个模块。但是我得到了错误: PicklingError: Can't
我正在尝试使用 Cython 加速 PEP 484 类型的 python 脚本。我想保持一些语义和可读性。 之前,我有一个 Flags = int def difference(f1: Flags,
这个问题已经有答案了: Collapse multiple submodules to one Cython extension (5 个回答) 已关闭 3 年前。 我在一个包中有多个 .py 文件
我已经能够在我的 .pyx 脚本上使用 cython 在 linux 上创建一个 .so 文件。我也可以成功地在我的 python 解释器上进行导入。 我的问题是如何在不使用 cython 的情况下将
我是一名优秀的程序员,十分优秀!