-
Notifications
You must be signed in to change notification settings - Fork 277
Feature: Expr and GenExpr support numpy unary func like np.sin
#1170
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
base: master
Are you sure you want to change the base?
Changes from all commits
452ca36
4d911c1
b119224
4a65d98
2d8945f
01658f4
f0aacb6
7e78cac
196f43a
6dd2c93
ee4c6f6
e76b0d6
06b4df4
a76ef44
5611d52
3402b32
925cb43
857c969
c94177c
588cba4
cd642f9
f8e6132
940fd93
900fdc3
c85be94
4f6058a
a1501e6
259f4ce
cfa8d5a
0517639
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import math | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pyscipopt import Model, sqrt, log, exp, sin, cos | ||
| from pyscipopt.scip import Expr, GenExpr, ExprCons, Term | ||
| from pyscipopt import Model, cos, exp, log, sin, sqrt | ||
| from pyscipopt.scip import Expr, ExprCons, GenExpr, Term | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
|
|
@@ -118,10 +118,12 @@ def test_genexpr_op_genexpr(model): | |
| assert isinstance(1/x + genexpr, GenExpr) | ||
| assert isinstance(1/x**1.5 - genexpr, GenExpr) | ||
| assert isinstance(y/x - exp(genexpr), GenExpr) | ||
| # sqrt(2) is not a constant expression and | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we sure that SCIP's and numpy's trignometric functions are the same? Wondering whether precision differences might affect the results
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we shouldn't worry about this. No matter whether we use Because of the following things, it's better be a constant, not a
|
||
| # we can only power to constant expressions! | ||
| with pytest.raises(NotImplementedError): | ||
| genexpr **= sqrt(2) | ||
|
|
||
| genexpr **= sqrt(2) | ||
| assert isinstance(genexpr, GenExpr) | ||
|
|
||
| with pytest.raises(TypeError): | ||
| genexpr **= sqrt("2") | ||
|
|
||
| def test_degree(model): | ||
| m, x, y, z = model | ||
|
|
@@ -220,6 +222,24 @@ def test_getVal_with_GenExpr(): | |
| m.getVal(1 / z) | ||
|
|
||
|
|
||
| def test_unary(model): | ||
| m, x, y, z = model | ||
|
|
||
| assert str(abs(x)) == "abs(sum(0.0,prod(1.0,x)))" | ||
| assert str(np.absolute(x)) == "abs(sum(0.0,prod(1.0,x)))" | ||
| assert str(sin([x, y])) == "[sin(sum(0.0,prod(1.0,x))) sin(sum(0.0,prod(1.0,y)))]" | ||
| assert ( | ||
| str(np.sin([x, y])) == "[sin(sum(0.0,prod(1.0,x))) sin(sum(0.0,prod(1.0,y)))]" | ||
| ) | ||
| assert ( | ||
| str(sqrt([x, y])) == "[sqrt(sum(0.0,prod(1.0,x))) sqrt(sum(0.0,prod(1.0,y)))]" | ||
| ) | ||
| assert ( | ||
| str(np.sqrt([x, y])) | ||
| == "[sqrt(sum(0.0,prod(1.0,x))) sqrt(sum(0.0,prod(1.0,y)))]" | ||
| ) | ||
|
|
||
|
|
||
| def test_abs_abs_expr(): | ||
| m = Model() | ||
| x = m.addVar(name="x") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExprLikeis the base class and also a duck type.It defines the behavior, and its subclass defines the data.
I will use
ExprLiketo splitVariableandExprin the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the main benefit of this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variableis the subclass ofExpr. SoVariablecould operate likeExpr. And we don't have to add additional logic to handleVariable. This idea is good.But the relationships among
Variable,Term, andExprare very confusing.Variableis the unit of calculation. Why is it a subclass ofExpr?So
ExprLikecould splitVariableandExpr. And letVariablework withExpreasily.ExprLikeis a duck type, and it defines the operator's inputs and outputs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now,
ExprLikecould save code.__array_ufunc__won't repeat in Expr, and GenExpr.