
    biV                        d Z ddlmZ ddlZddl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lmZ ddlmZmZ dd	lmZ  G d
 de      Z 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.
    )annotationsN)ListLiteralTuple)AffAtom)hstack)
Constraint)DEFAULT_ORDER_DEPRECATION_MSG
Expression)size_from_shapec                       e Zd ZdZ	 d	 	 	 	 	 d fdZedd       ZddZddZe	j                  d        ZddZddZd	 Z	 d	 	 	 dd
Z xZS )reshapeat  
    Reshapes the expression.

    Vectorizes the expression then unvectorizes it into the new shape.
    The entries are reshaped and stored in column-major order, also known
    as Fortran order.

    Parameters
    ----------
    expr : Expression
       The expression to reshape
    shape : tuple or int
        The shape to reshape to
    order : F(ortran) or C
    c                   t        |t        j                        rt        |      f}t        j
                  st        |      dkD  rt        d      t        d |D              r| j                  ||j                        }t        |      | _        |2t        j                  dd      }t        j                   |t"               d}|dv sJ || _        t&        t(        | W  |       y )N   z:Expressions of dimension greater than 2 are not supported.c              3  &   K   | ]	  }|d k(    ywN ).0ds     U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/reshape.py	<genexpr>z#reshape.__init__.<locals>.<genexpr>>   s     &1qBw&s   	FUNC_NAMEr   F)r   C)
isinstancenumbersIntegralintsALLOW_ND_EXPRlen
ValueErrorany_infer_shapesizetuple_shaper
   replacewarningswarnFutureWarningordersuperr   __init__)selfexprshaper-   reshape_order_warning	__class__s        r   r/   zreshape.__init__3   s     eW--.ZME3u:> 2 3 3&&&%%eTYY7EEl=$A$I$I+W`$a!MM/?E
"""
gt%d+    c                F   | j                  d      dk(  sJ d       t        |       dk(  r|f} | S | j                  d      }| d|z
     dk\  sJ d       t        j                  || d|z
           \  }|dk7  rt        d| d|  d      t        fd	| D              } | S )
Nr      zOnly one dimension can be -1.r   z(Specified dimension must be nonnegative.z"Cannot reshape expression of size z into shape .c              3  0   K   | ]  }|d k(  rn  ywr   r   )r   r   	specifiedunspecifieds     r   r   z'reshape._infer_shape.<locals>.<genexpr>X   s     OAb+i?Os   )countr"   indexnpdivmodr#   r'   )r2   r&   unspecified_index	remainderr:   r;   s       @@r   r%   zreshape._infer_shapeJ   s    {{2!#D%DD#u:?GE  !&Ba"334I>M#MM>%'YYtU1?P;P5Q%R"KA~ 8l5'QRS  OOOEr5   c                     y)z$Is the atom log-log convex?
        Tr   r0   s    r   is_atom_log_log_convexzreshape.is_atom_log_log_convex[        r5   c                     y)z%Is the atom log-log concave?
        Tr   rC   s    r   is_atom_log_log_concavezreshape.is_atom_log_log_concave`   rE   r5   c                `    t        j                  |d   | j                  | j                        S )zReshape the value.
        r   r-   )r>   r   r2   r-   )r0   valuess     r   numericzreshape.numerice   s#     zz&)TZZtzzBBr5   c                    | j                   d   j                  }t        | j                        }||k(  st	        d| j                  d      y)zMChecks that the new shape has the same number of entries as the old.
        r   zInvalid reshape dimensions r8   N)argsr&   r   r(   r#   )r0   old_lennew_lens      r   validate_argumentszreshape.validate_argumentsk   sI     ))A,##!$++.'!48KKA  "r5   c                    | j                   S )z$Returns the shape argument.
        )r(   rC   s    r   shape_from_argszreshape.shape_from_argsu   s     {{r5   c                2    | j                   | j                  gS )zLReturns info needed to reconstruct the expression besides the args.
        )r(   r-   rC   s    r   get_datazreshape.get_dataz   s     TZZ((r5   c                (   |d   }|d   dk(  rt        j                  ||      g fS t        j                  |      }t        |      dk  rt        j                  ||      g fS t        j                  ||ddd         }t        j                  |      g fS )af  Reshape

        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   r7   r   Nr   )lur   	transposer"   )r0   arg_objsr2   dataargresults         r   graph_implementationzreshape.graph_implementation   s    & qk7c>JJsE*B//,,s#C5zQ

3.33Ctt5V,b11r5   )N)r2   zint | Tuple[int, ...]r-   zLiteral['F', 'C', None]returnNone)r2   Tuple[int, ...]r&   r   r]   r_   )r]   bool)r]   r^   )r]   r_   )r2   r_   r]   z!Tuple[lo.LinOp, List[Constraint]])__name__
__module____qualname____doc__r/   staticmethodr%   rD   rG   r   numpy_numericrK   rP   rR   rT   r\   __classcell__)r4   s   @r   r   r   "   s    ( *.	, %, '	,
 
,.   

 C C

) 6:2.2	*2r5   r   c                   t        | t              r,t        | j                        dk(  r| S | j	                  d      S t        | t
        j                        st        | t        t        f      r't        j                  |       } | j	                  d      S t        | t              r2g }| D ]  }t        |      }|j                  |         t        |      }|S ddt        |       z  z   }t        |      )Nr7   r   rI   zFThe input to deep_flatten must be an Expression, a NumPy array, an intz> or float, or a nested list thereof. Received input of type %s)r   r   r"   r2   flattenr>   ndarrayr   floatcast_to_constlistdeep_flattenappendr   typer#   )xyx0x1msgs        r   rn   rn      s    !Z qww<1H9939''	Arzz	"jS%L&A$$Q'yysy##!T 	Bb!BHHRL	 1I
RLtTUwVWC
S/r5   )"rd   
__future__r   r   r*   typingr   r   r   numpyr>   cvxpy.lin_ops.lin_oplin_opslin_oplocvxpy.lin_ops.lin_utils	lin_utilsrV   cvxpy.settingssettingsr    cvxpy.atoms.affine.affine_atomr   cvxpy.atoms.affine.hstackr   cvxpy.constraints.constraintr	   cvxpy.expressions.expressionr
   r   cvxpy.utilities.shaper   r   rn   r   r5   r   <module>r      sP    #   ' '  ! ! $ $  2 , 3 R 1y2g y2xr5   