
    biL                        d Z ddlmZmZ ddlZddlmc mZ	 ddl
mZ ddlmZmZ  G d d      Z e       ZdefdZd5d	eed
f   fdZd5d	eed
f   fdZd6d	eed
f   defdZdefdZdefdZd Zd Zd Zd Zd	eed
f   fdZd	eed
f   fdZd Z d	eed
f   fdZ!d	eed
f   fdZ"d Z#d	eed
f   fdZ$d	eed
f   fdZ%d7d	eed
f   fdZ&d Z'd	eed
f   fdZ(d	eed
f   fd Z)d5d!Z*d	eed
f   fd"Z+d8d#efd$Z,d8d#efd%Z-d& Z.d	eed
f   fd'Z/d	eed
f   fd(Z0d8d	eed
f   d)ee   fd*Z1d+ Z2d7d,Z3d7d-Z4d7d.Z5d/ Z6d0 Z7d1 Z8d2 Z9d3 Z:d4 Z;y)9a  
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.

THIS FILE IS DEPRECATED AND MAY BE REMOVED WITHOUT WARNING!
DO NOT CALL THESE FUNCTIONS IN YOUR CODE!
    )OptionalTupleN)LinEqConstrLinLeqConstrc                       e Zd ZdZddZy)CounterzaA counter for ids.

    Attributes
    ----------
    count : int
        The current count.
    Nc                     d| _         y )N   )count)selfs    R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/lin_ops/lin_utils.py__init__zCounter.__init__'   s	    
    )returnN)__name__
__module____qualname____doc__r    r   r   r   r      s    r   r   r   c                  X    t         j                  } t         xj                  dz  c_        | S )zdReturns a new id and updates the id counter.

    Returns
    -------
    int
        A new id.
    r
   )
ID_COUNTERr   )new_ids    r   get_idr   .   s%     FMr   shape.c                 f    |
t               }t        j                  t        j                  | g |      S )a  Creates a new internal variable.

    Parameters
    ----------
    shape : tuple
        The (rows, cols) dimensions of the variable.
    var_id : int
        The id of the variable.

    Returns
    -------
    LinOP
        A LinOp representing the new variable.
    )r   loLinOpVARIABLE)r   var_ids     r   
