
    bii                     \    d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d Z
d Z G d d	      Zy)
    N)sparseSuppFuncAtom)Variable)CONVEX_ATTRIBUTESc                    ddl m} ddlm} ddlm}  | | ||             |      }|j                  d      \  }}}|d   }	|	j                  | j                     }
t        j                  |
|
| j                  z         }|d   }t        j                  |j                  d	   ft        
      }d||<   |dd|f   }|dd| f   }t        j                   ||g       }|d   }|d   }|||fS )a  
    Return (A, b, K) so that
        {x : x satisfies constraints}
    can be written as
        {x : exists y where A @ [x; y] + b in K}.

    Parameters
    ----------
    x: cvxpy.Variable
    constraints: list of cvxpy.constraints.constraint.Constraint
        Each Constraint object must be DCP-compatible.

    Notes
    -----
    This function DOES NOT work when ``x`` has attributes, like ``PSD=True``,
    ``diag=True``, ``symmetric=True``, etc...
    r   )sum)Minimize)ProblemSCS)solverA   )shapedtypeTNbdims)cvxpy.atoms.affine.sumr	   cvxpy.problems.objectiver
   cvxpy.problems.problemr   get_problem_datavar_offsetsidnparangesizezerosr   boolr   hstack)xconstraintsr	   r
   r   probdatachaininvdatainvx_offset	x_indicesr   
x_selectorA_xA_otherr   Ks                     T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/transforms/suppfunc.pyscs_conicliftr/   	   s    $ +1.8CF#[1D
  000>D%
"+Cqtt$H		(Hqvv$56IS	AT:J Jy
AzM
CJ;G	W~	&&AS	AVAa7N    c                 6   | j                   rd}|dz  }t        |      | j                  }t        j                  ||| j
                  z         }|| j
                  z  }g }| j                  D ]1  }t        j                  |||z         }|j                  |       ||z  }3 g }| j                  D ]:  }||dz   z  dz  }	|j                  t        j                  |||	z                ||	z  }< d| j                  z  }
t        j                  |||
z         }||||d}|S )a   
    Parse a ConeDims object, as returned from SCS's apply function.

    Return a dictionary which gives row-wise information for the affine
    operator returned from SCS's apply function.

    Parameters
    ----------
    K : cvxpy.reductions.solvers.conic_solver.ConeDims

    Returns
    -------
    selectors : dict
        Keyed by strings, which specify cone types. Values are numpy
        arrays, or lists of numpy arrays. The numpy arrays give row indices
        of the affine operator (A, b) returned by SCS's apply function.
    z8SuppFunc doesn't yet support feasible sets represented 
