Python gsl_vector_set

时间:2018-08-13 14:33:52

标签: python ctypes gsl

我正在尝试使用gsl_vector_set()初始化内存中的两个向量。在主代码中,默认情况下将其初始化为零,但是我想将它们初始化为一些非零值。我基于使用gsl_vector_set()函数的工作函数编写了测试代码。

from ctypes import *;

gsl = cdll.LoadLibrary('libgsl-0.dll');
gsl.gsl_vector_get.restype = c_double;
gsl.gsl_matrix_get.restype = c_double;
gsl.gsl_vector_set.restype = c_double;

foo = dict(
    x_ht = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
            0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
    x_ht_m = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
              0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
    );
for f in range(0,18):
    gsl.gsl_vector_set(foo['x_ht_m'],f,c_double(1.0));
    gsl.gsl_vector_set(foo['x_ht'],f,c_double(1.0));

运行代码时出现此错误。

ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1

我是使用ctypes和gsl函数的新手,所以我不确定问题是什么或错误消息的含义。我也不确定是否有更好的方法将向量保存到内存中

1 个答案:

答案 0 :(得分:0)

感谢@CristiFati指出我的测试代码中需要gsl_vector_calloc。我注意到在主代码中,我需要设置的向量是

NAV.KF_dictnry['x_hat_m'] 

代替

NAV.KF_dictnry['x_ht_m']

因此,我通过创建一个包含字典的类来固定测试代码以更好地镜像实际代码,并添加了将向量中的每个值更改为任意值的功能。

from ctypes import *;
gsl = cdll.LoadLibrary('libgsl-0.dll');
gsl.gsl_vector_get.restype = c_double;
gsl.gsl_matrix_get.restype = c_double;
gsl.gsl_vector_set.restype = c_double;
class foo(object):
    fu = dict(
        x_hat = gsl.gsl_vector_calloc(c_size_t(18)),
        x_hat_m = gsl.gsl_vector_calloc(c_size_t(18)),
        );
x_ht = [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
        1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
x_ht_m = [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,
          1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
for f in range(0,18):
    gsl.gsl_vector_set(foo.fu['x_hat_m'],f,c_double(x_ht_m[f]));
    gsl.gsl_vector_set(foo.fu['x_hat'],f,c_double(x_ht[f]));

运行后,我检查了:

gsl.gsl_vector_get(foo.fu['x_hat_m'],0)

得出1.0(适用于整个矢量)。

原来是我这方面的一些愚蠢错误。

再次感谢!