2017-03-01 61 views
1

SciPy的documentation爲nquad規定,用於集成C函數的形式如何知道由nquad調用的C函數中的維數?

f(int n, double args[n]) 
where n is the number of extra parameters and args is an array of doubles of the additional parameters. 

因此,如何C函數應該知道有多少維正被集成,以使用args正確數量的?

如果我修改一般documentation用於C函數:

#include "stdio.h" 
double f(int n, double args[]) { 
    (void)args; 
    printf("%i\n", n); 
    return 0; 
} 

gcc -fPIC -shared func.c -o func.so 

編譯和運行這個Python程序:

#!/usr/bin/env python3 
import ctypes 
from scipy.integrate import nquad 
lib = ctypes.CDLL('func.so') 
func = lib.f 
func.restype = ctypes.c_double 
func.argtypes = (ctypes.c_int, ctypes.c_double) 
print(nquad(func, [[0, 1]])) 

我得到之間的值。 32764和32767在64位fedora 25上的n,而在32位的fedora 25上我得到0.在上面的鏈接中,c函數on不檢查n的值,但使用args [0] ... args [2]所以沒有辦法知道有多少維被集成?

調用nquad有:

print(nquad(func, [[0, 1]], args = [1,2,3])) 

反而不會改變被印在64位系統,即便N應該是不同的。我正在使用

gcc (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1) 
Python 3.5.2 
scipy 0.18.0 

回答

0

我閱讀文檔(您的第一個鏈接)。我覺得他們說的是,這不是調用func(x0, x1, ..., xn, t0, t1, ..., tm),他們「凝聚所有的參數爲計數和f(int n, double args[n])的數組。

注意這兩個推動所有的變量壓入堆棧,並從堆棧中讓他們很昂貴操作時經常執行,可以用這種方式避免。

+0

問題是根據他們的文檔和我的測試,n只告訴數字的額外參數,而不是維數。 – user1226313

0

我在github上的一個scipy問題中得到了答案:n是雙參數[]的長度,其中包括函數應被評估的座標以及任何其他給nquad並傳遞給c函數的參數

相關問題