
    bi                     j    d Z ddlZddlmZ ddl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)
a+  
Copyright, the CVXPY authors

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)sparse)problems)PSD)SOC)	Reductionc                   "    e Zd ZdZd Zd Zd Zy)SOC2PSDz?Convert all SOC constraints to equivalent PSD constraints.
    c                      y)NT )selfproblems     ]/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/cone2cone/soc2psd.pyacceptszSOC2PSD.accepts   s        c                    g }g }|j                   D ]6  }t        |      t        u r|j                  |       &|j                  |       8 g }g }i }|D ]  }	 |j                  \  }}	|j                  |j
                         	 |j                  dk(  r|d   }
|	j                  d   }	 |
t        j                  d      z  }t        j                  |	ddgd      j                  }|
t        j                  |      z  }	 	 t        j                  ||g|j                  |gg      }	 t        |      }|j
                  ||j
                  <   |j                  |       |j                  dk(  r|	j                  }	t        |j                  d         D ]  }||   }
|	j                  d   }|
t        j                  d      z  }|	d d ||dz   f   j                  }|
t        j                  |      z  }t        j                  ||g|j                  |gg      }t        |      }|j
                  ||j
                  <   |j                  |         t         j"                  j%                  |j&                  ||z         }||f}||fS )N)   r   r   F)order)constraintstyper   appendargsidshaper   	eye_arraycpreshapeTbmatr   axisranger   r   Problem	objective)r   r   soc_constraintsother_constraints
constraintpsd_constraintssoc_constraint_idssoc_id_from_psdtXscalar_termvector_term_lenABCMnew_psd_constraintsubidxnew_probleminverse_datas                       r   applyzSOC2PSD.apply"   s   !-- 	5JJ3&&&z2!((4		5 ) H	?J ??DAq%%jmm4 ww}d"#''!*  &"2"21"55JJq"Qs355&"2"2?"CC GGFSS!H 
 &)V"9C 2 5 56&&'9:??A%A#AGGAJ/ ?F"#F)K&'ggajO#f&6&6q&99A!F6!8O+,..A#f&6&6&GGAAa! A
 *-Q&=G]]O$6$9$9:#**+=>?sH	?T &&..w/@/@/@?/RT ();<L((r   c                 z   |j                   i k(  r|S |\  }}|j                         }i }|D ]  }g ||<   	 |j                   D ]F  }||v r.|j                   |   }	|	d   }
||   }||   j                  |
       5|j                   |   ||<   H |D ]%  }||v sdt        j                  ||         z  ||<   ' ||_         |S )a  
        `solution.dual_vars` contains dual variables corresponding to the constraints.

        The dual variables that we return in `solution` should correspond to the original
        SOC constraints, and not their PSD equivalents. To this end, inversion is required.
        r      )	dual_varskeysr   nphstack)r   solutionr6   r*   r)   psd_constraint_idsinverted_dual_vars	constr_idvar_idpsd_dual_varsoc_dual_var
soc_var_ids               r   invertzSOC2PSD.invert   s    r!O.:++,113+ 	/I,.y)	/ (( 	HF++'11&9
  ,A -V4
":.55lC .6-?-?-G"6*	H& ) 	WF++ ./;Mf;U1V-V"6*		W 0r   N)__name__
__module____qualname____doc__r   r7   rF   r   r   r   r	   r	      s    \)|,r   r	   )rJ   numpyr<   scipyr   cvxpyr   r   cvxpy.constraints.psdr   cvxpy.constraints.second_orderr   cvxpy.reductions.reductionr   r	   r   r   r   <module>rQ      s/         % . 0Qi Qr   