A function is (jointly) linear in a given set of variables if all second-order derivatives are identically equal to zero (including mixed ones). This can be verified as follows:
def is_linear(expr, vars): for x in vars: for y in vars: try: if not sympy.Eq(sympy.diff(expr, x, y), 0): return False except TypeError: return False return True
In the cycle, each derivative is taken and checked for equality to zero. If sympy cannot decide whether it is equal to zero (raising a TypeError), then it is not identically equal to zero.
Output:
>>> is_linear(eq1, [a,d]) True >>> is_linear(eq1, [a,c]) False
To verify a separate linearity (for example, separately in and separately in b), discard mixed partial derivatives:
def is_separately_linear(expr, vars): for x in vars: try: if not sympy.Eq(sympy.diff(expr, x, x), 0): return False except TypeError: return False return True
Output:
>>> is_separately_linear(eq1, [a,d]) True >>> is_separately_linear(eq1, [a,c]) True
user3717023
source share