
    bi                         d dl mZ d dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ e G d d             Zej                  d        Zd	 Zd
 Zd Zd Zd Zd Zy)    )	dataclassN)SymbolicQuadForm)TensorRepresentation)CoeffExtractorc                   J    e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   y)MockeInverseDatavar_offsetsx_length
var_shapesparam_shapesparam_to_sizeparam_id_mapN)__name__
__module____qualname__dict__annotations__int     [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_coeff_extractor.pyr   r      s%    Mr   r   c                  z    t        ddidddiddddddddddd	      } t        j                  }t        | |      S )
N   r      )r   r   r      )r   r   )r   r   r   )r	   r
   r   r   r   r   )r   cpCPP_CANON_BACKENDr   )inverset_databackends     r   coeff_extractorr"      sR    $Ft9^q)qa(M ""G-11r   c            	         t        j                  g d      } t        j                  g dg dg dg dg dg      }t        j                  d      }t        j                  dd	
      }t        j                  dd	
      }| |z  |t        j
                  ||      z  z
  |t        j                  |      z  z
  }t        j                  |      }t        j                  g d      }||k(  g}t        j                  ||      }	|	j                          | |z  |j                  t        j                  |t        j                  ||            z  z
  |j                  t        j                  t        j                  |            z  z
  }
t        j                  |	j                  |
      sJ t        j                  |j                  |      sJ y)z
    This is the problem reported in #2402, failing to solve when two parameters
    are used on quadratic forms with the same variable.
    gQ޿g)\(?
ףp=
?g(\ؿQ?giUMu/?C8
!?-C6*?h㈵$?r)   r(   gמY2?yCn+?瀟q@H&?a2U0*#?r)   r,   gdC?8*5{5?%.?r*   r-   r0   g#-#F?r,   r)   r.   r1   r,   g9̗7?         ?Tvaluenonneg        
               Nnparrayr   Variable	Parameter	quad_formsum_squaresMaximizeProblemsolver7   dotsumsquareiscloseallcloserSigmawrisk_aversion
ridge_coefobj_func	objectivefixed_wconstraintsprobexpected_values              r    test_issue_2402_scalar_parameterr[   $   s_    
5	6AHH555557 8E 	AALLr$7MBt4J1u}r||Au'===bnn]^N_A__HH%Ihh+,G<.K::i-DJJL[=#6#6PUW^I_9`#``266"))G"4556N ::djj.111;;qww(((r   c            	      P   t        j                  g d      } t        j                  g dg dg dg dg dg      }t        j                  d      }t        j                  dd	
      }d}| |z  |t        j
                  ||      z  z
  |t        j                  |      z  z
  }t        j                  |      }t        j                  g d      }||k(  g}t        j                  ||      }	|	j                          | |z  |j                  t        j                  |t        j                  ||            z  z
  |t        j                  t        j                  |            z  z
  }
t        j                  |	j                  |
      sJ t        j                  |j                  |      sJ y)z~
    This slight modification uses a constant instead of a parameter,
    which was a separate issue in the same problem.
    r$   r'   r+   r/   r2   r3   r4   r5   Tr6   r   r:   Nr@   rO   s              r   test_issue_2402_scalar_constantr]   D   sN    
5	6AHH555557 8E 	AALLr$7MJ1u}r||Au'===bnn]^N_A__HH%Ihh+,G<.K::i-DJJL[=#6#6PUW^I_9`#``RVVBIIg.//0N ::djj.111;;qww(((r   c                     t        j                  g d      } t        j                  g dg dg dg dg dg      }t        j                  d      }t        j                  dd	
      }t        j                  dt        j
                  d      d	
      }| |z  |t        j                  ||      z  z
  t        j                  t        j                  t        j                  |t        j                  g d            t        j                  |                  z
  }t        j                  |      }t        j                  g d      }||k(  g}t        j                  ||      }	|	j                          | |z  |j                  t        j                  |t        j                  ||            z  z
  t        j                  |j                  t        j                  g d      z  t        j                  |      z        z
  }
t        j                  |	j                  |
      sJ t        j                   |j                  |      sJ y)z
    This slight modification with the ridge_coef as a vector also failed
    with a different error due to a dimension mismatch.
    r$   r'   r+   r/   r2   r3   r4   g       @Tr6   )r4            	   r:   N)rA   rB   r   rC   rD   arangerE   rK   multiplyrL   rG   rH   rI   r7   rJ   rM   rN   rO   s              r   test_issue_2402_vectorre   d   s    
