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

Function normalize

numpy/f2py/symbolic.py:791–925  ·  view source on GitHub ↗

Normalize Expr and apply basic evaluation methods.

(obj)

Source from the content-addressed store, hash-verified

789
790
791def normalize(obj):
792 """Normalize Expr and apply basic evaluation methods.
793 """
794 if not isinstance(obj, Expr):
795 return obj
796
797 if obj.op is Op.TERMS:
798 d = {}
799 for t, c in obj.data.items():
800 if c == 0:
801 continue
802 if t.op is Op.COMPLEX and c != 1:
803 t = t * c
804 c = 1
805 if t.op is Op.TERMS:
806 for t1, c1 in t.data.items():
807 _pairs_add(d, t1, c1 * c)
808 else:
809 _pairs_add(d, t, c)
810 if len(d) == 0:
811 # TODO: determine correct kind
812 return as_number(0)
813 elif len(d) == 1:
814 (t, c), = d.items()
815 if c == 1:
816 return t
817 return Expr(Op.TERMS, d)
818
819 if obj.op is Op.FACTORS:
820 coeff = 1
821 d = {}
822 for b, e in obj.data.items():
823 if e == 0:
824 continue
825 if b.op is Op.TERMS and isinstance(e, integer_types) and e > 1:
826 # expand integer powers of sums
827 b = b * (b ** (e - 1))
828 e = 1
829
830 if b.op in (Op.INTEGER, Op.REAL):
831 if e == 1:
832 coeff *= b.data[0]
833 elif e > 0:
834 coeff *= b.data[0] ** e
835 else:
836 _pairs_add(d, b, e)
837 elif b.op is Op.FACTORS:
838 if e > 0 and isinstance(e, integer_types):
839 for b1, e1 in b.data.items():
840 _pairs_add(d, b1, e1 * e)
841 else:
842 _pairs_add(d, b, e)
843 else:
844 _pairs_add(d, b, e)
845 if len(d) == 0 or coeff == 0:
846 # TODO: determine correct kind
847 assert isinstance(coeff, number_types)
848 return as_number(coeff)

Callers 12

test_fromstringMethod · 0.90
__add__Method · 0.85
__mul__Method · 0.85
__pow__Method · 0.85
__truediv__Method · 0.85
__floordiv__Method · 0.85
substituteMethod · 0.85
traverseMethod · 0.85
as_termsFunction · 0.85
as_factorsFunction · 0.85
as_term_coeffFunction · 0.85
as_numer_denomFunction · 0.85

Calls 9

gcdFunction · 0.90
_pairs_addFunction · 0.85
as_numberFunction · 0.85
ExprClass · 0.85
as_term_coeffFunction · 0.85
as_applyFunction · 0.85
as_factorsFunction · 0.85
as_stringFunction · 0.85
maxFunction · 0.50

Tested by 1

test_fromstringMethod · 0.72