sympy codegen:强制统一签名
sympy codegen: force uniform signature
我正在创建一系列依赖于 x,y 变量的参数化 sympy 函数。一些函数结果只依赖于一些变量。因此,当我调用 'codegen' 时,创建的函数的接口会有所不同(有时它包括所有变量,有时不包括)。但是,我希望能够在 C 中以 uniform 方式调用函数。
长话短说,举个例子:
x,y = sympy.symbols("x,y")
S1 = x + y
S2 = x
d = {'S1':S1,'S2':S2}
for k in d.keys():
[(c_name, c_code), (h_name, c_header)] = codegen((k, d[k]), ...\
"C",func_name,header=False, empty=False)
print(c_code)
输出:
#include "S2.h"
#include <math.h>
double S2(double x) {
double S2_result;
S2_result = x;
return S2_result;
}
#include "S1.h"
#include <math.h>
double S1(double x, double y) {
double S1_result;
S1_result = x + y;
return S1_result;
}
我的问题是:如何让 codegen 创建具有相同签名的两个函数?
我认为您正在寻找 codegen()
的 argument_sequence
参数。它允许你指定一个固定的顺序,它也将接受冗余参数。
已记录 here。
我正在创建一系列依赖于 x,y 变量的参数化 sympy 函数。一些函数结果只依赖于一些变量。因此,当我调用 'codegen' 时,创建的函数的接口会有所不同(有时它包括所有变量,有时不包括)。但是,我希望能够在 C 中以 uniform 方式调用函数。
长话短说,举个例子:
x,y = sympy.symbols("x,y")
S1 = x + y
S2 = x
d = {'S1':S1,'S2':S2}
for k in d.keys():
[(c_name, c_code), (h_name, c_header)] = codegen((k, d[k]), ...\
"C",func_name,header=False, empty=False)
print(c_code)
输出:
#include "S2.h"
#include <math.h>
double S2(double x) {
double S2_result;
S2_result = x;
return S2_result;
}
#include "S1.h"
#include <math.h>
double S1(double x, double y) {
double S1_result;
S1_result = x + y;
return S1_result;
}
我的问题是:如何让 codegen 创建具有相同签名的两个函数?
我认为您正在寻找 codegen()
的 argument_sequence
参数。它允许你指定一个固定的顺序,它也将接受冗余参数。
已记录 here。