
    bi                        d Z ddlmZ ddlmZmZmZ ddlZddl	m
c mZ ddlm
c mZ ddlmZ ddlmZ ddlmZ dddZ G d	 d
e      Z G d de      Zy)a,  
Copyright 2013 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.
    )annotations)ListTupleUnionN)AffAtom)vec)
Constraintc                H   t        j                  |       } | j                         rt        t	        | d      |      S | j
                  dk(  rM| j                  d   | j                  d   k(  r.t        |      | j                  d   k  sJ d       t        | |      S t        d      )a  Extracts the diagonal from a matrix or makes a vector a diagonal matrix.

    Parameters
    ----------
    expr : Expression or numeric constant
        A vector or square matrix.

    k : int
        Diagonal in question. The default is 0.
        Use k>0 for diagonals above the main diagonal,
        and k<0 for diagonals below the main diagonal.

    Returns
    -------
    Expression
        An Expression representing the diagonal vector/matrix.
    F)order   r      zOffset out of bounds.z9Argument to diag must be a 1-d array or 2-d square array.)
r   cast_to_const	is_vectordiag_vecr   ndimshapeabsdiag_mat
ValueError)exprks     R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/diag.pydiagr      s    $   &D~~D,a00	aDJJqMTZZ]:1v

1%>'>>%a  TUU    c                       e Zd ZdZdd fdZddZddZddZddZd Z	ddZ
dd	Zdd
ZddZddZ	 d	 	 	 ddZ xZS )r   z)Converts a vector into a diagonal matrix.c                :    || _         t        t        |   |       y N)r   superr   __init__selfr   r   	__class__s      r   r    zdiag_vec.__init__<       h&t,r   c                    | j                   gS r   r   r"   s    r   get_datazdiag_vec.get_data@       xr   c                R    | j                   d   j                  dk(  st        d      y)z.Checks that the argument is a vector.
        r   r   z(Argument to diag_vec must be a 1-d arrayNargsr   r   r'   s    r   validate_argumentszdiag_vec.validate_argumentsC   s/     yy|  A%:  &r   c                     yz$Is the atom log-log convex?
        T r'   s    r   is_atom_log_log_convexzdiag_vec.is_atom_log_log_convexK        r   c                     yz%Is the atom log-log concave?
        Tr0   r'   s    r   is_atom_log_log_concavez diag_vec.is_atom_log_log_concaveP   r2   r   c                J    t        j                  |d   | j                        S )z<Convert the vector constant into a diagonal matrix.
        r   r&   npr   r   r"   valuess     r   numericzdiag_vec.numericU        wwvayDFF++r   c                n    | j                   d   j                  d   t        | j                        z   }||fS )zA square matrix.
        r   r,   r   r   r   )r"   rowss     r   shape_from_argszdiag_vec.shape_from_argsZ   s3     yy|!!!$s466{2d|r   c                     | j                   dk(  S )z%Is the expression symmetric?
        r   r&   r'   s    r   is_symmetriczdiag_vec.is_symmetric`        vv{r   c                     | j                   dk(  S )z%Is the expression hermitian?
        r   r&   r'   s    r   is_hermitianzdiag_vec.is_hermitiane   rC   r   c                D    | j                         xr | j                  dk(  S )z:Is the expression a positive semidefinite matrix?
        r   )	is_nonnegr   r'   s    r   is_psdzdiag_vec.is_psdj        ~~/DFFaK/r   c                D    | j                         xr | j                  dk(  S )z:Is the expression a negative semidefinite matrix?
        r   )	is_nonposr   r'   s    r   is_nsdzdiag_vec.is_nsdo   rI   r   c                L    t        j                  |d   | j                        g fS )at  Convolve two vectors.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : tuple
            The shape of the resulting expression.
        data :
            Additional data required by the atom.

        Returns
        -------
        tuple
            (LinOp for objective, list of constraints)
        r   )lur   r   r"   arg_objsr   datas       r   graph_implementationzdiag_vec.graph_implementationt   "    & HQK0"55r   r   r   intreturnNonerW   z	list[int]rW   rX   rW   bool)rW   zTuple[int, int]r   r   zTuple[int, ...]rW   z!Tuple[lo.LinOp, List[Constraint]])__name__
__module____qualname____doc__r    r(   r-   r1   r5   r;   r@   rB   rE   rH   rL   rR   __classcell__r#   s   @r   r   r   9   sW    3-

,


0
0 6:6.6	*6r   r   c                       e Zd ZdZdd fdZddZddZddZddZe	j                  dd       ZddZdd	Z	 d	 	 	 dd
Z xZS )r   z0Extracts the diagonal from a square matrix.
    c                :    || _         t        t        |   |       y r   )r   r   r   r    r!   s      r   r    zdiag_mat.__init__   r$   r   c                    | j                   gS r   r&   r'   s    r   r(   zdiag_mat.get_data   r)   r   c                R    | j                   d   j                  dk(  st        d      y)z5Checks that the argument is a square matrix.
        r   r   z)Argument to diag_mat must be a 2-d array.Nr+   r'   s    r   r-   zdiag_mat.validate_arguments   s/     yy|  A%;  &r   c                     yr/   r0   r'   s    r   r1   zdiag_mat.is_atom_log_log_convex   r2   r   c                     yr4   r0   r'   s    r   r5   z diag_mat.is_atom_log_log_concave   r2   r   c                J    t        j                  |d   | j                        S )z3Extract the diagonal from a square matrix constant.r   r&   r7   r9   s     r   r;   zdiag_mat.numeric   r<   r   c                p    | j                   d   j                  \  }}|t        | j                        z  }|fS )zA column vector.r   r>   )r"   r?   _s      r   r@   zdiag_mat.shape_from_args   s2    ))A,$$aDFFwr   c                    | j                   d   j                         xs | j                   d   j                         xr | j                  dk(  S )zIs the expression nonnegative?r   )r,   rG   rH   r   r'   s    r   rG   zdiag_mat.is_nonneg   s=    		!&&(ADIIaL,?,?,ARtvvQR{Rr   c                L    t        j                  |d   | j                        g fS )a  Extracts the diagonal of a matrix.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : tuple
            The shape of the resulting expression.
        data :
            Additional data required by the atom.

        Returns
        -------
        tuple
            (LinOp for objective, list of constraints)
        r   )rN   r   r   rO   s       r   rR   zdiag_mat.graph_implementation   rS   r   rT   rU   rY   rZ   r[   )rW   z
np.ndarray)rW   z
Tuple[int]r   r]   )r^   r_   r`   ra   r    r(   r-   r1   r5   r   numpy_numericr;   r@   rG   rR   rb   rc   s   @r   r   r      s`    -

 , ,S
 6:6.6	*6r   r   rT   )r   rV   rW   zUnion['diag_mat', 'diag_vec'])ra   
__future__r   typingr   r   r   numpyr8   cvxpy.lin_ops.lin_oplin_opslin_oplocvxpy.lin_ops.lin_utils	lin_utilsrN   cvxpy.atoms.affine.affine_atomr   cvxpy.atoms.affine.vecr   cvxpy.constraints.constraintr	   r   r   r   r0   r   r   <module>r|      sN    # % %  ! ! $ $ 2 & 3V8N6w N6b?6w ?6r   