
    bi$                     f    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  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.
    )problems)cvxtypes)
Expression)InverseData)	Reduction)Solutionc                   X     e Zd ZdZd
d fdZd Zd ZddefdZ	 dde	de
defd	Z xZS )Canonicalizationa  Recursively canonicalize each expression in a problem.

    This reduction recursively canonicalizes every expression tree in a
    problem, visiting each node. At every node, this reduction first
    canonicalizes its arguments; it then canonicalizes the node, using the
    canonicalized arguments.

    The attribute `canon_methods` is a dictionary
    mapping node types to functions that canonicalize them; the signature
    of these canonicalizing functions must be

        def canon_func(expr, canon_args) --> (new_expr, constraints)

    where `expr` is the `Expression` (node) to canonicalize, canon_args
    is a list of the canonicalized arguments of this expression,
    `new_expr` is a canonicalized expression, and `constraints` is a list
    of constraints introduced while canonicalizing `expr`.

    Attributes:
    ----------
        canon_methods : dict
            A dictionary mapping node types to canonicalization functions.
        problem : Problem
            A problem owned by this reduction.
    c                 <    t         t        |   |       || _        y )N)problem)superr
   __init__canon_methods)selfr   r   	__class__s      \/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/canonicalization.pyr   zCanonicalization.__init__3   s    .w.?*    c                 Z   t        |      }| j                  |j                        \  }}|j                  D ]P  }| j                  |      \  }}|||gz   z  }|j                  j                  |j                  |j                  i       R t        j                  j                  ||      }||fS )z<Recursively canonicalize the objective and every constraint.)
r   canonicalize_tree	objective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Canonicalization.apply7   s    "7+-1-C-C.** "-- 	NJ
 (,'='=($L*|n!<<$$++Z]]LOO,LM	N &&../@BL((r   c                 l   |j                   D ci c]   }||j                  v r||j                  |   " }}|j                  j                         D ci c]#  \  }}||j                  v r||j                  |   % }}}t        |j                  |j                  |||j                        S c c}w c c}}w N)	id_mapprimal_varsr   items	dual_varsr   statusopt_valattr)r   solutionr   vidpvarsorig_iddvarss          r   invertzCanonicalization.invertL   s    ;G;N;N 1C8/// h**3// 1 1 &2%=%=%C%C%E/!Wc8--- (,,S11 / / )9)95% ' 	'1/s   %B+(B0canonicalize_paramsc                    t        |      t        j                         k(  rx| j                  |j                  d   j
                  j                  |      \  }}|j                  d   j                  D ]!  }| j                  ||      \  }}||g|z   z  }# ||fS g }g }|j                  D ]#  }	| j                  |	|      \  }
}||
gz  }||z  }% | j                  |||      \  }}||z  }||fS )a4  Recursively canonicalize an Expression.
        
        Args:
            expr: Expression to canonicalize.
            canonicalize_params: Should constant subtrees 
                containing parameters be canonicalized?
        
        Returns:
            canonicalized expression, constraints
        r   )r3   )	typer   partial_problemr   argsr   exprr   canonicalize_expr)r   r8   r3   
canon_exprconstrsconstrr    r!   
canon_argsarg	canon_argcs               r   r   z"Canonicalization.canonicalize_treeV   s;    :1133"&"8"8iil$$))?R #9 #TJ))A,22 7+/+A+A(; ,B ,(j L>J667, 7"" JGyy #55(;  6  	1 yk)
1 !22$7 3 MJ
 qLG7""r   r8   r7   c                    t        |t              r*|xr |j                         }|j                         xr | }nd}|r|g fS t	        |      | j
                  v r | j
                  t	        |         ||      S |j                  |      g fS )aw  Canonicalize an expression, w.r.t. canonicalized arguments.
        
        Args:
            expr: Expression to canonicalize.
            args: Arguments to the expression.
            canonicalize_params: Should constant subtrees 
                containing parameters be canonicalized?
        
        Returns:
            canonicalized expression, constraints
        F)
isinstancer   
parametersis_constantr5   r   copy)r   r8   r7   r3   canon_with_params
skip_canons         r   r9   z"Canonicalization.canonicalize_expr}   s    " dJ' !4 I8I))+E4E0EJJ8O:+++14%%d4j1$==99T?B&&r   r%   )returnN)T)__name__
__module____qualname____doc__r   r#   r2   boolr   r   listr9   __classcell__)r   s   @r   r
   r
      sJ    2+)*'%#4 %#V )-	'' ' "&	'r   r
   N)rL   cvxpyr   cvxpy.expressionsr   cvxpy.expressions.expressionr   cvxpy.reductions.inverse_datar   cvxpy.reductions.reductionr   cvxpy.reductions.solutionr   r
    r   r   <module>rW      s,      & 3 5 0 .C'y C'r   