[英]Generate C code for sympy expression “e1 op e2” where op is an arithmetic comparison operator
Suppose I have a sympy expression 假设我有一个sympy表达
e1 op e2
where op
is any arithmetic comparison operator, such as <, >, <=
, etc. 其中
op
是任何算术比较运算符,例如<, >, <=
等。
I want to generate a C code 我想生成一个C代码
double f(double x1,double x2,..., double xn){
if (e1 op e2) return 0 else return 1;
}
where x1,...,x2 are the free variables in either e1 or e2. 其中x1,...,x2是e1或e2中的自由变量。 Can I use sympy.printing.ccode , or sympy.utlilities.codegen to simplify this code generation?
我可以使用sympy.printing.ccode或sympy.utlilities.codegen简化此代码生成吗?
Here is an example using ccode and Piecewise (you would need to provide the function body as a template): 这是一个使用ccode和Piecewise的示例(您需要提供函数主体作为模板):
>>> from sympy import symbols, Piecewise, ccode
>>> x, y, z = symbols('x y z')
>>> e1 = x + 2
>>> e2 = y - 5
>>> rel = e1 < e2
>>> pw = Piecewise((0, rel), (1, True))
>>> print(ccode(pw, assign_to=z))
if (x + 2 < y - 5) {
z = 0;
}
else {
z = 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.