create_varr    ;   s)     ~88BKKF33r   c                 f    |
t               }t        j                  t        j                  | g |      S )zWraps a parameter.

    Parameters
    ----------
    shape : tuple
        The (rows, cols) dimensions of the operator.

    Returns
    -------
    LinOP
        A LinOp wrapping the parameter.
    )r   r   r   PARAM)r   param_ids     r   create_paramr$   O   s*     888BHHeR22r   sparsec                     |dk(  r+t         j                  }t        j                  |       s)| d   } n#|rt         j                  }nt         j
                  }t        j                  ||g |       S )ag  Wraps a constant.

    Parameters
    ----------
    value : scalar, NumPy matrix, or SciPy sparse matrix.
        The numeric constant to wrap.
    shape : tuple
        The (rows, cols) dimensions of the constant.
    sparse : bool
        Is the constant a SciPy sparse matrix?

    Returns
    -------
    LinOP
        A LinOp wrapping the constant.
    r
   r
   )r   r   )r   SCALAR_CONSTnpisscalarSPARSE_CONSTDENSE_CONSTr   )valuer   r%   op_types       r   create_constr/   a   sS    $ //{{5!$KE	//..88GUB..r   c                     t        | j                        dk(  xs( t        j                  | j                  t              dk(  S )zReturns whether a LinOp is a scalar.

    Parameters
    ----------
    operator : LinOp
        The LinOp to test.

    Returns
    -------
        True if the LinOp is a scalar, False otherwise.
    r   )dtyper
   )lenr   r)   prodintoperators    r   	is_scalarr7      s1     x~~!#Nrwwx~~S'IQ'NNr   c                     | j                   t        j                  t        j                  t        j                  t        j
                  fv S )zReturns whether a LinOp is constant.

    Parameters
    ----------
    operator : LinOp
        The LinOp to test.

    Returns
    -------
        True if the LinOp is a constant, False otherwise.
    )typer   r(   r+   r,   r"   r5   s    r   is_constr:      s5     ==R____^^XX' ' 'r   c                 h    t        j                  t         j                  | d   j                  | d      S )zAdd linear operators.

    Parameters
    ----------
    operators : list
        A list of linear operators.

    Returns
    -------
    LinOp
        A LinOp representing the sum of the operators.
    r   N)r   r   SUMr   )	operatorss    r   sum_exprr>      s'     88BFFIaL..	4@@r   c                 d    t        j                  t         j                  | j                  | gd      S )zNegate an operator.

    Parameters
    ----------
    expr : LinOp
        The operator to be negated.

    Returns
    -------
    LinOp
        The negated operator.
    N)r   r   NEGr   r5   s    r   neg_exprrA      s#     88BFFHNNXJ==r   c                 .    t        | t        |      g      S )a-  Difference of linear operators.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the difference.
    rh_op : LinOp
        The right-hand operator in the difference.

    Returns
    -------
    LinOp
        A LinOp representing the difference of the operators.
    r>   rA   lh_oprh_ops     r   sub_exprrG      s     UHUO,--r   c                 T   t         j                  j                  | j                  |j                        \  }}}t        j                  | j
                  || j                  | j                        } t        j                  |j
                  ||j                  |j                        }| ||fS )av  Promote arguments for multiplication.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the multiplication.
    rh_op : LinOp
        The right-hand operator in the multiplication.

    Returns
    -------
    LinOp
       Promoted left-hand operator.
    LinOp
       Promoted right-hand operator.
    tuple
       Shape of the product
    )ur   mul_shapes_promoter   r   r9   argsdata)rE   rF   lh_shaperh_shaper   s        r   promote_lin_ops_for_mulrO      s    & !" : :U[[!"HhHHUZZ5::ZZ!EHHUZZ5::ZZ!E%r   c                 P    t        j                  t         j                  ||g|       S )a;  Multiply two linear operators, with the constant on the left.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the product.
    rh_op : LinOp
        The right-hand operator in the product.

    Returns
    -------
    LinOp
        A linear operator representing the product.
    )r   r   MULrE   rF   r   s      r   mul_exprrS      s     88BFFEE7E22r   c                 P    t        j                  t         j                  || g|      S )ap  Multiply two linear operators, with the constant on the right.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the product.
    rh_op : LinOp
        The right-hand operator in the product.
    shape : tuple
        The shape of the product.

    Returns
    -------
    LinOp
        A linear operator representing the product.
    )r   r   RMULrR   s      r   	rmul_exprrV          " 88BGGUUGU33r   c                     t        | j                  |j                        }t        j                  t        j                  ||g|       S )a(  Multiply two linear operators elementwise.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the product.
    rh_op : LinOp
        The right-hand operator in the product.

    Returns
    -------
    LinOp
        A linear operator representing the product.
    )maxr   r   r   MUL_ELEMrR   s      r   multiplyr[     s3     U[[)E88BKK77r   c                 P    t        j                  t         j                  ||g|       S )aP  Kronecker product of two matrices, where the right operand is a Variable

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the product.
    rh_op : LinOp
        The right-hand operator in the product.

    Returns
    -------
    LinOp
        A linear operator representing the Kronecker product.
    )r   r   KRON_RrR   s      r   kron_rr^   '       88BIIuugu55r   c                 P    t        j                  t         j                  || g|      S )aO  Kronecker product of two matrices, where the left operand is a Variable

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the product.
    rh_op : LinOp
        The right-hand operator in the product.

    Returns
    -------
    LinOp
        A linear operator representing the Kronecker product.
    )r   r   KRON_LrR   s      r   kron_lrb   9  r_   r   c                 d    t        j                  t         j                  | j                  | g|      S )a  Divide one linear operator by another.

    Assumes rh_op is a scalar constant.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the quotient.
    rh_op : LinOp
        The right-hand operator in the quotient.
    shape : tuple
        The shape of the quotient.

    Returns
    -------
    LinOp
        A linear operator representing the quotient.
    )r   r   DIVr   rD   s     r   div_exprre   K  s#    & 88BFFEKK%%88r   c                 P    t        j                  t         j                  || gd      S )a  Promotes a scalar operator to the given shape.

    Parameters
    ----------
    operator : LinOp
        The operator to promote.
    shape : tuple
        The dimensions to promote to.

    Returns
    -------
    LinOp
        A linear operator representing the promotion.
    N)r   r   PROMOTEr6   r   s     r   promoteri   a       88BJJz488r   c                 N    t        j                  t         j                  || g       S )z,Broadcasts operators to a common shape.
    )r   r   BROADCAST_TOr=   r   s     r   broadcast_torn   s  s     88BOOUIr::r   c                 V    t        j                  t         j                  || g||g      S )zSum the entries of an operator.

    Parameters
    ----------
    expr : LinOp
        The operator to sum the entries of.
    shape : tuple
        The shape of the sum.

    Returns
    -------
    LinOp
        An operator representing the sum.
    rL   )r   r   SUM_ENTRIES)r6   r   axiskeepdimss       r   sum_entriesrt   y  s$     88BNNEH:T8<LMMr   c                 P    t        j                  t         j                  d| gd      S )zSum the diagonal entries of an operator.

    Parameters
    ----------
    expr : LinOp
        The operator to sum the diagonal entries of.

    Returns
    -------
    LinOp
        An operator representing the sum of the diagonal entries.
    r'   N)r   r   TRACEr5   s    r   tracerw     s     88BHHfxj$77r   c                 P    t        j                  t         j                  || g|      S )a@  Indexes/slices an operator.

    Parameters
    ----------
    operator : LinOp
        The expression to index.
    keys : tuple
        (row slice, column slice)
    shape : tuple
        The shape of the expression after indexing.

    Returns
    -------
    LinOp
        An operator representing the indexing.
    )r   r   INDEX)r6   r   keyss      r   indexr{     s    " 88BHHehZ66r   c                 P    t        j                  t         j                  ||g|       S )ai  1D discrete convolution of two vectors.

    Parameters
    ----------
    lh_op : LinOp
        The left-hand operator in the convolution.
    rh_op : LinOp
        The right-hand operator in the convolution.
    shape : tuple
        The shape of the convolution.

    Returns
    -------
    LinOp
        A linear operator representing the convolution.
    )r   r   CONVrR   s      r   convr~     rW   r   c                      t         j                        dk  r S | j                  ddd   }nt         fd|D              }t        j                  t        j
                  | g|g      S )a  Transposes an operator.

    Parameters
    ----------
    operator : LinOp
        The operator to transpose.
    axes : None or tuple of ints
        If specified, it must be a tuple or list which contains a permutation
        of [0,1,..,N-1] where N is the number of dimensions of the operator.

    Returns
    -------
    LinOp
       A linear operator representing the transpose.
       Nc              3   <   K   | ]  }j                   |     y wN)r   ).0ir6   s     r   	<genexpr>ztranspose.<locals>.<genexpr>  s     :(..+:s   rp   )r2   r   tupler   r   	TRANSPOSE)r6   axesr   s   `  r   	transposer     s_      8>>Q<NN4R4(E:T::ExxehZtfEEr   c                 P    t        j                  t         j                  || gd      S )a
  Reshapes an operator.

    Parameters
    ----------
    operator : LinOp
        The operator to reshape.
    shape : tuple
        The (rows, cols) of the reshaped operator.

    Returns
    -------
    LinOp
       LinOp representing the reshaped expression.
    N)r   r   RESHAPErh   s     r   reshaper     rj   r   kc                     | j                   d   t        |      z   }||f}t        j                  t        j                  || g|      S )a  Converts a vector to a diagonal matrix.

    Parameters
    ----------
    operator : LinOp
        The operator to convert to a diagonal matrix.
    k : int
        The offset of the diagonal.

    Returns
    -------
    LinOp
       LinOp representing the diagonal matrix.
    r   )r   absr   r   DIAG_VEC)r6   r   rowsr   s       r   diag_vecr     s?     >>!s1v%D4LE88BKK
A66r   c                     | j                   d   t        |      z
  df}t        j                  t        j                  || g|      S )a  Converts the diagonal of a matrix to a vector.

    Parameters
    ----------
    operator : LinOp
        The operator to convert to a vector.
    k : int
        The offset of the diagonal.

    Returns
    -------
    LinOp
       LinOp representing the matrix diagonal.
    r   r
   )r   r   r   r   DIAG_MAT)r6   r   r   s      r   diag_matr     s;     ^^AQ'+E88BKK
A66r   c                     | j                   d   | j                   d   z  }|| j                   d   z
  dz  df}t        j                  t        j                  || gd      S )zVectorized upper triangular portion of a square matrix.

    Parameters
    ----------
    operator : LinOp
        The matrix operator.

    Returns
    -------
    LinOp
       LinOp representing the vectorized upper triangle.
    r   r
   r   N)r   r   r   	UPPER_TRI)r6   entriesr   s      r   	upper_trir     sX     nnQq 11Gq))A-q1E88BLL%(T::r   c                 N    t        j                  t         j                  || d      S )a  Concatenates operators horizontally.

    Parameters
    ----------
    operator : list
        The operators to stack.
    shape : tuple
        The (rows, cols) of the stacked operators.

    Returns
    -------
    LinOp
       LinOp representing the stacked expression.
    N)r   r   HSTACKrm   s     r   hstackr   )       88BIIui66r   c                 N    t        j                  t         j                  || d      S )a  Concatenates operators vertically.

    Parameters
    ----------
    operator : list
        The operators to stack.
    shape : tuple
        The (rows, cols) of the stacked operators.

    Returns
    -------
    LinOp
       LinOp representing the stacked expression.
    N)r   r   VSTACKrm   s     r   vstackr   ;  r   r   rr   c                 P    t        j                  t         j                  || |g      S )ao  Concatenate operators on axis.

    Parameters
    ----------
    operator : list
        The operators to concatenate.
    shape : tuple
        The (rows, cols) of the concatenated operators.
    axis : int, optional
        The axis along which the operators will be joined.

    Returns
    -------
    LinOp
       LinOp representing the stacked expression.
    )r   r   CONCATENATE)r=   r   rr   s      r   concatenater   L  s    " 88BNNE9tf==r   c                 6    || S t        | t        |      g      S )z,Returns the operator in the constraint.
    rC   rD   s     r   get_constr_exprr   _  s"     }011r   c                 `    |
