
    bi                         d Z ddlmc mZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ  G d dej                        Z G d	 d
e      Z G d de      Zy)a,  
Copyright 2013 Steven Diamond

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)DCPError)
Expression)scalar_value)scopesc                       e Zd ZdZdZddZdefdZdefdZd Z	d	 Z
d
 Zd ZeZd ZeZed        ZdefdZdefdZy)	ObjectivezAn optimization objective.

    Parameters
    ----------
    expr : Expression
        The expression to act upon. Must be a scalar.

    Raises
    ------
    ValueError
        If expr is not a scalar.
    	objectivereturnNc                    t        j                  |      g| _        d| _        | j                  d   j	                         st        d| j                  z        | j                  d   j                         st        d| j                  z        y )Nr   z,The '%s' objective must resolve to a scalar.z'The '%s' objective must be real valued.)r   cast_to_constargsndim	is_scalar
ValueErrorNAMEis_real)selfexprs     S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/problems/objective.py__init__zObjective.__init__)   s    --d34		yy|%%'K#yy) * *yy|##%F#yy) * * &    c                 f    | j                   j                  dt        | j                  d         dS )N(r   ))	__class____name__reprr   r   s    r   __repr__zObjective.__repr__5   s$    >>22D14FGGr   c                 r    dj                  | j                  | j                  d   j                         g      S )N r   )joinr   r   namer   s    r   __str__zObjective.__str__8   s+    xxDIIaL$5$5$7899r   c                 $    |dk(  r| S t               Nr   NotImplementedErrorr   others     r   __radd__zObjective.__radd__;   s    A:K%''r   c                 N    t        |t        t        f      s
t               | | z   S )N)
isinstanceMinimizeMaximizer(   r)   s     r   __sub__zObjective.__sub__A   s&    %(H!56%''vr   c                 &    |dk(  r|  S t               r&   r'   r)   s     r   __rsub__zObjective.__rsub__G   s    A:5L%''r   c                     t        |t        t        f      s
t               t	        |       t
        k(  |dk  k(  rt        | j                  d   |z        S t        | j                  d   |z        S )Ng        r   )r-   intfloatr(   typer/   r.   r   r)   s     r   __mul__zObjective.__mul__M   s^    %#u.%''J("4DIIaL5011DIIaL5011r   c                 R    t        |t        t        f      s
t               | d|z  z  S )Ng      ?)r-   r4   r5   r(   r)   s     r   __div__zObjective.__div__X   s(    %#u.%''s5y!!r   c                 P    | j                   d   j                  }|yt        |      S )z/The value of the objective expression.
        r   N)r   valuer   )r   vs     r   r;   zObjective.value_   s)     IIaL9?"r   c                 <    | j                   d   j                         S )z:Returns if the objective is a quadratic function.
        r   )r   is_quadraticr   s    r   r>   zObjective.is_quadratici   s     yy|((**r   c                 <    | j                   d   j                         S )zEReturns if the objective is a quadratic of piecewise affine.
        r   )r   is_qpwar   s    r   r@   zObjective.is_qpwan   s     yy|##%%r   )r
   N)r   
__module____qualname____doc__r   r   strr   r$   r+   r0   r2   r7   __rmul__r9   __truediv__propertyr;   boolr>   r@    r   r   r   r      s~     D
*H# H: :((2 H"
 K# #+d +
& &r   r   c                   v    e Zd ZdZdZddZd Zd ZddedefdZ	ddedefd	Z
ddefd
ZdefdZed        Zy)r.   zAn optimization objective for minimization.

    Parameters
    ----------
    expr : Expression
        The expression to minimize. Must be a scalar.

    Raises
    ------
    ValueError
        If expr is not a scalar.
    minimizer
   c                 4    t        | j                  d          S r&   )r/   r   r   s    r   __neg__zMinimize.__neg__       1&&r   c                     t        |t        t        f      s
