
    bi_/                         d Z ddlZddlZddlmZ ddlmZ ddl	m
c mZ ddefdZddefdZd Zd Zd	 Zd
 Zd ZddedefdZd Zd Zd Zde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.

THIS FILE IS DEPRECATED AND MAY BE REMOVED WITHOUT WARNING!
DO NOT CALL THESE FUNCTIONS IN YOUR CODE!
    N)fftconvolveis_absc                 N   | j                   t        j                  u rs| j                  |v r3|r"t	        j
                  || j                           S || j                     S t	        j                  t	        j                  | j                              S | j                   t        j                  u r2t	        j                  t	        j                  | j                              S g }| j                  D ]  }|j                  t        |||               |rt        | |      S t        | |      S )ai  Multiply the expression tree by a vector.

    Parameters
    ----------
    lin_op : LinOp
        The root of an expression tree.
    val_dict : dict
        A map of variable id to value.
    is_abs : bool, optional
        Multiply by the absolute value of the matrix?

    Returns
    -------
    NumPy matrix
        The result of the multiplication.
    )typeloVARIABLEdatanpabsasmatrixzerosshapeNO_OPargsappendmul
op_abs_mulop_mul)lin_opval_dictr   	eval_argsargs        Q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/lin_ops/tree_mat.pyr   r      s    $ {{bkk!;;("vvhv{{344,, ;;rxx566		 {{288FLL122	;; 	9CSh78	9fi00&),,    c                 <   | j                   t        j                  u r| j                  |iS | j                   t        j                  u ri S |rt        | |      }nt        | |      }g }| j                  D ]  }|j                  t        |||               t        |      S )ap  Multiply the transpose of the expression tree by a vector.

    Parameters
    ----------
    lin_op : LinOp
        The root of an expression tree.
    value : NumPy matrix
        The vector to multiply by.
    is_abs : bool, optional
        Multiply by the absolute value of the matrix?

    Returns
    -------
    dict
        A map of variable id to value.
    )r   r   r   r	   r   op_abs_tmulop_tmulr   r   tmul	sum_dicts)r   valuer   resultresult_dictsr   s         r   r   r   H   s    $ {{bkk!U##		 	 /FVU+F;; 	;CS&& 9:	; &&r   c                 r    i }| D ]/  }|j                         D ]  \  }}||v r||   |z   ||<   |||<    1 |S )zSums the dictionaries entrywise.

    Parameters
    ----------
    dicts : list
        A list of dictionaries with numeric entries.

    Returns
    -------
    dict
        A dict with the sum.
    )items)dictssum_dictr   id_r    s        r   r   r   k   s[     H &"..* 	&JCh ( 5 %		&& Or   c                    | j                   t        j                  t        j                  t        j                  fv r| j
                  }|S | j                   t        j                  u ry| j                   t        j                  u rt        |      }|S | j                   t        j                  u r|d    }|S | j                   t        j                  u r t        | j
                  i       }||d   z  }|S | j                   t        j                  u r t        | j
                  i       }|d   |z  }|S | j                   t        j                  u rt        j                  |d         }|S | j                   t        j                  u r| j
                  \  }}|d   ||f   }|S | j                   t        j                   u r|d   j"                  }|S | j                   t        j$                  u rt'        | |d         }|S | j                   t        j(                  u r't        j*                  | j,                        |d   z  }|S | j                   t        j.                  u r/t1        j2                  |d         }t        j4                  |      }|S t7        d      )a,  Applies the linear operator to the arguments.

    Parameters
    ----------
    lin_op : LinOp
        A linear operator.
    args : list
        The arguments to the operator.

    Returns
    -------
    NumPy matrix or SciPy sparse matrix.
        The result of applying the linear operator.
    Nr   Unknown linear operator.)r   r   SCALAR_CONSTDENSE_CONSTSPARSE_CONSTr	   r   SUMsumNEGMULr   DIVSUM_ENTRIESr
   INDEX	TRANSPOSETCONVconv_mulPROMOTEonesr   DIAG_VECintffrom_2D_to_1Ddiag	Exception)r   r   r!   coeffdivisorrow_slccol_slcvals           r   r   r      s'     {{rHH> M; 
	 		T2 M1 
	q'. M- 
	FKK$tAw( M' 
	fkk2&a" M! 
	&Q M 
	 !;;a')* M 
	$a M 
	&$q'* M 


	"&tAw. M 
	#  a) M 233r   c                 n   | j                   t        j                  t        j                  t        j                  fv r!t        j                  | j                        }|S | j                   t        j                  u r|d   }|S | j                   t        j                  u r!t        | j                  i d      }||d   z  }|S | j                   t        j                  u r!t        | j                  i d      }|d   |z  }|S | j                   t        j                  u rt        | |d   d      }|S t        | |      }|S )aB  Applies the absolute value of the linear operator to the arguments.

    Parameters
    ----------
    lin_op : LinOp
        A linear operator.
    args : list
        The arguments to the operator.

    Returns
    -------
    NumPy matrix or SciPy sparse matrix.
        The result of applying the linear operator.
    r   T)r   )r   r   r*   r+   r,   r
   r   r	   r/   r0   r   r1   r6   r7   r   )r   r   r!   r?   r@   s        r   r   r      s     {{rHH$ M 
	a M 
	FKKT*tAw M 
	fkk2t,a
 M	 
	&$q'$7 M %Mr   c                    | j                   t        j                  u r|}|S | j                   t        j                  u r| }|S | j                   t        j                  u rCt        | j                  i       }t        j                  |      r||z  }|S |j                  |z  }|S | j                   t        j                  u rt        | j                  i       }||z  }|S | j                   t        j                  u rDt        j                  t        j                  | j                  d   j                              |z  }|S | j                   t        j                   u rW| j                  \  }}t        j                  t        j"                  | j                  d   j                              }||||f<   |S | j                   t        j$                  u r|j                  }|S | j                   t        j&                  u r3t        j                  | j                  d         j)                  |      }|S | j                   t        j*                  u rIt        j,                  |      }t/        |t        j0                        rt        j2                  |      d   }|S | j                   t        j4                  u rt7        | |d      }|S t9        d      )a]  Applies the transpose of the linear operator to the arguments.

    Parameters
    ----------
    lin_op : LinOp
        A linear operator.
    value : NumPy matrix
        A numeric value to apply the operator's transpose to.

    Returns
    -------
    NumPy matrix or SciPy sparse matrix.
        The result of applying the linear operator.
    r   T)	transposer)   )r   r   r-   r/   r0   r   r	   r
   isscalarr5   r1   r2   r   r9   r   r   r3   r   r4   r8   dotr:   r=   
