Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Machinery for exps, cs, varlocs data -- common to nomials and programs""" 

2import numpy as np 

3from ..keydict import KeySet 

4from ..repr_conventions import ReprMixin 

5from ..varkey import VarKey 

6 

7 

8class NomialData(ReprMixin): 

9 """Object for holding cs, exps, and other basic 'nomial' properties. 

10 

11 cs: array (coefficient of each monomial term) 

12 exps: tuple of {VarKey: float} (exponents of each monomial term) 

13 varlocs: {VarKey: list} (terms each variable appears in) 

14 units: pint.UnitsContainer 

15 """ 

16 # pylint: disable=too-many-instance-attributes 

17 _hashvalue = _varlocs = _exps = _cs = _varkeys = None 

18 

19 def __init__(self, hmap): 

20 self.hmap = hmap 

21 self.vks = set() 

22 for exp in self.hmap: 

23 self.vks.update(exp) 

24 self.units = self.hmap.units 

25 self.any_nonpositive_cs = any(c <= 0 for c in self.hmap.values()) 

26 

27 def to(self, units): 

28 "Create new Signomial converted to new units" 

29 return self.__class__(self.hmap.to(units)) 

30 

31 @property 

32 def exps(self): 

33 "Create exps or return cached exps" 

34 if self._exps is None: 

35 self._exps = tuple(self.hmap.keys()) 

36 return self._exps 

37 

38 @property 

39 def cs(self): 

40 "Create cs or return cached cs" 

41 if self._cs is None: 

42 self._cs = np.array(list(self.hmap.values())) 

43 if self.hmap.units: 

44 self._cs = self._cs*self.hmap.units 

45 return self._cs 

46 

47 def __hash__(self): 

48 return hash(self.hmap) 

49 

50 @property 

51 def varkeys(self): 

52 "The NomialData's varkeys, created when necessary for a substitution." 

53 if self._varkeys is None: 

54 self._varkeys = KeySet(self.vks) 

55 return self._varkeys 

56 

57 def __eq__(self, other): 

58 "Equality test" 

59 if not hasattr(other, "hmap"): 

60 return NotImplemented 

61 if isinstance(other, VarKey): 

62 return False 

63 if self.hmap != other.hmap: 

64 return False 

65 if self.units != other.units: 

66 return False 

67 return True