
    bi                         d Z ddlmZ ddlZddlmZ ddlm	Z	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Zg dZg dZdee   fdZdee   fdZddefdZdej@                  fdZ! G d de      Z"y)a,  
Copyright 2017 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.
    )ListN)diagreshape)upper_tri_to_full)cvxtypes)Constant)Variable)	Reduction)Solution)
nonnegnonposposneg	symmetricr   PSDNSDboundssparsity)r   r   r   r   r   )r   r   r   returnc                 "    t        | t              S )zhReturns a list of the (constraint-generating) convex attributes present
       among the variables.
    )attributes_presentCONVEX_ATTRIBUTES)	variabless    [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/cvx_attr2constr.pyconvex_attributesr   =   s     i):;;    c                 Z    |D cg c]  t        fd| D              s c}S c c}w )zRReturns a list of the relevant attributes present
       among the variables.
    c              3   <   K   | ]  }|j                        y wN)
attributes).0vattrs     r   	<genexpr>z%attributes_present.<locals>.<genexpr>H   s      -;AQ\\$-? -;s   )any)r   attr_mapr#   s     `r   r   r   D   s5     & <T -;09-; *;D < < <s   ((projectc                 J   | j                   d   r%t        j                  |j                  d            S t	        | gt
              r| j                  d   }t        j                  | j                        }t        j                  |      }|j                  d      ||<   ||j                  z   t        j                  |j                               z
  S | j                   d   r0t        j                  | j                        }||| j                  <   |S |r| j                  |      S |S )Nr   Forderr   r   )r    spdiags_arrayflattenr   SYMMETRIC_ATTRIBUTESshapenpzerostriu_indicesTr   diagonal
sparse_idxr'   )variablelowered_valuer'   nvalueidxss         r   recover_value_for_variabler<   L   s    6"~~m33#3>??	XJ(<	=NN1(q!#++#+6duww)9!:::			Z	((%2h!!"	..r   c                     t        | gt              r%|t        j                  | j                  d            S | j
                  d   rt        j                  |      S |S )Nr   r   )r   r/   r1   r3   r0   r    r   )r7   r:   s     r   lower_valuer>   `   sM    8*&:;R__X^^A%6788			V	$wwu~r   c                   ^     e Zd ZdZd
deddf fdZdee   fdZdefdZ	d Z
defd	Z xZS )CvxAttr2Constrz3Expand convex variable attributes into constraints.Nreduce_boundsr   c                 <    || _         t        t        |   |       y)z=If reduce_bounds, reduce lower and upper bounds on variables.)problemN)rA   superr@   __init__)selfrC   rA   	__class__s      r   rE   zCvxAttr2Constr.__init__l   s    *nd,W,=r   c                 j    | j                   rt        S t        D cg c]  }|t        vs| c}S c c}w )z,Returns the attributes that will be reduced.)rA   r   BOUND_ATTRIBUTES)rF   r#   s     r   reduction_attributesz#CvxAttr2Constr.reduction_attributesq   s8    $$ "3dBR6R  s   00c                      y)NT )rF   rC   s     r   acceptszCvxAttr2Constr.acceptsz   s    r   c           
         t        |j                         t              s|dfS | j                         }i }i }i }g }|j                         D ]/  }|j                  |vs|||j                  <   d}|j
                  j                         }	|D ]  }
|	|
   s	d}|
dk(  rd nd|	|
<    t        |gt              r|j                  d   }||dz   z  dz  df}t        |fd|j                  i|	}|j                  |       |||j                  <   t        t        |            }||z  }t        |||fd	
      }n|j
                  d   rt        |j                  d         }t        |fd|j                  i|	}|j                  |       |||j                  <   t!        j"                  |j                  |j                  d	
      }t!        j$                  |      }t        t'        j(                  t!        j*                  |      ||fft!        j,                  |j                  t.              |f      d      }t        ||z  |j                  d	
      }n|j
                  d   rQt        |j                  d   fd|j                  i|	}|j                  |       |||j                  <   t1        |      }nV|rCt        |j                  fd|j                  i|	}|j                  |       |||j                  <   n|}|||j                  <   ||t	        |      <   |j3                         r|j5                  |dz	         n#|j
                  d   r|j5                  |dz         | j6                  s|j9                  ||       2 |j:                  j=                  |      }i }|j>                  D ]?  }|j5                  |j=                  |             |d   j                  ||j                  <   A |||f} tA        jB                         ||      |fS )NrL   FTr   r         var_idr)   r*   r   )dtype)r0   sparse_coeff)namer   r   )
id_objects)"r   r   r   rJ   idr    copyr/   r0   r	   set_variable_of_provenancer   r   r   lenr6   r1   ravel_multi_indexaranger,   	csc_arrayonesprodintr   is_psdappendrA   _bound_domain	objective	tree_copyconstraintsr   rC   )rF   rC   rJ   
id2new_var
id2new_obj
id2old_varconstrvarnew_varnew_attrkeyr9   r0   	upper_tri
fill_coefffull_matobj
sparse_varrow_idxcol_idxcoeff_matrixdiag_varcons_id_mapconsinverse_datas                            r   applyzCvxAttr2Constr.apply}   s   !'"3"3"57HIB;  $88: 


$$& 3	3CvvZ'%(
366">>..0/ KC}"&03xUK
 &se-AB		!A!WaZOE ( Jsvv J JI88=)2Jsvv&!)*;A*>!?J)I5H!(QF#>C^^J/CNN1-.A!)!!GCFF!Gh!GJ99#>)3Jsvv& 223>>399TWXG iilG#+BLL"''!*wPWFX9Y;=77399TW;XZ[:\-^9G$IL ",";SYYcRC^^F+'		!PSVVPxPH77<)1Jsvv&x.C"399HSVVHxHC2237),Jsvv&C),Jsvv&&)
2c7#::<MM#(+^^E*MM#(+%%%%c62g3	3l ))Z)@'' 	1DMM$..J.?@#)":==K 	1 #J<!x!#v.<<r   c                    |s|S |\  }}}i }|j                         D ]I  \  }}||   }	|	j                  |j                  v s$t        ||j                  |	j                           ||<   K |j                         D 
ci c]#  \  }
}||j                  v r|
|j                  |   % }}
}t        |j                  |j                  |||j                        S c c}}
w r   )	itemsrW   primal_varsr<   	dual_varsr   statusopt_valr#   )rF   solutionrz   rg   ri   rx   pvarsrW   rk   rl   orig_idviddvarss                r   invertzCvxAttr2Constr.invert   s    O.:+
J!'') 	;GB nGzzX1116--gjj9;b		; &1%6%6%8/!Wc8--- (,,S11 / / )9)95% ' 	'/s   <(C)NF)__name__
__module____qualname____doc__boolrE   r   strrJ   rM   r{   r   r   __classcell__)rG   s   @r   r@   r@   i   sH    =>D >T >
d3i $ H=T' 'r   r@   )T)#r   typingr   numpyr1   scipy.sparsesparser,   cvxpy.atomsr   r   cvxpy.atoms.affine.upper_trir   cvxpy.expressionsr   cvxpy.expressions.constantsr   cvxpy.expressions.variabler	   cvxpy.reductions.reductionr
   cvxpy.reductions.solutionr   r   rI   r/   listr   r   r   r   r<   ndarrayr>   r@   rL   r   r   <module>r      s        % : & 0 / 0 .   <DI <<tCy < (BJJ n'Y n'r   