-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25c0799
commit 1689b11
Showing
6 changed files
with
144 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from sympy import symbols | ||
from sympy.core.containers import Dict as SympyDict | ||
|
||
NO_AXIS = symbols("_other_") | ||
|
||
|
||
class AxisDict(SympyDict): | ||
"""This class acts like a dict with arithmetic operations. It is useful to process 'axes' LCA computations""" | ||
|
||
def _apply_op(self, other, fop, null_val): | ||
# None is the key for non flagged values | ||
if not isinstance(other, AxisDict): | ||
dic = dict() | ||
dic[NO_AXIS] = other | ||
other = AxisDict(dic) | ||
|
||
all_keys = set(other._dict.keys()) | set(self._dict.keys()) | ||
return AxisDict({key: fop(self._dict.get(key, null_val), other._dict.get(key, null_val)) for key in all_keys}) | ||
|
||
def __repr__(self): | ||
"""Custom representation that returns string as key instead of symbols""" | ||
return "{%s}" % ",".join("'%s': %s" % (k.__repr__(), v.__repr__()) for k, v in self._dict.items()) | ||
|
||
def __str__(self): | ||
return self.__repr__() | ||
|
||
def _apply_self(self, fop): | ||
return AxisDict({key: fop(val) for key, val in self._dict.items()}) | ||
|
||
def __add__(self, other): | ||
return self._apply_op(other, lambda a, b: a + b, 0) | ||
|
||
def __radd__(self, other): | ||
return self._apply_op(other, lambda a, b: b + a, 0) | ||
|
||
def __mul__(self, other): | ||
return self._apply_self(lambda a: a * other) | ||
|
||
def __rmul__(self, other): | ||
return self._apply_self(lambda a: other * a) | ||
|
||
def __truediv__(self, other): | ||
return self._apply_self(lambda a: a / other) | ||
|
||
def __rtruediv__(self, other): | ||
return NotImplemented | ||
|
||
def _defer(self, funcname, args, kwargs): | ||
return AxisDict( | ||
{ | ||
key: val if not hasattr(val, funcname) else getattr(val, funcname)(*args, **kwargs) | ||
for key, val in self._dict.items() | ||
} | ||
) | ||
|
||
def str_keys(self): | ||
# REturn a list to ensure the order is kept | ||
return list(str(key) for key in self._dict.keys()) | ||
|
||
@property | ||
def free_symbols(self): | ||
"""Only return free symbol for values (not keys)""" | ||
res = set() | ||
for key, val in self._dict.items(): | ||
if hasattr(val, "free_symbols"): | ||
res |= val.free_symbols | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from sympy import symbols, lambdify, simplify | ||
|
||
from lca_algebraic.axis_dict import AxisDict, NO_AXIS | ||
|
||
|
||
def test_sum(): | ||
a, b = symbols("a b") | ||
|
||
a1 = AxisDict({a: 1}) | ||
a2 = AxisDict({a: 2}) | ||
b2 = AxisDict({b: 2}) | ||
|
||
assert a1 + b2 == AxisDict({a: 1, b: 2}) | ||
assert a1 + a2 == AxisDict({a: 3}) | ||
assert a1 + 1 == AxisDict({a: 1, NO_AXIS: 1}) | ||
|
||
|
||
def test_mul(): | ||
a = symbols("a") | ||
|
||
a1 = AxisDict({a: 2}) | ||
assert a1 * 2 == AxisDict({a: 4}) | ||
assert simplify(a1 / 2) == AxisDict({a: 1}) | ||
|
||
|
||
def test_free_symbols(): | ||
dic = AxisDict({"a": "b"}) | ||
assert dic.free_symbols == set([symbols("b")]) | ||
|
||
|
||
def test_lambdify(): | ||
a, b = symbols("a b") | ||
|
||
a1 = AxisDict({a: b * 2}) | ||
|
||
lambd = lambdify([b], a1) | ||
|
||
res = lambd(2) | ||
|
||
assert res == {a: 4} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters