
    bi                         d Z ddlmZmZmZ ddlZddlmZ de	de
deej                     fdZde	de
deej                     fd	Zdeee
   ee
   f   fd
Z G d de      Zy)aD  
Copyright 2017 Robin Verschueren, 2017 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.
    )ListOptionalTupleN)	Reduction	variablesvar_sizereturnc                    t        | D cg c]  }|j                          c}      }|syt        j                  |t        j                         }d}| D ]  }|j                         rd||||j                  z    nU|j                  d   F|j                  d   d   }t        j                  ||j                  d      }|||||j                  z    ||j                  z  } |S c c}w )zCoalesces lower bounds for the variables.

    Parameters
    ----------
    variables: A list of the variables present in the problem.
    var_size: Size of the coalesced variable.
    Nr   boundsForder)	any_has_lower_boundsnpfullinf	is_nonnegsize
attributesreshape)	r   r   varbounds_presentlower_boundsvert_offsetxvar_lower_bound	flatteneds	            [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/matrix_stuffing.pyextract_lower_boundsr       s     YGc#//1GHN778bffW-LK ;;=;<L[%78\\(#/ll84Q7O

?AFF#FI;DL[%78qvv  Hs   C"c                    t        | D cg c]  }|j                          c}      }|syt        j                  |t        j                        }d}| D ]  }|j                         rd||||j                  z    nU|j                  d   F|j                  d   d   }t        j                  ||j                  d      }|||||j                  z    ||j                  z  } |S c c}w )zCoalesces upper bounds for the variables.

    Parameters
    ----------
    variables: A list of the variables present in the problem.
    var_size: Size of the coalesced variable.
    Nr   r      r   r   )	r   _has_upper_boundsr   r   r   	is_nonposr   r   r   )	r   r   r   r   upper_boundsr   r   var_upper_boundr   s	            r   extract_upper_boundsr'   4   s     YGc#//1GHN778RVV,LK ;;=;<L[%78\\(#/ll84Q7O

?AFF#FI;DL[%78qvv  Hs   C!c                    g g d}}}| D ]  }t        |j                  d      }|j                  r9t        j                  |j                  |d      }||D cg c]  }||z   f
 c}z  }|j
                  r9t        j                  |j
                  |d      }||D cg c]  }||z   f
 c}z  }||j                  z  } ||fS c c}w c c}w )z
    Coalesces bool, int indices for variables.
    The indexing scheme assumes that the variables will be coalesced into
    a single one-dimensional variable, with each variable being reshaped
    in Fortran order.
    r   )r"   r   r   )maxshapeboolean_idxr   ravel_multi_indexinteger_idxr   )r   r+   r-   offsetr   ravel_shape	ravel_idxidxs           r   extract_mip_idxr2   O   s     (*2qfK !''4(==,,Q]]KsSIyAS6\OAAK==,,Q]]KsSIyAS6\OAAK!&& ## B Bs   CCc                   $    e Zd ZdZddZd Zd Zy)MatrixStuffingz>Stuffs a problem into a standard form for a family of solvers.Nc                      y)a  Returns a stuffed problem.

        The returned problem is a minimization problem in which every
        constraint in the problem has affine arguments that are expressed in
        the form A @ x + b.


        Parameters
        ----------
        problem: The problem to stuff; the arguments of every constraint
            must be affine

        Returns
        -------
        Problem
            The stuffed problem
        InverseData
            Data for solution retrieval
        N )selfproblems     r   applyzMatrixStuffing.applyf   s        c                     t               NNotImplementedError)r7   solutioninverse_datas      r   invertzMatrixStuffing.invertz       !##r:   c                     t               r<   r=   )r7   r8   r@   s      r   stuffed_objectivez MatrixStuffing.stuffed_objective}   rB   r:   )r	   N)__name__
__module____qualname____doc__r9   rA   rD   r6   r:   r   r4   r4   c   s    H($$r:   r4   )rH   typingr   r   r   numpyr   cvxpy.reductions.reductionr   listintndarrayr    r'   r2   r4   r6   r:   r   <module>rO      s   " ) (  0D C HRZZ<P 6D C HRZZ<P 6$%S	49(<"= $($Y $r:   