Return expression as term-coefficient pair.
(obj)
| 1092 | |
| 1093 | |
| 1094 | def as_term_coeff(obj): |
| 1095 | """Return expression as term-coefficient pair. |
| 1096 | """ |
| 1097 | if isinstance(obj, Expr): |
| 1098 | obj = normalize(obj) |
| 1099 | if obj.op is Op.INTEGER: |
| 1100 | return as_integer(1, obj.data[1]), obj.data[0] |
| 1101 | if obj.op is Op.REAL: |
| 1102 | return as_real(1, obj.data[1]), obj.data[0] |
| 1103 | if obj.op is Op.TERMS: |
| 1104 | if len(obj.data) == 1: |
| 1105 | (term, coeff), = obj.data.items() |
| 1106 | return term, coeff |
| 1107 | # TODO: find common divisor of coefficients |
| 1108 | if obj.op is Op.APPLY and obj.data[0] is ArithOp.DIV: |
| 1109 | t, c = as_term_coeff(obj.data[1][0]) |
| 1110 | return as_apply(ArithOp.DIV, t, obj.data[1][1]), c |
| 1111 | return obj, 1 |
| 1112 | raise OpError(f'cannot convert {type(obj)} to term and coeff') |
| 1113 | |
| 1114 | |
| 1115 | def as_numer_denom(obj): |