
    bi                         d Z ddlmZmZ ddlZddlm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lmZ ddlmZ  G d	 d
e      ZddefdZdedej<                  fd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.
    )ListTupleN)AffAtom)reshape)vec)
Constraint)
Expressionc                        e Zd ZdZd fdZej                  d        ZddZde	e
e
f   fdZdefdZdefdZ	 dd	e	e
d
f   de	ej                   ee   f   fdZ xZS )	upper_tria  
    The vectorized strictly upper-triangular entries.

    The vectorization is performed by concatenating (partial) rows.
    For example, if

    ::

        A = np.array([[10, 11, 12, 13],
                      [14, 15, 16, 17],
                      [18, 19, 20, 21],
                      [22, 23, 24, 25]])

    then we have

    ::

        upper_tri(A).value == np.array([11, 12, 13, 16, 17, 21])

    returnc                 ,    t         t        |   |       y N)superr   __init__)selfexpr	__class__s     W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/upper_tri.pyr   zupper_tri.__init__4   s    i'-    c                     t        j                  |d   j                  d   d|d   j                  d         }|d   |   S )zB
        Vectorize the strictly upper triangular entries.
        r      )nkm)nptriu_indicesshape)r   values	upper_idxs      r   numericzupper_tri.numeric7   s@    
 OOfQiooa&8AQRAST	ay##r   c                     | j                   d   j                  dk(  r9| j                   d   j                  d   | j                   d   j                  d   k7  rt        d      y)z5Checks that the argument is a square matrix.
        r      r   z1Argument to upper_tri must be a 2-d square array.N)argsndimr   
ValueErrorr   s    r   validate_argumentszupper_tri.validate_arguments?   s^     yy|  A%1););A)>$))A,BTBTUVBW)WC  *Xr   c                 T    | j                   d   j                  \  }}||dz
  z  dz  dfS )zA vector.
        r   r   r"   )r#   r   )r   rowscolss      r   shape_from_argszupper_tri.shape_from_argsG   s3     YYq\''
dd1fq !$$r   c                      y)z$Is the atom log-log convex?
        T r&   s    r   is_atom_log_log_convexz upper_tri.is_atom_log_log_convexM        r   c                      y)z%Is the atom log-log concave?
        Tr-   r&   s    r   is_atom_log_log_concavez!upper_tri.is_atom_log_log_concaveR   r/   r   r   .c                 6    t        j                  |d         g fS )a  Vectorized strictly upper triangular entries.

        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   arg_objsr   datas       r   graph_implementationzupper_tri.graph_implementationW   s    & Xa[)2..r   )r   Nr   )__name__
__module____qualname____doc__r   r   numpy_numericr    r'   r   intr+   boolr.   r1   loLinOpr   r   r6   __classcell__)r   s   @r   r   r      s    *. $ $%sCx % 
  6:/$S#X/	rxxj))	*/r   r   strictc                    t        j                  |       } | j                         st        d      | j                  dk7  rt        | d      } | j                  d   }|rd|z  dz   dz  dz   dz  }nd|z  dz   dz  dz
  dz  }t        |      }||dz   z  dz  |k(  s||dz
  z  dz  |k(  st        d	      	 |rdnd}t        j                  ||
      \  }}||z  |z   }t        j                  |      }t        j                  |j                        }	t        j                  |	||ff||z  |f      }
t        |
| z  ||fd      j                   S )zReshapes a vector into an upper triangular matrix in
    row-major order. The strict argument specifies whether an upper or a strict upper triangular
    matrix should be returned.
    Inverts cp.upper_tri.
    zThe input must be a vector.r   F)orderr      g      ?r"   z3The size of the vector must be a triangular number.)r   r   )r	   cast_to_const	is_vectorr%   r$   r   r   r<   r   r   arangeonessizesp	csc_arrayr   T)r   rA   ellr   r   row_idxcol_idxP_rowsP_colsP_valsPs              r   vec_to_upper_trirV   m   sZ    ##D)D>>677yyA~4s#
**Q-C#gkc!A%!+ #gkc!A%!+AAQK1#qAE{a'73'>NOO
 AqA.GW[7"FYYs^FWWV[[!F
fvv./As|DA1t8aV3/111r   r   r   c                    | | dz   z  dz  }t        j                  |       \  }}||k7  }t        j                  || z  |z   ||   | z  ||   z   g      }t        j                  t        j                  |      t        j                  |      |   g      }t        j                  |j
                  t              }t        j                  |||ff| | z  |f      S )aI  
    Returns a coefficient matrix A that creates a symmetric matrix when
    multiplied with a variable vector v.
    That is, (A @ v).reshape((n, n)) is a symmetric matrix.

    Parameters
    ----------
    n : int
        The length of the matrix.

    Returns
    -------
    sp.csc_array
        The coefficient matrix.
    r   r"   )dtyperF   )	r   r   concatenaterI   rJ   rK   floatrL   rM   )r   entriesr)   r*   maskrP   rQ   r   s           r   upper_tri_to_fullr]      s      1gqjG #JD$ 4<DnndQhotDzA~T
/JKLGnnbii0"))G2DT2JKLGWWW\\/F <<'7!34QUG<LMMr   )F) r:   typingr   r   numpyr   scipy.sparsesparserL   cvxpy.lin_ops.lin_oplin_opslin_opr>   cvxpy.lin_ops.lin_utils	lin_utilsr3   cvxpy.atoms.affine.affine_atomr   cvxpy.atoms.affine.reshaper   cvxpy.atoms.affine.vecr   cvxpy.constraints.constraintr   cvxpy.expressions.expressionr	   r   r=   rV   r<   rM   r]   r-   r   r   <module>rl      sf       ! ! $ $ 2 . & 3 3L/ L/^#24 #2LN N Nr   