
    bi                         d Z ddlm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mZ ddlmZ dd	lmZ d
 Zd Zd Zd Zd ZdefdZd Z G d d      Zy)a,  
Copyright 2018 Akshay Agrawal

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.
    )defaultdict)TupleN)reshape)vec)NonNegNonPos)Zero)canonInterfacec                 r    | j                   d   }| j                   d   }t        ||z
  | j                        S Nr      	constr_id)argsr   r   
inequalitylhsrhss      U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/utilities.pylower_ineq_to_nonposr      5    
//!
C
//!
C#)z';';<<    c                 r    | j                   d   }| j                   d   }t        ||z
  | j                        S r   )r   r   r   r   s      r   lower_ineq_to_nonnegr   $   r   r   c                 r    | j                   d   }| j                   d   }t        ||z
  | j                        S r   )r   r	   r   )equalityr   r   s      r   lower_equalityr   *   s5    
--
C
--
Cc	X%7%788r   c                 F    t        | j                   | j                        S )Nr   )r   exprr   )nonposs    r   nonpos2nonnegr!   0   s    6;;,&*:*:;;r   c                    | j                   }| j                   j                  }t        j                  ||j                  d      }|d   }t        j                  |j                  d      }t        ||   t        |d      z  |d      }|g fS )NF)orderr   csc)format)_select_matshapenpr   sizesp	eye_arrayr   )r   r   
select_matfinal_shape
select_vecargidentitylowereds           r   special_index_canonr3   4   s{    !!J""((KJ
sCJ
q'C||CHHU3Hhz*SC-@@+UXYGB;r   returnc                 &    t        d | D              S )Nc              3   ^   K   | ]%  }|j                   D ]  }|j                           ' y wN)r   	is_affine).0constrr0   s      r   	<genexpr>z"are_args_affine.<locals>.<genexpr>@   s3      '6 ++' }} ' 's   +-)all)constraintss    r   are_args_affiner>   ?   s     '[ ' ' 'r   c                 l    t        t              }| D ]  }|t        |         j                  |       ! |S )a?  Organize the constraints into a dictionary keyed by constraint names.

    Parameters
    ---------
    constraints : list of constraints

    Returns
    -------
    dict
        A dict keyed by constraint types where dict[cone_type] maps to a list
        of exactly those constraints that are of type cone_type.
    )r   listtypeappend)r=   
constr_mapcs      r   group_constraintsrE   D   s:     T"J &47""1%&r   c                   b    e Zd ZdZddededdfdZddeddfdZdd	ej                  d
ede
fdZy)
ReducedMata  Utility class for condensing the mapping from parameters to problem data.

    For maximum efficiency of representation and application, the mapping from
    parameters to problem data must be condensed. It begins as a CSC sparse matrix
    matrix_data, such that multiplying by a parameter vector gives the problem data.
    The row index array and column pointer array are saved as problem_data_index,
    and a CSR matrix reduced_mat that when multiplied by a parameter vector gives
    the values array. The ReducedMat class caches the condensed representation
    and provides a method for multiplying by a parameter vector.

    This class consolidates code from ParamConeProg and ParamQuadProg.

    Attributes
    ----------
    matrix_data : SciPy CSC sparse matrix
       A matrix representing the mapping from parameter to problem data.
    var_len : int
       The length of the problem variable.
    quad_form: (optional) if True, consider quadratic form matrix P
    var_len	quad_formr4   Nc                 X    || _         || _        || _        d | _        d | _        d | _        y r7   )matrix_datarH   rI   reduced_matproblem_data_indexmapping_nonzero)selfrK   rH   rI   s       r   __init__zReducedMat.__init__m   s6    &"   #'  $r   
keep_zerosc                    | j                   y| j                  t        j                  | j                   j                        dk7  rLt        j                  | j                   | j                  | j                        \  }}}}|| _        |||f| _	        n| j                   | _        d| _	        |r=| j                  0t        j                  | j                   | j                        | _
        yyy)zCache computed attributes if not present.

        Parameters
        ----------
            keep_zeros: (optional) if True, store explicit zeros in A where
                        parameters are affected.
        Nr   )rK   rL   r)   prodr(   r
   reduce_problem_data_tensorrH   rI   rM   rN   A_mapping_nonzero_rows)rO   rQ   rL   indicesindptrr(   s         r   cachezReducedMat.cache|   s     ## wwt''--.!3"==(($,,H 4Wfe $/ +2FE*B'#'#3#3 *.'$..6#1#H#H  $,,$0D  7:r   	param_vecwith_offsetc                     t        j                  | j                  || j                  | j                  || j
                        S )a  Wraps get_matrix_from_tensor in canonInterface.

        Parameters
        ----------
            param_vec: flattened parameter vector
            with_offset: (optional) return offset. Defaults to True.

        Returns
        -------
            A tuple (A, b), where A is a matrix with `var_length` columns
            and b is a flattened NumPy array representing the constant offset.
            If with_offset=False, returned b is None.
        )nonzero_rowsrZ   rM   )r
   get_matrix_from_tensorrL   rH   rN   rM   )rO   rY   rZ   s      r   r]   z!ReducedMat.get_matrix_from_tensor   s=     44i--##66	8 	8r   )F)T)__name__
__module____qualname____doc__intboolrP   rX   r)   ndarrayr   r]    r   r   rG   rG   W   sS    *$S $T $d $0 0 0:8

 8 8Y^ 8r   rG   )ra   collectionsr   typingr   numpyr)   scipy.sparsesparser+   cvxpy.atoms.affine.reshaper   cvxpy.atoms.affine.vecr   cvxpy.constraints.nonposr   r   cvxpy.constraints.zeror	   cvxpy.cvxcore.pythonr
   r   r   r   r!   r3   rc   r>   rE   rG   re   r   r   <module>rp      s]     $    . & 3 ' /==9<'D '
&T8 T8r   