
    bi\                     B    d Z ddlZddlZddlZddlmZ  G d de      Zy)a6  
Copyright 2013 Steven Diamond, Eric Chu

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                   X    e Zd ZdZddZddZddZddZddZddZ	dd	Z
dd
ZddZy)TestNonlinearAtomsz, Unit tests for the nonlinear atoms module. Nc                    t        j                  dd      | _        t        j                  dd      | _        t        j                  dd      | _        t        j                  dd      | _        t        j                  dd	      | _        y )
N   x)namey)r   r   AB)   r   C)cpVariabler   r
   r   r   r   selfs    [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_nonlinear_atoms.pysetUpzTestNonlinearAtoms.setUp   s\    QS)QS)V#.V#.V#.    c                    t        j                  t        j                  t        j                  | j                                    }| j                  dt
        j                  gk  g}t        j                  ||      }|j                  t         j                        }| j                  |d       | j                  | j                  j                  dt
        j                  g       t        j                  t        j                  | j                              }t        j                  | j                        dk\  | j                  ddgk  g}t        j                  ||      }|j                  t         j                        }| j                  |d       | j                  | j                  j                  ddg       t        j                  t        j                  | j                        d         }| j                  dt
        j                  gk  g}t        j                  ||      }|j                  t         j                        }| j                  |d       t        j                  t        j                  | j                  d               }| j                  dt
        j                  gk  g}t        j                  ||      }|j                  t         j                        }| j                  |d       y )N   solverr   r   )r   Maximizesumlogr   matheProblemsolveCLARABELassertAlmostEqualassertItemsAlmostEqualvalueMinimize)r   objconstrpresults        r   test_log_problemz#TestNonlinearAtoms.test_log_problem$   s   kk"&&01&&QK'(JJsF#,vq)##DFFLL1dff+> kk"&&.)&&.A%tvv!Q'78JJsF#,vq)##DFFLL1a&9 kk"&&.+,&&QK'(JJsF#,vq) kk"&&+,&&QK'(JJsF#,vq)r   c                     | j                  t        j                  d      j                  d       t	        j
                  t        j                  d      j                        sJ y)zTest the entr atom.
        r   N)assertEqualr   entrr$   npisneginfr   s    r   	test_entrzTestNonlinearAtoms.test_entrC   sB     	))1-{{2772;,,---r   c                    d}d}t         j                  j                  |      }|j                  dd|      }|t	        |      z  }t        j                  |d      }t        j                  |      }t        j                  t        j                  ||            }t        j                  |      dk(  g}t        j                  t        j                  |      |      }	||_        |	j                  t
        j                         | j                  |j                  |d	
       |	j                  t
        j                         | j                  |j                  |d	
       y)z$Test a problem with kl_div.
        2   
                 ?Tnonnegr   r   r   placesN)r/   randomRandomStateuniformr   r   	Parameterr   kl_divr   r%   r$   r    SCSr#   r!   )
r   kKkSeedprng	npSPriors	p_refProbv_probobjklconstrsklprobs
             r   test_kl_divzTestNonlinearAtoms.test_kl_divI   s    yy$$U+LLc2.	I.	 LLD1	Rryy3466&>Q&'BKK.8#	BFF###FLL)A#FBKK(##FLL)A#Fr   c                    d}d}t         j                  j                  |      }|j                  dd|      }|t	        |      z  }t        j                  |d      }t        j                  |      }t        j                  t        j                  ||            }t        j                  |      dk(  g}t        j                  t        j                  |      |      }	||_        |	j                  t
        j                         | j                  |j                  |d	
       |	j                  t
        j                         | j                  |j                  |d	
       y)z&Test a problem with rel_entr.
        r3   r4   r5   r6   Tr7   r   r   r   r9   N)r/   r;   r<   r=   r   r   r>   r   rel_entrr   r%   r$   r    r@   r#   r!   )
r   rA   rB   rC   rD   rE   rF   obj_rel_entrrH   rel_entr_probs
             r   test_rel_entrz TestNonlinearAtoms.test_rel_entrb   s    yy$$U+LLc2.	I.	 LLD1	Rvvbkk&)<=66&>Q&'

2;;|#<gF#	266*##FLL)A#F2;;/##FLL)A#Fr   c                 "   t        j                         }t        j                         }t        j                  t        j                  t        j                  ||            ||z   dk  g      }|j                  t         j                         | j                  |j                  |j                  d       | j                  |j                  d       t        j                  t        j                  t        j                  ||            ||z   dk  g      }|j                  t         j                         	 | j                  |j                  dd       | j                  |j                  d	d       | j                  |j                  d
       y)zBA test showing the difference between kl_div and rel_entr
        r   )constraintsr   r   r9   r   g2@?   g%J?gAGZѿN)
r   r   r   r%   r?   r    r!   r#   r$   rL   )r   r   r
   kl_div_probrN   s        r   test_difference_kl_div_rel_entrz2TestNonlinearAtoms.test_difference_kl_div_rel_entr{   s(    KKMKKMjjRYYq!_!=APQEUVJ<X-##AGGQWWQ#?##K$5$5q9

2;;r{{1a/@#APQTUPUYZPZ|\2;;/	 	##AGGYq#A##AGGYq#A##M$7$7Cr   c                 :   dD ]  }t        |       t        j                  |      }t        j                  t        j                  t        j
                  |                  }t        j                  |t        j                  |      dk(  g      }|j                  t        j                         | j                  |j                  |d|z  gz  d       |j                  t        j                         | j                  |j                  |d|z  gz  d        y)z"Test a problem with entr.
           r4      r   r   r6   r   r9   N)printr   r   r   r   r.   r   r    r!   r#   r$   r@   r   nr   r&   r(   s        r   test_entr_probz!TestNonlinearAtoms.test_entr_prob   s      	EA!HAA++bffRWWQZ01C

3a 01AGG2;;G'''BqD6!'DGG266G"''BqD6!'D	Er   c                 :   dD ]  }t        |       t        j                  |      }t        j                  t        j                  t        j
                  |                  }t        j                  |t        j                  |      dk(  g      }|j                  t        j                         | j                  |j                  |d|z  gz  d       |j                  t        j                         | j                  |j                  |d|z  gz  d        y)	z!Test a problem with exp.
        rV   r   r   r6   r   r9   rR   N)rY   r   r   r%   r   expr   r    r@   r#   r$   r!   rZ   s        r   test_expzTestNonlinearAtoms.test_exp   s      	EA!HAA++bffRVVAY/0C

3a 01AGG266G"''BqD6!'DGG2;;G'''BqD6!'D	Er   c                 6   dD ]  }t        |       t        j                  |      }t        j                  t        j                  t        j
                  |                  }t        j                  |t        j                  |      dk(  g      }|j                  t        j                         | j                  |j                  |d|z  gz         |j                  t        j                         | j                  |j                  |d|z  gz  d        y)z!Test a problem with log.
        rV   r   r   r6   r   r9   N)rY   r   r   r   r   r   r   r    r!   r#   r$   r@   rZ   s        r   test_logzTestNonlinearAtoms.test_log   s      	EA!HAA++bffRVVAY/0C

3a 01AGG2;;G'''BqD6:GG266G"''BqD6!'D	Er   )returnN)__name__
__module____qualname____doc__r   r*   r1   rJ   rO   rT   r\   r_   ra    r   r   r   r      s8    6/*>.G2G2D,EEEr   r   )	rf   r   numpyr/   cvxpyr   cvxpy.tests.base_testr   r   rg   r   r   <module>rk      s(        *]E ]Er   