5	6AHH55555 E 	AALLr$7Mq1dCJ1u}r||Au'===
r{{2;;z288K3HI299UV<XYZH H%Ihh+,G<.K::i-DJJL[=#6#6PUW^I_9`#``
z"((;"77"))G:LLMNN ::djj.111;;qww(((r   c                  F   t        j                  d      } t        j                  dd      }t        j                  dd      }t        j                  d      }t        j
                  |t        j                  | |      z  |t        j                  |       z  z         }t        j                  |t        j                  |       dk(  g      }|j                          t        j                  |j                  d      sJ t        j                  | j                  ddg      sJ y	)
zJ
    This is a MWE / regression test for the issue reported in #2402.
    r   r5   Tr6   r9   r   )rX   g      ?N)r   rC   rD   rA   eyeMinimizerE   rF   rH   rK   rI   rM   r7   rN   )xp1p2PrV   problems         r   test_problem_end_to_endrn      s     	AA	C	-B	C	-B
q	ABa!33b2>>!;L6LLMIjja0@AGMMO::gmmS)));;qwwc
+++r   c                    t        j                  dd      }t        j                  dd      }t        j                  d      }t        j                  ddd	      }t        j                  d
dd	      }||z  ||z  z   }|j                  ||z  dt	        |t        j
                  t        j                  d            t        j                  |t        j                  d                  f|j                  ||z  dt	        |t        j
                  t        j                  d            t        j                  |d            fi}| j                  ||      \  }}	t        |      dk(  sJ t        j                  |d   d   j                         t        j                  d            sJ |d   d   }
t        |
t               sJ t        j                  |
j"                  t        j$                  d            sJ t        j                  |
j&                  t        j(                  g d            sJ t        j                  |
j*                  t        j(                  g d            sJ |
j,                  dk(  sJ t        j                  |
j.                  t        j(                  g d            sJ t        j                  |	j                         t        j                  d            sJ y)z~
    This is a unit test for the same problem.
    The variable and parameter namings are derived from the problem above.
    r   r   )var_id)r   r   r?      r5   T)r7   r8   idr9   r   qr   rl      )r   r   r   r   )r   r   )r   r   r   r   N)r   rC   rD   rr   r   ConstantrA   rg   rE   quad_over_linextract_quadratic_coeffslenrN   toarrayzeros
isinstancer   dataonesrowrB   colshapeparameter_offset)r"   x1x14x16rk   p3affine_expr
quad_formscoeffsconstantrl   s              r   test_coeff_extractorr      s-   
 
Qq	!B
++fR
(C
++R
 C	C	3B	C	3Bs(R#X%K 	HRRVVAY!7b"&&QR)9TU

 	HRRVVAY!79I9I"c9RS
J '??ZXFHv;!;;vay~--/&1ABBBq	#Aa-...;;qvvrww|,,,;;quubhh|4555;;quubhh|455577f;;q))288L+ABBB;;x'')288Q=999r   c                  T   d} t        j                  g d      }t        j                  g d      }t        j                  g d      }t        j                  | d      }t        j                  ||      dz  j                         }||z  |z
  t        j                  |      |z  z
  }t        j                  t        j                  |      g       }t        j                  |j                  t        j                  d	      |j                  t        j                  d
	      dd      sJ y)zJ
    This is a MWE / regression test for the issue reported in #2437.
    r   )g{Gz?g{Gz?r&   )g{Gz?g?gQ?)gQ?g{Gz?r%   rR   )namer   T)solveruse_quad_objFr   gMbP?)rtolatolN)rA   rB   r   rC   rd   rK   absrH   rG   rM   rI   CLARABEL)Nt_costalphaivolrR   riskUrm   s           r   test_issue_2437r      s    
 	
AXX()FHH'(E88&'D
AC AKK4 A%**,D	E	D266!9v--AjjQ,G::R[[t<R[[u=	  r   )dataclassesr   numpyrA   pytestcvxpyr   cvxpy.atoms.quad_formr   cvxpy.lin_ops.canon_backendr   cvxpy.utilities.coeff_extractorr   r   fixturer"   r[   r]   re   rn   r   r   r   r   r   <module>r      sm    !    2 < :    
2 
2)@)@ )F,"%:Pr   