t               }t        | |      }t        ||| j                        S )aq  Creates an internal equality constraint.

    Parameters
    ----------
    lh_term : LinOp
        The left-hand operator in the equality constraint.
    rh_term : LinOp
        The right-hand operator in the equality constraint.
    constr_id : int
        The id of the CVXPY equality constraint creating the constraint.

    Returns
    -------
    LinEqConstr
    )r   r   r   r   rE   rF   	constr_idexprs       r   	create_eqr   i  s0      H	5%(DtY44r   c                 `    |
t               }t        | |      }t        ||| j                        S )ap  Creates an internal less than or equal constraint.

    Parameters
    ----------
    lh_term : LinOp
        The left-hand operator in the <= constraint.
    rh_term : LinOp
        The right-hand operator in the <= constraint.
    constr_id : int
        The id of the CVXPY equality constraint creating the constraint.

    Returns
    -------
    LinLeqConstr
    )r   r   r   r   r   s       r   
create_leqr     s0      H	5%(Di55r   c                 H    |t        |      }t        t        |       ||      S )as  Creates an internal greater than or equal constraint.

    Parameters
    ----------
    lh_term : LinOp
        The left-hand operator in the >= constraint.
    rh_term : LinOp
        The right-hand operator in the >= constraint.
    constr_id : int
        The id of the CVXPY equality constraint creating the constraint.

    Returns
    -------
    LinLeqConstr
    )rA   r   )rE   rF   r   s      r   
create_geqr     s&      huoui88r   c                     | j                   t        j                  k(  r| j                  | j                  fgS g }| j
                  D ]  }|t        |      z  } |S )zGet a list of the variables in the operator and their shapes.

    Parameters
    ----------
    operator : LinOp
        The operator to extract the variables from.

    Returns
    -------
    list
        A list of (var id, var shape) pairs.
    )r9   r   r   rL   r   rK   get_expr_vars)r6   vars_args      r   r   r     sV     }}#/00== 	(C]3''E	(r   c                 .   | j                   t        j                  k(  r| j                  j	                         S g }| j
                  D ]  }|t        |      z  } t        | j                  t        j                        r|t        | j                        z  }|S )zGet a list of the parameters in the operator.

    Parameters
    ----------
    operator : LinOp
        The operator to extract the parameters from.

    Returns
    -------
    list
        A list of parameter objects.
    )	r9   r   r"   rL   
parametersrK   get_expr_params
isinstancer   )r6   paramsr   s      r   r   r     sz     }} }}''))== 	+Coc**F	+ hmmRXX.ohmm44Fr   c                 t     || j                         } t        |       || j                  | j                        S )aO  Creates a copy of the constraint modified according to func.

    Parameters
    ----------
    constr : LinConstraint
        The constraint to modify.
    func : function
        Function to modify the constraint expression.

    Returns
    -------
    LinConstraint
        A copy of the constraint with the specified changes.
    )r   r9   r   r   )constrfuncr   s      r   copy_constrr     s1     D4<f..==r   c                 >   | j                   t        j                  k(  r| j                  |v r|| j                     S g }| j                  D ]  }|j                  t        ||              t        j                  | j                   | j                  || j                        S )a@  Replaces the given variables in the expression.

    Parameters
    ----------
    expr : LinOp
        The expression to replace variables in.
    id_to_new_var : dict
        A map of id to new variable.

    Returns
    -------
    LinOp
        An LinOp identical to expr, but with the given variables replaced.
    )	r9   r   r   rL   rK   appendreplace_new_varsr   r   )r   id_to_new_varnew_argsr   s       r   r   r     s     yyBKKDII$>TYY''99 	COO m4	 xx		4::xCCr   c                 8    | j                   }|t        d      |S )a*  Wrapper on accessing a parameter.

    Parameters
    ----------
    param : Parameter
        The parameter whose value is being accessed.

    Returns
    -------
    The numerical value of the parameter.

    Raises
    ------
    ValueError
        Raises error if parameter value is None.
    z$Problem has missing parameter value.)r-   
ValueError)paramvals     r   check_param_valr     s#    " ++C
{?@@
r   c                    | j                   t        j                  k(  r)t        t	        | j
                        | j                        S g }| j                  D ]  }|j                  t        |              t        | j
                  t        j                        r{| j
                  j                   t        j                  k(  rT| j
                  }t        |j                  t              sJ t	        |j
                        }t        ||j                        }n| j
                  }t        j                  | j                   | j                  ||      S )zReplaces parameters with constant nodes.

    Parameters
    ----------
    expr : LinOp
        The expression to replace parameters in.

    Returns
    -------
    LinOp
        An LinOp identical to expr, but with the parameters replaced.
    )r9   r   r"   r/   r   rL   r   rK   r   replace_params_with_constsr   r   r   )r   r   r   data_lin_opr   rL   s         r   r   r     s     yyBHHODII6

CC99 	=COO6s;<	= dii*tyy~~/I))Kk//777!+"2"23C[%6%67D99Dxx		4::x>>r   r   )F)NN)r   )<r   typingr   r   numpyr)   cvxpy.lin_ops.lin_oplin_opslin_opr   cvxpy.utilities	utilitiesrI   cvxpy.lin_ops.lin_constraintsr   r   r   r   r4   r   r    r$   boolr/   r7   r:   r>   rA   rG   rO   rS   rV   r[   r^   rb   re   ri   rn   rt   rw   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s%  $ #  ! !  C

 
 Y

 
4eCHo 4(3c3h 3$/uS#X / /<O4 O'$ '$A > .$83%S/ 3$45c? 4(8&6c3h 6$6c3h 6$9,9U38_ 9$;5c? ;NsCx N$8 75c? 7(4eCHo 4(F69U38_ 9$7# 7(7# 7&;$7U38_ 7$7U38_ 7">%S/ ># >&25,6,9*,2>&D40?r   