def _collect_bilinear(self, expr, bilin, quad): if not expr.is_expression_type(): return if type(expr) is ProductExpression: if len(expr._numerator) != 2: for e in expr._numerator: self._collect_bilinear(e, bilin, quad) # No need to check denominator, as this is poly_degree==2 return if not isinstance(expr._numerator[0], _VarData) or \ not isinstance(expr._numerator[1], _VarData): raise RuntimeError("Cannot yet handle complex subexpressions") if expr._numerator[0] is expr._numerator[1]: quad.append((expr, expr._numerator[0])) else: bilin.append((expr, expr._numerator[0], expr._numerator[1])) return if type(expr) is PowExpression and value(expr._args[1]) == 2: # Note: directly testing the value of the exponent above is # safe: we have already verified that this expression is # polynominal, so the exponent must be constant. tmp = ProductExpression() tmp._numerator = [expr._args[0], expr._args[0]] tmp._denominator = [] expr._args = (tmp, as_numeric(1)) #quad.append( (tmp, tmp._args[0]) ) self._collect_bilinear(tmp, bilin, quad) return # All other expression types for e in expr._args: self._collect_bilinear(e, bilin, quad)
def _collect_bilinear(self, expr, bilin, quad): if not expr.is_expression(): return if type(expr) is _ProductExpression: if len(expr._numerator) != 2: for e in expr._numerator: self._collect_bilinear(e, bilin, quad) # No need to check denominator, as this is poly_degree==2 return if not isinstance(expr._numerator[0], _VarData) or \ not isinstance(expr._numerator[1], _VarData): raise RuntimeError("Cannot yet handle complex subexpressions") if expr._numerator[0] is expr._numerator[1]: quad.append( (expr, expr._numerator[0]) ) else: bilin.append( (expr, expr._numerator[0], expr._numerator[1]) ) return if type(expr) is _PowExpression and value(expr._args[1]) == 2: # Note: directly testing the value of the exponent above is # safe: we have already verified that this expression is # polynominal, so the exponent must be constant. tmp = _ProductExpression() tmp._numerator = [ expr._args[0], expr._args[0] ] tmp._denominator = [] expr._args = (tmp, as_numeric(1)) #quad.append( (tmp, tmp._args[0]) ) self._collect_bilinear(tmp, bilin, quad) return # All other expression types for e in expr._args: self._collect_bilinear(e, bilin, quad)