t               t	        |      t        u r(t        | j
                  d   |j
                  d   z         S t        d      Nr   z"Problem does not follow DCP rules.)r-   r.   r/   r(   r6   r   r   r)   s     r   __add__zMinimize.__add__   sR    %(H!56%'';("DIIaL5::a=899?@@r   c                 4    | j                   d   j                  S )zCPass on the target expression's objective and constraints.
        r   )r   canonical_formr   s    r   canonicalizezMinimize.canonicalize   s     yy|***r   dppc                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z&The objective must be convex.
        r   N)r   	dpp_scoper   	is_convexr   rU   s     r   is_dcpzMinimize.is_dcp   sX     !!# 0yy|--/0 0yy|%%''0 0   AA$c                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z.The objective must be log-log convex.
        r   N)r   rW   r   is_log_log_convexrY   s     r   is_dgpzMinimize.is_dgp   sX     !!# 8yy|5578 8yy|--//8 8r[   c                    t        j                         5  |j                         dk(  r| j                  d      cd d d        S |j                         dk(  r| j	                  d      cd d d        S t        d|      # 1 sw Y   y xY wNdcpT)rU   dgpzUnsupported context r   rW   lowerrZ   r^   r   r   contexts     r   is_dppzMinimize.is_dpp        	B}}%'{{t{,	B 	B E){{t{,		B 	B !!7AA	B 	B   %A>$A>2A>>Bc                 <    | j                   d   j                         S )z+The objective must be quasiconvex.
        r   )r   is_quasiconvexr   s    r   is_dqcpzMinimize.is_dqcp   s     yy|**,,r   c                     | S zBThe value of the objective given the solver primal value.
        rI   results    r   primal_to_resultzMinimize.primal_to_result   s	     r   N)r
   r/   Fra   )r   rA   rB   rC   r   rM   rQ   rT   rH   rZ   r^   rg   rl   staticmethodrq   rI   r   r   r.   r.   t   sr     D'A+
($ (4 (0$ 04 0Bt B- -
  r   r.   c                   z    e Zd ZdZdZdefdZd Zd Zdde	de	fdZ
dde	de	fd	Zdde	fd
Zde	fdZed        Zy)r/   zAn optimization objective for maximization.

    Parameters
    ----------
    expr : Expression
        The expression to maximize. Must be a scalar.

    Raises
    ------
    ValueError
        If expr is not a scalar.
    maximizer
   c                 4    t        | j                  d          S r&   )r.   r   r   s    r   rM   zMaximize.__neg__   rN   r   c                     t        |t        t        f      s
t               t	        |      t        u r(t        | j
                  d   |j
                  d   z         S t        d      rP   )r-   r.   r/   r(   r6   r   	Exceptionr)   s     r   rQ   zMaximize.__add__   sR    %(H!56%'';("DIIaL5::a=899@AAr   c                 h    | j                   d   j                  \  }}t        j                  |      |fS )z3Negates the target expression's objective.
        r   )r   rS   luneg_expr)r   objconstraintss      r   rT   zMaximize.canonicalize   s0      99Q<66[C +..r   rU   c                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z'The objective must be concave.
        r   N)r   rW   r   
is_concaverY   s     r   rZ   zMaximize.is_dcp   sX     !!# 1yy|..01 1yy|&&((1 1r[   c                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z/The objective must be log-log concave.
        r   N)r   rW   r   is_log_log_concaverY   s     r   r^   zMaximize.is_dgp   sX     !!# 9yy|6689 9yy|..009 9r[   c                    t        j                         5  |j                         dk(  r| j                  d      cd d d        S |j                         dk(  r| j	                  d      cd d d        S t        d|      # 1 sw Y   y xY wr`   rc   re   s     r   rg   zMaximize.is_dpp   rh   ri   c                 <    | j                   d   j                         S )z,The objective must be quasiconcave.
        r   )r   is_quasiconcaver   s    r   rl   zMaximize.is_dqcp   s     yy|++--r   c                     |  S rn   rI   ro   s    r   rq   zMaximize.primal_to_result   s     wr   Nrr   rs   )r   rA   rB   rC   r   r.   rM   rQ   rT   rH   rZ   r^   rg   rl   rt   rq   rI   r   r   r/   r/      sy     D' 'B/)$ )4 )1$ 14 1Bt B. .
  r   r/   )rC   cvxpy.lin_ops.lin_utilslin_ops	lin_utilsr{   cvxpy.utilities	utilitiesucvxpy.errorr   cvxpy.expressions.expressionr    cvxpy.interface.matrix_utilitiesr   r   	Canonicalr   r.   r/   rI   r   r   <module>r      sQ     % $    3 9 "X& X&vCy CLDy Dr   