• <bdo id='KbJQy'></bdo><ul id='KbJQy'></ul>
    <i id='KbJQy'><tr id='KbJQy'><dt id='KbJQy'><q id='KbJQy'><span id='KbJQy'><b id='KbJQy'><form id='KbJQy'><ins id='KbJQy'></ins><ul id='KbJQy'></ul><sub id='KbJQy'></sub></form><legend id='KbJQy'></legend><bdo id='KbJQy'><pre id='KbJQy'><center id='KbJQy'></center></pre></bdo></b><th id='KbJQy'></th></span></q></dt></tr></i><div id='KbJQy'><tfoot id='KbJQy'></tfoot><dl id='KbJQy'><fieldset id='KbJQy'></fieldset></dl></div>
  1. <legend id='KbJQy'><style id='KbJQy'><dir id='KbJQy'><q id='KbJQy'></q></dir></style></legend>

    <small id='KbJQy'></small><noframes id='KbJQy'>

  2. <tfoot id='KbJQy'></tfoot>

    1. Swig,返回一个双精度数组

      时间:2023-09-02

      • <bdo id='fXcL3'></bdo><ul id='fXcL3'></ul>
      • <legend id='fXcL3'><style id='fXcL3'><dir id='fXcL3'><q id='fXcL3'></q></dir></style></legend>

        • <small id='fXcL3'></small><noframes id='fXcL3'>

          <i id='fXcL3'><tr id='fXcL3'><dt id='fXcL3'><q id='fXcL3'><span id='fXcL3'><b id='fXcL3'><form id='fXcL3'><ins id='fXcL3'></ins><ul id='fXcL3'></ul><sub id='fXcL3'></sub></form><legend id='fXcL3'></legend><bdo id='fXcL3'><pre id='fXcL3'><center id='fXcL3'></center></pre></bdo></b><th id='fXcL3'></th></span></q></dt></tr></i><div id='fXcL3'><tfoot id='fXcL3'></tfoot><dl id='fXcL3'><fieldset id='fXcL3'></fieldset></dl></div>
            <tbody id='fXcL3'></tbody>

                <tfoot id='fXcL3'></tfoot>
                本文介绍了Swig,返回一个双精度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我知道,通常有很多方法可以解决某些问题.但是在这里我知道我想要哪种方式,但我无法使其与 Python 和 SWIG 一起使用......

                我有一个 C 函数,它返回一个双精度值数组:

                double *my(int x){双 a,b,*buf;buf = malloc (x * sizeof(double));a=3.14;b=2.7;缓冲区[0]=一个;buf[1]=b;返回缓冲区;}

                在这里,我绝对希望将数组作为返回值.不像许多示例中的void"函数,它写入输入数组.现在,我想获得一个 SWIG-python 包装器,它可以用作:

                >>>导入示例>>>打印 example.my(7)[3.14,2.7]

                无论我做什么,我都会遇到一些概念上的问题 - 我总是会遇到一些问题.比如 <Swig Object of type 'double *' at 0xFABCABA12>

                我试图在我的 swg 文件中定义一些类型映射:

                %typemap(out) double [ANY] {诠释我;$result = PyList_New($1_dim0);对于 (i = 0; i < $1_dim0; i++) {PyObject *o = PyFloat_FromDouble((double) $1[i]);PyList_SetItem($result,i,o);}}

                但我仍然无法按要求得出我的结果.有没有人有一个简单的代码示例来完成这个任务?

                解决方案

                第一个问题是你的typemap不匹配,你需要一个%typemap(out) double * { ... } 因为你的函数返回一个指向 double 而不是 double 数组的指针.

                如果您的列表是固定大小的(即整数文字),如您给出的示例(我认为这不是您想要的),您可以简单地更改我上面给出的类型映射并交换 $1_dim0 为固定大小.

                否则你的问题是你的 %typemap(out) double * 不可能知道你的参数 int x 的值.您可以返回一个包含指针和大小的结构.然后您可以轻松定义类型映射以将其转换为列表(或 NumPy 数组,另请参阅我对 用数组成员包装 C 结构以便在 python 中访问:SWIG?cython?ctypes?).

                顺便说一句,不可能在 C 中返回固定大小的数组(另请参阅此答案:声明一个 C 函数返回一个数组),所以 %typemap(out) double [ANY] { ... } 永远不会匹配.p>

                I know, there are often many ways to solve certain problems. But here I know which way I want to have it, but I am unable to make it work with Python and SWIG...

                I have a C-function, which returns me an array of double values:

                double *my(int x)
                {
                   double a,b,*buf;
                   buf = malloc (x * sizeof(double));
                   a=3.14;
                   b=2.7;
                   buf[0]=a;
                   buf[1]=b;
                   return buf;
                }
                

                Here, I definitively want to have the array as a return value. Not, as in many examples a 'void' function, which writes into an input array. Now, I would like to get a SWIG-python wrapper, which might be used as:

                >>> import example
                >>> print example.my(7)
                [3.14,2.7]
                

                Whatever I do, I have some conceptual problems here - I always get s.th. like <Swig Object of type 'double *' at 0xFABCABA12>

                I tried to define some typemaps in my swg file:

                %typemap(out) double [ANY] {
                  int i;
                  $result = PyList_New($1_dim0);
                  for (i = 0; i < $1_dim0; i++) {
                    PyObject *o = PyFloat_FromDouble((double) $1[i]);
                    PyList_SetItem($result,i,o);
                  }
                }
                

                But still I am unable to get out my results as required. Does anyone have a simple code-example to achieve this task?

                解决方案

                The first problem is that your typemap doesn't match, you'll need a %typemap(out) double * { ... } since your function returns a pointer to double and not a double array.

                If your list is of fixed size (i.e. an integer literal) as in the example you gave (which I assume is not what you want) you could simply change the typemap as I gave above and exchange $1_dim0 for the fixed size.

                Otherwise your problem is that your %typemap(out) double * cannot possibly know the value of your parameter int x. You could return a struct that carries both the pointer and the size. Then you can easily define a typemap to turn that into a list (or a NumPy array, see also my response to Wrap C struct with array member for access in python: SWIG? cython? ctypes?).

                Incidentally it's not possible to return a fixed sized array in C (see also this answer: Declaring a C function to return an array), so a %typemap(out) double [ANY] { ... } can never match.

                这篇关于Swig,返回一个双精度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在不使用请求上下文的情况下在烧瓶中呈现模板 下一篇:Python:为什么“回归"?不会在简单的 for 循环和“打印"中打印出所有列表元素.会做的?

                相关文章

                <small id='PZku1'></small><noframes id='PZku1'>

                <i id='PZku1'><tr id='PZku1'><dt id='PZku1'><q id='PZku1'><span id='PZku1'><b id='PZku1'><form id='PZku1'><ins id='PZku1'></ins><ul id='PZku1'></ul><sub id='PZku1'></sub></form><legend id='PZku1'></legend><bdo id='PZku1'><pre id='PZku1'><center id='PZku1'></center></pre></bdo></b><th id='PZku1'></th></span></q></dt></tr></i><div id='PZku1'><tfoot id='PZku1'></tfoot><dl id='PZku1'><fieldset id='PZku1'></fieldset></dl></div>
                  <tfoot id='PZku1'></tfoot>

                      <bdo id='PZku1'></bdo><ul id='PZku1'></ul>
                    <legend id='PZku1'><style id='PZku1'><dir id='PZku1'><q id='PZku1'></q></dir></style></legend>