Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_affine_var_and_param_coeff(expr):
expr = Value.numericToValue(expr)
if (not isAffine(expr) or \
isinstance(expr, Value) or \
is_constant_expr(expr)):
return {}
elif (isinstance(expr, constructs.Variable)):
return {expr: 1}
elif (isinstance(expr, AbstractBinaryOpNode)):
coeff = {}
left_coeff = get_affine_var_and_param_coeff(expr.left)
right_coeff = get_affine_var_and_param_coeff(expr.right)
if (expr.op == '+'):
coeff = dict((n, left_coeff.get(n, 0) + right_coeff.get(n, 0)) \
for n in set(left_coeff) | set(right_coeff))
elif (expr.op == '-'):
coeff = dict((n, left_coeff.get(n, 0) - right_coeff.get(n, 0)) \
for n in set(left_coeff) | set(right_coeff))
elif (expr.op == '*'):
left_is_constant = is_constant_expr(expr.left, affine=True)
right_is_constant = is_constant_expr(expr.right, affine=True)
#sanity check should be true if the expression is affine
assert(not (left_is_constant and right_is_constant))
def getType(expr):
expr = Value.numericToValue(expr)
assert(isinstance(expr, AbstractExpression))
if (isinstance(expr, Value)):
return expr.typ
elif (isinstance(expr, constructs.Variable)):
return expr.typ
elif (isinstance(expr, constructs.Reference)):
return expr.objectRef.typ
elif (isinstance(expr, AbstractBinaryOpNode)):
left_type = getType(expr.left)
right_type = getType(expr.right)
return result_type(left_type, right_type)
elif (isinstance(expr, AbstractUnaryOpNode)):
return getType(expr.child)
elif (isinstance(expr, constructs.Cast)):
return expr.typ
elif (isinstance(expr, constructs.Select)):
true_type = getType(expr.trueExpression)
false_type = getType(expr.falseExpression)
assert true_type == false_type
return true_type