zwith power cone constraints.r         )nonnegexpsocpsd)
p3dNotImplementedErrorzeror   r   r4   r6   appendr7   r5   )r-   msgidxnonneg_idxssoc_idxsr6   idxspsd_idxsr7   veclenexpsizeexp_idxs	selectorss                r.   scs_cone_selectorsrF   2   s#   $ 	uuI--!#&&
&&C))Cqxx0K188OCHuu yycCi(s
 Huu aA%		#sV|45v !%%iGyycGm,H	I r0   c                   0    e Zd ZdZd ZdefdZddZd Zy)	SuppFunca	  
    Given a list of CVXPY Constraint objects :math:`\texttt{constraints}`
    involving a real CVXPY Variable :math:`\texttt{x}`, consider the convex set

    .. math::

        S = \{ v : \text{it's possible to satisfy all } \texttt{constraints}
                    \text{ when } \texttt{x.value} = v \}.

    This object represents the *support function* of :math:`S`.
    This is the convex function

    .. math::

        y \mapsto \max\{ \langle y, v \rangle : v \in S \}.

    The support function is a fundamental object in convex analysis.
    It's extremely useful for expressing dual problems using
    `Fenchel duality <https://en.wikipedia.org/wiki/Fenchel%27s_duality_theorem>`_.

    Parameters
    ----------
    x : Variable
        This variable cannot have any attributes, such as PSD=True, nonneg=True,
        symmetric=True, etc...

    constraints : list[Constraint]
        Usually, these are constraints over :math:`\texttt{x}`, and some number of auxiliary
        CVXPY Variables. It is valid to supply :math:`\texttt{constraints = []}`.

    Examples
    --------
    If :math:`\texttt{h = cp.SuppFunc(x, constraints)}`, then you can use
    :math:`\texttt{h}` just like any other scalar-valued atom in CVXPY.
    For example, if :math:`\texttt{x}` was a CVXPY Variable with
    :math:`\texttt{x.ndim == 1}`, you could do the following:

    .. code::

        z = cp.Variable(shape=(10,))
        A = np.random.standard_normal((x.size, 10))
        c = np.random.rand(10)
        objective =  h(A @ z) - c @ z
        prob = cp.Problem(cp.Minimize(objective), [])
        prob.solve()

    Notes
    -----
    You are allowed to use CVXPY Variables other than :math:`\texttt{x}` to define
    :math:`\texttt{constraints}`, but the set :math:`S` only consists of objects
    (vectors or matrices) with the same shape as :math:`\texttt{x}`.

    It's possible for the support function to take the value :math:`+\infty`
    for a fixed vector :math:`\texttt{y}`. This is an important point, and
    it's one reason why support functions are actually formally defined with
    the supremum ":math:`\sup`" rather than the maximum ":math:`\max`".
    For more information on support functions, check out
    `this Wikipedia page <https://en.wikipedia.org/wiki/Support_function>`_.
    c                 H   t        t              st        d      t        fdt        D              rt        d      |D ]+  }|j                         }t        |      dkD  s"t        d       | _        || _        d | _	        d | _
        d | _        | j                          y )Nz?The first argument must be an unmodified cvxpy Variable object.c              3   <   K   | ]  }j                   |     y wN)
attributes).0attrr!   s     r.   	<genexpr>z$SuppFunc.__init__.<locals>.<genexpr>   s     @dq||D!@s   z7The first argument cannot have any declared attributes.r   z=Convex sets described with Parameter objects are not allowed.)
isinstancer   
ValueErroranyr   
parameterslenr!   r"   _A_b_K_sels_compute_conic_repr_of_set)selfr!   r"   con
con_paramss    `   r.   __init__zSuppFunc.__init__   s    !X&^__@.?@@VWW 	bC)J:" !`aa	b &'')r0   returnc                     t        ||       }|S )z
        Return an atom representing

            max{ cvxpy.vec(y) @ cvxpy.vec(x) : x in S }

        where S is the convex set associated with this SuppFunc object.
        r   )rY   y
sigma_at_ys      r.   __call__zSuppFunc.__call__   s     "!T*
r0   Nc                     t        | j                        dk(  rt               }|dk(  g}n| j                  }t        | j                  |      \  }}}t        |      }|| _        || _        || _        y )Nr   r   )	rT   r"   r   r/   r!   rF   rU   rV   rW   )rY   dummyconstrsr   r   r-   K_selss          r.   rX   z#SuppFunc._compute_conic_repr_of_set   sg    t A%JEzlG&&G01a#A&r0   c                 H    | j                   | j                  | j                  fS rK   )rU   rV   rW   )rY   s    r.   conic_repr_of_setzSuppFunc.conic_repr_of_set   s    ww--r0   )r]   N)	__name__
__module____qualname____doc__r\   r   ra   rX   rg    r0   r.   rH   rH   a   s#    :x* 	\ 	
.r0   rH   )numpyr   scipyr   cvxpy.atoms.suppfuncr   cvxpy.expressions.variabler    cvxpy.reductions.cvx_attr2constrr   r/   rF   rH   rl   r0   r.   <module>rr      s,      - / >&R,^e. e.r0   