-
Notifications
You must be signed in to change notification settings - Fork 40
/
data.py
71 lines (60 loc) · 2.13 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Machinery for exps, cs, varlocs data -- common to nomials and programs"""
import numpy as np
from ..keydict import KeySet
from ..repr_conventions import ReprMixin
from ..varkey import VarKey
class NomialData(ReprMixin):
"""Object for holding cs, exps, and other basic 'nomial' properties.
cs: array (coefficient of each monomial term)
exps: tuple of {VarKey: float} (exponents of each monomial term)
varlocs: {VarKey: list} (terms each variable appears in)
units: pint.UnitsContainer
"""
# pylint: disable=too-many-instance-attributes
_hashvalue = _varlocs = _exps = _cs = _varkeys = None
def __init__(self, hmap):
self.hmap = hmap
self.units = self.hmap.units
self.any_nonpositive_cs = any(c <= 0 for c in self.hmap.values())
def to(self, units):
"Create new Signomial converted to new units"
return self.__class__(self.hmap.to(units))
@property
def exps(self):
"Create exps or return cached exps"
if self._exps is None:
self._exps = tuple(self.hmap.keys())
return self._exps
@property
def cs(self):
"Create cs or return cached cs"
if self._cs is None:
self._cs = np.array(list(self.hmap.values()))
if self.hmap.units:
# TODO: treat vars as dimensionless, it's a hack
self._cs = self._cs*self.hmap.units
return self._cs
def __hash__(self):
return hash(self.hmap)
@property
def vks(self):
"Set of a NomialData's varkeys, created as necessary."
vks = set()
for exp in self.hmap:
vks.update(exp)
return vks
@property # TODO: remove this
def varkeys(self):
"KeySet of a NomialData's varkeys, created as necessary."
return KeySet(self.vks)
def __eq__(self, other):
"Equality test"
if not hasattr(other, "hmap"):
return NotImplemented
if isinstance(other, VarKey):
return False
if self.hmap != other.hmap:
return False
if self.units != other.units:
return False
return True