Skip to content

Commit 79ac37d

Browse files
schymansjirikuncar
authored andcommitted
fix(equations): improve substitutions with equations
* Extend .subs() to take more than one equation.
1 parent e617013 commit 79ac37d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

essm/equations/_core.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import warnings
2626

2727
import six
28-
2928
from sympy.core.relational import Eq
29+
from sympy.series import sequence
3030

3131
from ..bases import RegistryType
3232
from ..transformer import build_instance_expression
@@ -155,25 +155,42 @@ def __add__(self, other):
155155
)
156156

157157
def subs(self, *args, **kwargs): # should mirror sympy.core.basic.subs
158-
"""Return a new equation with subs applied to both sides.
158+
r"""Return a new equation with subs applied to both sides.
159159
160160
**Examples:**
161161
162-
>>> from essm.equations.physics.thermodynamics import eq_Pa, eq_PN2_PO2
163-
>>> from essm.variables.physics.thermodynamics import P_N2, P_O2
164-
>>> eq_Pa.subs({P_N2: P_O2})
165-
Eq(P_a, 2*P_O2 + P_wa)
166-
>>> eq_Pa.subs(eq_PN2_PO2)
167-
Eq(P_a, P_O2*x_N2/x_O2 + P_O2 + P_wa)
168-
162+
>>> from essm.equations.leaf.energy_water import (
163+
... eq_Rs_enbal, eq_El, eq_Hl, eq_Rll )
164+
>>> eq_Rs_enbal.subs(eq_El, eq_Hl, eq_Rll)
165+
Eq(R_s, E_lmol*M_w*lambda_E + a_sh*... + a_sh*h_c*(-T_a + T_l))
166+
>>> from essm.equations.physics.thermodynamics import (
167+
... eq_Le, eq_Dva, eq_alphaa)
168+
>>> from essm.variables.physics.thermodynamics import (
169+
... Le, D_va, alpha_a, T_a)
170+
>>> eq_Le.subs(D_va, eq_Dva.rhs)
171+
Eq(Le, alpha_a/(T_a*p_Dva1 - p_Dva2))
172+
>>> eq_Le.subs({D_va: eq_Dva.rhs, alpha_a: eq_alphaa.rhs})
173+
Eq(Le, (T_a*p_alpha1 - p_alpha2)/(T_a*p_Dva1 - p_Dva2))
174+
>>> eq_Le.subs(eq_Dva, eq_alphaa)
175+
Eq(Le, (T_a*p_alpha1 - p_alpha2)/(T_a*p_Dva1 - p_Dva2))
169176
"""
177+
sequence = args
178+
only_eqs = all(isinstance(arg, Eq) for arg in args)
179+
170180
if len(args) == 1:
171181
if isinstance(args[0], Eq):
172-
arg1 = {args[0].lhs: args[0].rhs}
173-
return Eq(self.lhs.subs(arg1, **kwargs),
174-
self.rhs.subs(arg1, **kwargs))
182+
sequence = ({args[0].lhs: args[0].rhs}, )
183+
elif len(args) == 2 and not only_eqs:
184+
sequence = ({args[0]: args[1]}, )
185+
elif args and only_eqs:
186+
sub_eqs = {}
187+
for arg in args:
188+
sub_eqs[arg.lhs] = arg.rhs
189+
sequence = (sub_eqs, )
190+
175191
return Eq(
176-
self.lhs.subs(*args, **kwargs), self.rhs.subs(*args, **kwargs)
192+
self.lhs.subs(*sequence, **kwargs),
193+
self.rhs.subs(*sequence, **kwargs),
177194
)
178195

179196

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ pep8ignore = docs/conf.py ALL
1818
testpaths = docs tests essm
1919

2020
[check-manifest]
21-
ignore = .gitignore
21+
ignore = .gitignore

0 commit comments

Comments
 (0)