Recursively substitute symbols with values in symbols map. Symbols map is a dictionary of symbol-expression pairs.
(self, symbols_map)
| 582 | return Expr(Op.INDEXING, (self,) + index) |
| 583 | |
| 584 | def substitute(self, symbols_map): |
| 585 | """Recursively substitute symbols with values in symbols map. |
| 586 | |
| 587 | Symbols map is a dictionary of symbol-expression pairs. |
| 588 | """ |
| 589 | if self.op is Op.SYMBOL: |
| 590 | value = symbols_map.get(self) |
| 591 | if value is None: |
| 592 | return self |
| 593 | m = re.match(r'\A(@__f2py_PARENTHESIS_(\w+)_\d+@)\Z', self.data) |
| 594 | if m: |
| 595 | # complement to fromstring method |
| 596 | items, paren = m.groups() |
| 597 | if paren in ['ROUNDDIV', 'SQUARE']: |
| 598 | return as_array(value) |
| 599 | assert paren == 'ROUND', (paren, value) |
| 600 | return value |
| 601 | if self.op in (Op.INTEGER, Op.REAL, Op.STRING): |
| 602 | return self |
| 603 | if self.op in (Op.ARRAY, Op.COMPLEX): |
| 604 | return Expr(self.op, tuple(item.substitute(symbols_map) |
| 605 | for item in self.data)) |
| 606 | if self.op is Op.CONCAT: |
| 607 | return normalize(Expr(self.op, tuple(item.substitute(symbols_map) |
| 608 | for item in self.data))) |
| 609 | if self.op is Op.TERMS: |
| 610 | r = None |
| 611 | for term, coeff in self.data.items(): |
| 612 | if r is None: |
| 613 | r = term.substitute(symbols_map) * coeff |
| 614 | else: |
| 615 | r += term.substitute(symbols_map) * coeff |
| 616 | if r is None: |
| 617 | ewarn('substitute: empty TERMS expression interpreted as' |
| 618 | ' int-literal 0') |
| 619 | return as_number(0) |
| 620 | return r |
| 621 | if self.op is Op.FACTORS: |
| 622 | r = None |
| 623 | for base, exponent in self.data.items(): |
| 624 | if r is None: |
| 625 | r = base.substitute(symbols_map) ** exponent |
| 626 | else: |
| 627 | r *= base.substitute(symbols_map) ** exponent |
| 628 | if r is None: |
| 629 | ewarn('substitute: empty FACTORS expression interpreted' |
| 630 | ' as int-literal 1') |
| 631 | return as_number(1) |
| 632 | return r |
| 633 | if self.op is Op.APPLY: |
| 634 | target, args, kwargs = self.data |
| 635 | if isinstance(target, Expr): |
| 636 | target = target.substitute(symbols_map) |
| 637 | args = tuple(a.substitute(symbols_map) for a in args) |
| 638 | kwargs = {k: v.substitute(symbols_map) |
| 639 | for k, v in kwargs.items()} |
| 640 | return normalize(Expr(self.op, (target, args, kwargs))) |
| 641 | if self.op is Op.INDEXING: |