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
8class NomialData(ReprMixin):
9 """Object for holding cs, exps, and other basic 'nomial' properties.
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
19 def __init__(self, hmap):
20 self.hmap = hmap
21 self.units = self.hmap.units
22 self.any_nonpositive_cs = any(c <= 0 for c in self.hmap.values())
24 def to(self, units):
25 "Create new Signomial converted to new units"
26 return self.__class__(self.hmap.to(units))
28 @property
29 def exps(self):
30 "Create exps or return cached exps"
31 if self._exps is None:
32 self._exps = tuple(self.hmap.keys())
33 return self._exps
35 @property
36 def cs(self):
37 "Create cs or return cached cs"
38 if self._cs is None:
39 self._cs = np.array(list(self.hmap.values()))
40 if self.hmap.units:
41 self._cs = self._cs*self.hmap.units
42 return self._cs
44 def __hash__(self):
45 return hash(self.hmap)
47 @property
48 def vks(self):
49 "Set of a NomialData's varkeys, created as necessary."
50 vks = set()
51 for exp in self.hmap:
52 vks.update(exp)
53 return vks
55 @property # TODO: remove this
56 def varkeys(self):
57 "KeySet of a NomialData's varkeys, created as necessary."
58 return KeySet(self.vks)
60 def __eq__(self, other):
61 "Equality test"
62 if not hasattr(other, "hmap"):
63 return NotImplemented
64 if isinstance(other, VarKey):
65 return False
66 if self.hmap != other.hmap:
67 return False
68 if self.units != other.units:
69 return False
70 return True