Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix differentiation of Expressions containing native_numeric_types #3017

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyomo/core/expr/calculus/diff_with_pyomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _diff_GeneralExpression(node, val_dict, der_dict):
val_dict: ComponentMap
der_dict: ComponentMap
"""
der_dict[node.expr] += der_dict[node]
der_dict[node.arg(0)] += der_dict[node]


def _diff_ExternalFunctionExpression(node, val_dict, der_dict):
Expand Down
11 changes: 11 additions & 0 deletions pyomo/core/tests/unit/test_derivs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ def e2(m, i):
symbolic = reverse_sd(m.o.expr)
self.assertAlmostEqual(derivs[m.x], pyo.value(symbolic[m.x]), tol)

def test_constant_named_expressions(self):
m = pyo.ConcreteModel()
m.x = pyo.Var(initialize=3)
m.e = pyo.Expression(expr=2)

e = m.x * m.e
derivs = reverse_ad(e)
symbolic = reverse_sd(e)
self.assertAlmostEqual(derivs[m.x], pyo.value(symbolic[m.x]), tol + 3)
self.assertAlmostEqual(derivs[m.x], approx_deriv(e, m.x), tol)

def test_multiple_named_expressions(self):
m = pyo.ConcreteModel()
m.x = pyo.Var()
Expand Down
Loading