Skip to content

Commit 85a8e75

Browse files
committed
added div and some other small changes
1 parent 5870139 commit 85a8e75

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

cvxpy/atoms/affine/binary_operators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ def is_decr(self, idx) -> bool:
404404
return self.args[1].is_nonpos()
405405
else:
406406
return self.args[0].is_nonneg()
407+
408+
def point_in_domain(self):
409+
return np.ones(self.args[1].shape)
407410

408411
def graph_implementation(
409412
self, arg_objs, shape: Tuple[int, ...], data=None

cvxpy/reductions/expr2smooth/canonicalizers/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from cvxpy.atoms.elementwise.power import power
2020
from cvxpy.atoms.pnorm import Pnorm
2121
from cvxpy.atoms.elementwise.abs import abs
22+
from cvxpy.atoms.affine.binary_operators import DivExpression
23+
from cvxpy.reductions.expr2smooth.canonicalizers.div_canon import div_canon
2224
from cvxpy.reductions.expr2smooth.canonicalizers.log_canon import log_canon
2325
from cvxpy.reductions.expr2smooth.canonicalizers.minimum_canon import minimum_canon
2426
from cvxpy.reductions.expr2smooth.canonicalizers.abs_canon import abs_canon
@@ -33,5 +35,5 @@
3335
log: log_canon,
3436
power: power_canon,
3537
Pnorm : pnorm_canon,
36-
# inv: inv_pos_canon,
38+
DivExpression: div_canon,
3739
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Copyright 2025 CVXPY developers
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from cvxpy.expressions.variable import Variable
18+
19+
20+
def div_canon(expr, args):
21+
denom = Variable(args[1].shape)
22+
if args[1].value is not None:
23+
denom.value = args[1].value
24+
else:
25+
denom.value = expr.point_in_domain()
26+
return expr.copy([args[0], denom]), [denom == args[1]]

cvxpy/reductions/expr2smooth/canonicalizers/log_canon.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919

2020
def log_canon(expr, args):
2121
t = Variable(args[0].size)
22-
t.value = expr.point_in_domain()
22+
if args[0].value is not None:
23+
t.value = args[0].value
24+
else:
25+
t.value = expr.point_in_domain()
2326
return expr.copy([t]), [t==args[0]]

cvxpy/reductions/expr2smooth/canonicalizers/power_canon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ def power_canon(expr, args):
3636
t = Variable(shape)
3737
if 0 < p < 1:
3838
if x.value is not None:
39+
# TODO: check if this initialization is correct
3940
t.value = np.power(np.abs(x.value), p)
4041
return t, [t**(1/p) == x, t >= 0]
4142
elif p > 1:
4243
t = Variable(args[0].shape)
4344
if args[0].value is not None:
44-
t.value = np.power(args[0].value, p)
45+
t.value = args[0].value
4546
else:
4647
t.value = expr.point_in_domain()
4748
return expr.copy([t]), [t==args[0]]

0 commit comments

Comments
 (0)