isinstancematrixasarrayr6   r7   r>   )r   r    r!   r?   r@   rA   rB   s          r   r   r      s=    {{bff@ M? 
	< M; 
	FKK$;;u5[F2 M/ WWU]F. M- 
	fkk2&w( M' 
	&RWWV[[^%9%9:;EA$ M# 
	 !;;RXXfkk!n&:&:;<#(w  M 
	$ M 


	"a)--e4 M 
	#fbii(ZZ'*F
 M	 
	&%48 M 233r   c                    | j                   t        j                  u r|}|S | j                   t        j                  u rDt	        | j
                  i d      }t        j                  |      r||z  }|S |j                  |z  }|S | j                   t        j                  u rt	        | j
                  i d      }||z  }|S | j                   t        j                  u rt        | |dd      }|S t        | |      }|S )aR  Applies the linear operator |A.T| to the arguments.

    Parameters
    ----------
    lin_op : LinOp
        A linear operator.
    value : NumPy matrix
        A numeric value to apply the operator's transpose to.

    Returns
    -------
    NumPy matrix or SciPy sparse matrix.
        The result of applying the linear operator.
    T)r   r   r/   r0   r   r	   r
   rG   r5   r1   r6   r7   r   )r   r    r!   r?   r@   s        r   r   r     s     {{bff  M 
	FKKT*;;u5[F M WWU]F M 
	fkk2t,w
 M	 
	&%t4 M 'Mr   rF   c                 "   t        | j                  i |      }t        t        j                  ||g      \  }}|r#t        j                  |      }t        ||d      S |j                  |j                  k\  rt        ||d      S t        ||d      S )a  Multiply by a convolution operator.

    arameters
    ----------
    lin_op : LinOp
        The root linear operator.
    rh_val : NDArray
        The vector being convolved.
    transpose : bool
        Is the transpose of convolution being applied?
    is_abs : bool
        Is the absolute value of convolution being applied?

    Returns
    -------
    NumPy NDArray
        The convolution.
    valid)modefull)	r   r	   mapr;   from_1D_to_2Dr
   flipudr   size)r   rh_valrF   r   constants        r   r7   r7   .  s    & 6;;F+H4--&/ABHf99X&68':: ==FKK'xf==vxf==r   c                     t        | i       }|j                  d   |j                  d   z  }t        j                  ||d      S )zReturns the constant term in the expression.

    Parameters
    ----------
    lin_op : LinOp
        The root linear operator.

    Returns
    -------
    NumPy NDArray
        The constant term as a flattened vector.
    r      F)r   r   r
   reshape)r   rV   
const_sizes      r   get_constantr\   P  s>     62H"8>>!#44J::h
C00r   c                 z    | D cg c]  }t        |j                         }}t        j                  |      S c c}w )zReturns the constant term for the constraints matrix.

    Parameters
    ----------
    constraints : list
        The constraints that form the matrix.

    Returns
    -------
    NumPy NDArray
        The constant term as a flattened vector.
    )r\   exprr
   hstack)constraintsc	constantss      r   get_constr_constantrc   b  s4     0;;!aff%;I;99Y <s   8c                 D   g }| D ]  }t        |      }t        j                  |j                        }t	        |      }|r0t        j                  t
        j                  |j                  g d      } |||j                  |j                        }|j                  |        |S )zReturns a new list of constraints with constant terms removed.

    Parameters
    ----------
    constraints : list
        The constraints that form the matrix.

    Returns
    -------
    list
        The pruned constraints.
    N)r   copydeepcopyr^   
prune_exprr   LinOpr   r   	constr_idr   )r`   pruned_constraintsconstrconstr_typer^   is_constantpruneds          r   prune_constantsro   t  s      *6l}}V[[) &88BHHdjj"d;DT6#3#3V\\B!!&)* r   returnc                 f   | j                   t        j                  u ry| j                   t        j                  t        j                  t        j
                  t        j                  fv ryg }d}| j                  D ]#  }t        |      }|rd}|j                  |       % |dd | j                  dd |S )zPrunes constant branches from the expression.

    Parameters
    ----------
    lin_op : LinOp
        The root linear operator.

    Returns
    -------
    bool
        Were all the expression's arguments pruned?
    FTN)
r   r   r   r*   r+   r,   PARAMr   rg   r   )r   pruned_argsrm   r   arg_constants        r   rg   rg     s     {{bkk!	# 
# KK{{ $!#Ks#	$ !^FKKNr   )F)FF)__doc__re   numpyr
   scipy.signalr   cvxpy.interface	interfacer;   cvxpy.lin_ops.lin_oplin_opsr   r   boolr   r   r   r   r   r   r   r7   r\   rc   ro   rg    r   r   <module>r~      s   $   $  ! !&-$ &-R '  'F00fD0f F> >d >D1$ $4$ r   