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

refactor: make base constants real numbers #160

Merged
merged 6 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions optlang/expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from optlang.symbolics import One
from optlang.symbolics import Integer


def parse_optimization_expression(obj, linear=True, quadratic=False, expression=None, **kwargs):
Expand Down Expand Up @@ -70,6 +70,7 @@ def _parse_linear_expression(expression, expanded=False, **kwargs):
"""
offset = 0
constant = None
one = Integer(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how much overhead this adds (probably very little), but I guess this could be moved outside of the function so it's only executed once when importing


if expression.is_Add:
coefficients = expression.as_coefficients_dict()
Expand All @@ -84,7 +85,7 @@ def _parse_linear_expression(expression, expanded=False, **kwargs):

for var in coefficients:
if not (var.is_Symbol):
if var == One:
if var == one:
constant = var
offset = float(coefficients[var])
elif expanded:
Expand Down
12 changes: 6 additions & 6 deletions optlang/symbolics.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
Real = symengine.RealDouble
Basic = symengine.Basic
Number = symengine.Number
Zero = Integer(0)
One = Integer(1)
NegativeOne = Integer(-1)
Zero = Real(0)
One = Real(1)
NegativeOne = Real(-1)
sympify = symengine.sympy_compat.sympify

Add = symengine.Add
Expand Down Expand Up @@ -113,9 +113,9 @@ def mul(*args):
Real = sympy.RealNumber
Basic = sympy.Basic
Number = sympy.Number
Zero = Integer(0)
One = Integer(1)
NegativeOne = Integer(-1)
Zero = Real(0)
One = Real(1)
NegativeOne = Real(-1)
sympify = sympy.sympify

Add = sympy.Add
Expand Down
4 changes: 2 additions & 2 deletions optlang/tests/test_symbolics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SymbolicsTestCase(unittest.TestCase):

def test_add_identity(self):
self.assertEqual(optlang.symbolics.add(), 0)
self.assertEqual(optlang.symbolics.add(), 0.0)

def test_mul_identity(self):
self.assertEqual(optlang.symbolics.mul(), 1)
self.assertEqual(optlang.symbolics.mul(), 1.0)