
    bi                         d 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
 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      Zy)aY  
Copyright 2013 Steven Diamond, 2017 Akshay Agrawal, 2017 Robin Verschueren

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.
    )Tuple)problems)quad_over_lin)cvxtypes)
Expression)Minimize)Canonicalization)CANON_METHODS)InverseData)QUAD_CANON_METHODSc                   n     e Zd ZdZddeddf fdZd Zd Zdedee	e
f   fd	Zdedee	e
f   fd
Z xZS )Dcp2ConezReduce DCP problems to a conic form.

    This reduction takes as input (minimization) DCP problems and converts
    them into problems with affine or quadratic objectives and conic
    constraints whose arguments are affine.
    Nquad_objreturnc                 h    t         t        |   |       t        | _        t        | _        || _        y )N)problem)superr	   __init__cone_canon_methodsquad_canon_methodsr   )selfr   r   	__class__s      ]/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/dcp2cone/dcp2cone.pyr   zDcp2Cone.__init__%   s-    .w.?"4"4     c                 ^    t        |j                        t        k(  xr |j                         S )zBA problem is accepted if it is a minimization and is DCP.
        )type	objectiver   is_dcp)r   r   s     r   acceptszDcp2Cone.accepts+   s&     G%%&(2Gw~~7GGr   c                    | j                  |      st        d      t        |      }| j                  |j                  d      \  }}|j
                  D ]Q  }| j                  |d      \  }}|||gz   z  }|j                  j                  |j                  |j                  i       S t        j                  j                  ||      }||fS )z0Converts a DCP problem to a conic form.
        z%Cannot reduce problem to cone programTF)r   
ValueErrorr   canonicalize_treer   constraintscons_id_mapupdateidr   r   Problem)	r   r   inverse_datacanon_objectivecanon_constraints
constraintcanon_constr
aux_constrnew_problems	            r   applyzDcp2Cone.apply0   s     ||G$DEE"7+-1-C-Ct.%** "-- 	NJ
 (,'='=E(#$L*|n!<<$$++Z]]LOO,LM	N &&../@BL((r   affine_abovec                    t        |      t        j                         k(  rv| j                  |j                  d   j
                  j                  d      \  }}|j                  d   j                  D ]   }| j                  |d      \  }}||g|z   z  }" ||fS t        |      | j                  v}g }	g }|j                  D ]&  }
| j                  |
|xr |      \  }}|	|gz  }	||z  }( | j                  ||	|      \  }}||z  }||fS )a@  Recursively canonicalize an Expression.

        Parameters
        ----------
        expr : The expression tree to canonicalize.
        affine_above : The path up to the root node is all affine atoms.

        Returns
        -------
        A tuple of the canonicalized expression and generated constraints.
        r   F)
r   r   partial_problemr"   argsr   exprr#   r   canonicalize_expr)r   r4   r0   
canon_exprconstrsconstrr,   r-   affine_atom
canon_argsarg	canon_argcs                r   r"   zDcp2Cone.canonicalize_treeI   s*    :1133"&"8"8iil$$))5#2J))A,22 7+/+A+A&%+P(jL>J667 7"" t*D,C,CCKJGyy #55c;;W<X	1yk)
1 !224\RMJqLG7""r   c                    t        |t              r$|j                         r|j                         s|g fS | j                  r|rt        |      | j                  v rt        |      t        j                         k(  r/|j                         s | j                  t        |         ||      S t        |      t        k(  r/|j                         s | j                  t        |         ||      S  | j                  t        |         ||      S t        |      | j                  v r | j                  t        |         ||      S |j                  |      g fS )a  Canonicalize an expression, w.r.t. canonicalized arguments.

        Parameters
        ----------
        expr : The expression tree to canonicalize.
        args : The canonicalized arguments of expr.
        affine_above : The path up to the root node is all affine atoms.

        Returns
        -------
        A tuple of the canonicalized expression and generated constraints.
        )
isinstancer   is_constant
parametersr   r   r   r   power_quadratic_powerr   r   is_quadraticcopy)r   r4   r3   r0   s       r   r5   zDcp2Cone.canonicalize_exprh   s    dJ'  "4??+<8O==\d4jD<S<S.SDzX^^--d6K6K6M:t..tDz:4FFd},T5F5F5H:t..tDz:4FF:t..tDz:4FF:00064**4:6tTBByy""r   )NF)__name__
__module____qualname____doc__boolr   r   r/   r   r   listr"   r5   __classcell__)r   s   @r   r   r      sa    !t ! !H
)2#D #U:tCS=T #>#$ #5UYIYCZ #r   r   N)rI   typingr   cvxpyr   cvxpy.atoms.quad_over_linr   cvxpy.expressionsr   cvxpy.expressions.expressionr   cvxpy.problems.objectiver   !cvxpy.reductions.canonicalizationr	   (cvxpy.reductions.dcp2cone.canonicalizersr
   r   cvxpy.reductions.inverse_datar   ,cvxpy.reductions.qp2quad_form.canonicalizersr   r   r    r   r   <module>rX      s9       3 & 3 - > X 5 ah# h#r   