def q_affine(expr, vars):
"""
Return True if ``expr`` is (separately) affine in the variables ``vars``,
False otherwise.
Readapted from: https://stackoverflow.com/questions/36283548\
/check-if-an-equation-is-linear-for-a-specific-set-of-variables/
"""
# A function is (separately) affine in a given set of variables if all
# non-mixed second order derivatives are identically zero.
for x in as_tuple(vars):
if x not in expr.atoms():
return False
try:
if diff(expr, x) == nan or not Eq(diff(expr, x, x), 0):
return False
except TypeError:
return False
return True
评论列表
文章目录