
    bi                     :    d Z ddlZddlZddlmZ  G d de      Zy)a3  
Copyright 2022, the CVXPY developers

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.
    N)BaseTestc                       e Zd ZddZd Zy)TestGeoMeanNc           
         dt        j                         v rddi}nddi}d}d}t        j                  j	                  d       ddt        j                  j                  ||      z   z  }t        j                  ||fd	
      }t        j                  |d      dk  g}t        j                  t        j                  ||      d      }t        j                  g dg dg dg dg      }t        |      D ]  \  }	}
t        j                  |
t        j                  |      z        }t        j                  ||      } |j                  di | |j                  }t        j                  t        j                   ||
            }t        j                  ||      }|j                          |j                  }	 | j#                  ||d        y# t$        $ r}t'        d|	 dt)        |
       d        |j                  di |dd	i t'        |j                         |j                  d	       t'        |j                         t'        d       t'        |       |d}~ww xY w)a  
        Consider four market equilibrium problems.

        The budgets "b" in these problems are chosen so that canonicalization
        of geo_mean(u, b) hits a recursive code-path in power_tools.dyad_completion(...).

        The reference solution is computed by taking the log of the geo_mean objective,
        which has the effect of making the problem ExpCone representable.
        MOSEKsolverCLARABEL      r   g      ?   T)shapenonneg)axis)n         M   l   )g      .@g      @       @g        g      "@)g      ,@g      5@g      k@g     L@g      @)g      @g      B@g     @S@r   r      placesFailure at index z	 (when b=).verbose)r   zThe valuation matrix wasN )cpinstalled_solversnprandomseedrandVariablesummultiplyarray	enumerateMaximizelogProblemsolvevaluegeo_meanassertItemsAlmostEqualAssertionErrorprintstr)selflog_solve_argsn_buyern_itemsVXconsubsiblog_objectivelog_probexpect_Xgeo_objectivegeo_probactual_Xes                     W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_power_tools.pytest_multi_step_dyad_completionz+TestGeoMean.test_multi_step_dyad_completion   s    b**,,&0N&
3N
		q1ryy~~gw778KKw0>qq!Q&'FF2;;q!$1-XX!!%"	
  bM 	DAqKKBFF1I6Mzz-6HHNN,^,wwHKKAq(9:Mzz-6HNNwwH
++Hhq+I	 " )!Ic!fXR@A>>>aggt,agg01as   5G	IBIIc           	      :   dt        j                         v rddi}nddd}d}d}t        j                  d      }t        j                  j                  d	       d
t        j                  j                  d      z  }t        t        ||d            D ]b  \  }}||z  }|d	   |z  |d   d|z
  z  z  dz   |d<   t        j                  t        j                  ||z
  d            }	t         j                  j                  |d	   |d   |d   |g      g}
t        j                  |	|
      } |j                  di | |j                  j!                         }t        j"                  |d|z
  g      }t        j$                  |dd |      t        j&                  |d         k\  g}t        j                  |	|      }|j                          |j                  j!                         }	 | j)                  ||d       e y# t*        $ r}t-        d| d| d       |d}~ww xY w)z
        Use
            geo_mean((x,y), (alpha, 1-alpha)) >= |z|
        as a reformulation of
            PowCone3D(x, y, z, alpha).

        Check validity of the reformulation by solving
        orthogonal projection problems.
        r   r   SCSg|=)r   eps      r   r   
   r   g?N   r   r   z (when alpha=r   r   )r   r   r#   r   r    r!   r"   r'   rangeMinimizenormconstraints	PowCone3Dr*   r+   r,   copyr&   r-   absr.   r/   r0   )r2   proj_solve_argsmin_numeratordenominatorxyr;   	numeratoralpha_float	objectiveactual_constraintsactual_probactual_xweightsapprox_constraintsapprox_probapprox_xrC   s                     rD   test_3d_power_cone_approxz%TestGeoMean.test_3d_power_cone_approxJ   s    b**,,'1O).u=OKKN
		q""%eM;&JK 	LAy#k1KaDK'AaDQ_,EFMAaDBGGAE1$56I"$..":":1Q41qt<G=#J "K**Y0BCKK00ww||~HhhQ_=>G"$++aeW"=!"M!N**Y0BCKww||~H++Hhq+I#	$ " )!M+bIJs    G88	HHH)returnN)__name__
__module____qualname__rE   rc   r       rD   r   r      s    /b'rh   r   )__doc__numpyr   cvxpyr   cvxpy.tests.base_testr   r   r   rh   rD   <module>rm      s#       *Z( Zrh   