MCPcopy Create free account
hub / github.com/numpy/numpy / substitute

Method substitute

numpy/f2py/symbolic.py:585–658  ·  view source on GitHub ↗

Recursively substitute symbols with values in symbols map. Symbols map is a dictionary of symbol-expression pairs.

(self, symbols_map)

Source from the content-addressed store, hash-verified

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

Callers 5

linear_solveMethod · 0.95
doxy_configFunction · 0.45
process_tempitaFunction · 0.45
substituteMethod · 0.45
test_substituteMethod · 0.45

Calls 6

ExprClass · 0.85
normalizeFunction · 0.85
ewarnFunction · 0.85
as_numberFunction · 0.85
as_arrayFunction · 0.70
getMethod · 0.45

Tested by 1

test_substituteMethod · 0.36