power¶
-
class
cvxpy.power(x, p, max_denom=1024)[source]¶ Elementwise power function \(f(x) = x^p\).
If
expris a CVXPY expression, thenexpr**pis equivalent topower(expr, p).Specifically, the atom is given by the cases
\[\begin{split}\begin{array}{ccl} p = 0 & f(x) = 1 & \text{constant, positive} \\ p = 1 & f(x) = x & \text{affine, increasing, same sign as $x$} \\ p = 2,4,8,\ldots &f(x) = |x|^p & \text{convex, signed monotonicity, positive} \\ p < 0 & f(x) = \begin{cases} x^p & x > 0 \\ +\infty & x \leq 0 \end{cases} & \text{convex, decreasing, positive} \\ 0 < p < 1 & f(x) = \begin{cases} x^p & x \geq 0 \\ -\infty & x < 0 \end{cases} & \text{concave, increasing, positive} \\ p > 1,\ p \neq 2,4,8,\ldots & f(x) = \begin{cases} x^p & x \geq 0 \\ +\infty & x < 0 \end{cases} & \text{convex, increasing, positive}. \end{array}\end{split}\]Note
Generally,
pcannot be represented exactly, so a rational, i.e., fractional, approximation must be made.Internally,
powercomputes a rational approximation topwith a denominator up tomax_denom. The resulting approximation can be found through the attributepower.p. The approximation error is given by the attributepower.approx_error. Increasingmax_denomcan give better approximations.When
pis anintorFractionobject, the approximation is usually exact.Note
The final domain, sign, monotonicity, and curvature of the
poweratom are determined by the rational approximation top, not the input parameterp.For example,
>>> from cvxpy import Variable, power >>> x = Variable() >>> g = power(x, 1.001) >>> g.p Fraction(1001, 1000) >>> g Expression(CONVEX, POSITIVE, (1, 1))
results in a convex atom with implicit constraint \(x \geq 0\), while
>>> g = power(x, 1.0001) >>> g.p 1 >>> g Expression(AFFINE, UNKNOWN, (1, 1))
results in an affine atom with no constraint on
x.When \(p > 1\) and
pis not a power of two, the monotonically increasing version of the function with full domain,\[\begin{split}f(x) = \begin{cases} x^p & x \geq 0 \\ 0 & x < 0 \end{cases}\end{split}\]can be formed with the composition
power(pos(x), p).The symmetric version with full domain,
\[f(x) = |x|^p\]can be formed with the composition
power(abs(x), p).
Parameters: x : cvx.Variable
p : int, float, or Fraction
Scalar power.
max_denom : int
The maximum denominator considered in forming a rational approximation of
p.