
    Ti                     x    d Z ddlZddlmZ ddlmZ eeej	        fZ
 ej        ej                  j        Zd ZdS )a   
This module provides the _project function that attempts to project the initial guess
onto the feasible set.

Adapted from the corresponding function in the PDFO package (https://www.pdfo.net) by
Tom M. Ragonneau (https://ragonneau.github.io) and Zaikun Zhang (https://www.zhangzk.net).
    N   LinearConstraint)OptimizeResultc           	         d}t          | t                    r| gn5t          | d          r| n"t          d                    |                    	 t          j        t
          j                  n0# t          $ r# t          d                    |                    w xY wt          j	                  dk    r"t          d                    |                    j
        }t          |t                    r|g}n5t          |d          r|}n"t          d                    |                    	 t          j        |t
          j                  }n0# t          $ r# t          d                    |                    w xY wt          |j	                  dk    s|j
        |k    r"t          d	                    |                    t          |t                    r|g}n5t          |d          r|}n"t          d
                    |                    	 t          j        |t
          j                  }n0# t          $ r# t          d                    |                    w xY wt          |j	                  dk    s|j
        |k    r"t          d                    |                    t          |t                    rJddht          |                                          k    r#t          |d         t                    s*|d         "t          d                    |                    d}|d         >t          j        t          j        |fd          |fd          }	t#          |	          S t%          t          j        t          j        |d         j        |d         j        z
            t.                              rt          j        |          | k    rt          j        |          |k    r|d         j        }
|d         j        |d         j        z   dz  }t
          j                            |
|t          j        |
          z
  d          \  }}}}t          j        t          j        |z   |fd          |fd          }	t#          |	          S |d         O	 ddlm} ddlm } ddlm} |d         }t                      t                      }}t          j        g g          t          j        g g          c|d<   |d<   |d         !                    d|j        j	        d                   |d<   |d         !                    d|j        j	        d                   |d<   t          j        g           t          j        g           c|d<   |d<   t          j        g           t          j        g           c|d<   |d<   tE          |j        j
                  D ]*}|j        |         |j        |         k    rt          j#        |d         |j        ||dz   ddf         fd          |d<   t
          j$        |d         |j        |         f         |d<   t
          j$        |d         |j        |         f         |d<   t          j#        |d         |j        ||dz   ddf         fd          |d<   t
          j$        |d         |j        |         f         |d<   t
          j$        |d         |j        |         f         |d<   ,|d         j
        dk    r3|d         j
        dk    r"|d         j
        dk    r |di | |di |g}nG|d         j
        dk    r|d         j
        dk    r	 |di |}n|d         j
        dk    r	 |di |}nd}t          j        |d                   }t          j        |d                   }t          j%        ||d                   &                                st          j%        |d         |          &                                s{t          j'        ||d                   &                                sNt          j%        |          &                                s't          j%        |          &                                r  |fdfd |||          |           S t#                    S # tP          $ r t#                    cY S w xY wt#                    S )!a  Projection of the initial guess onto the feasible set.

    Parameters
    ----------
    x0: ndarray, shape (n,)
        The same as in prepdfo.
    lb: ndarray, shape (n,)
        The same as in prepdfo.
    ub: ndarray, shape (n,)
        The same as in prepdfo.
    constraints: dict
        The general constraints of the problem, defined as a dictionary with
        fields:
            linear: LinearConstraint
                The linear constraints of the problem.
            nonlinear: dict
                The nonlinear constraints of the problem. When ``_project`` is called, the nonlinear constraints are
                None.

    Returns
    -------
    result: OptimizeResult
        The result of the projection.

    Authors
    -------
    Tom M. RAGONNEAU (ragonneau.github.io)
    and Zaikun ZHANG (www.zhangzk.net)

    Dedicated to the late Professor M. J. D. Powell FRS (1936--2015).
    prima__len__z,{}: UNEXPECTED ERROR: x0 should be a vector.)dtypez5{}: UNEXPECTED ERROR: x0 should contain only scalars.r   z,{}: UNEXPECTED ERROR: lb should be a vector.z5{}: UNEXPECTED ERROR: lb should contain only scalars.z={}: UNEXPECTED ERROR: the size of lb is inconsistent with x0.z,{}: UNEXPECTED ERROR: ub should be a vector.z5{}: UNEXPECTED ERROR: ub should contain only scalars.z={}: UNEXPECTED ERROR: the size of ub is inconsistent with x0.linear	nonlinearNz6{}: UNEXPECTED ERROR: The constraints are ill-defined.g@xDr   )axis)x   )rcond)minimize)Boundsr   Albub c                 @    t          j        | z
  | z
            dz  S )Nr   )npdotr   x0_cs    z/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/_lib/pyprima/common/_project.py<lambda>z_project.<locals>.<lambda>   s     "&T1t8*D*Dq*H     c                     | z
  S )Nr   r   s    r   r   z_project.<locals>.<lambda>   s    _`cg_g r   )jacboundsconstraints))
isinstancescalar_typeshasattr
ValueErrorformatr   asarrayfloat64lenshapesizedictsetkeysr   nanminnanmaxr   all
less_equalabsr   r   epsmaxminr   linalglstsqr   scipy.optimizer   r   reshaperangeconcatenater_greaterany	not_equalImportError)x0r   r   r"   invokerlenx0lb_cub_cmax_conx_projabxi_r   ScipyBoundsScipyLinearConstraintr   pc_args_ineq
pc_args_eqiproject_constraintsax_ineqax_eqr   s                           @r   _projectrV      sf	   @ G "l## Yt	Y		 YGNNwWWXXXbz$bj111 b b bPWWX_``aaab
4:!GNNwWWXXXIE "l## Yt	Y		 YGNNwWWXXXbz$bj111 b b bPWWX_``aaab
4:!tyE11X__`ghhiii "l## Yt	Y		 YGNNwWWXXXbz$bj111 b b bPWWX_``aaab
4:!tyE11X__`ghhiii k4(( c(K1HCP[P`P`PbPbLcLc1c1cK13CDD 2dHST\H]HeQXXY`aabbbG 8$BItTl;;;TBKKK''''	R]26+h"7":[=R=U"UVVX[\\	]	] (F4LLWH$$)@)@ !#"%H(=(@@AEiooaRVAt__)<DoIIAq! BItby$&7a@@@$GaPPP''''8(1	*//////<<<<<<PPPPPP *F (,vvtvv*L13RD1A1A2:rdCSCS.Lz# ,S 1 9 9!VX^A=N O OL(o55a9JKKJsO35:b>>2:b>>0L
4 035:b>>2:b>>0L
4 069>** M M9Q<69Q<//(*S8I68TUVWXYVYTY[\[\[\T\K]7^ef(g(g(gL%)+|D/A69Q</O)PL&)+|D/A69Q</O)PL&&&(njovxPQRSTURUPUWXWXWXPXGY5Zab&c&c&cJsO')uZ-=vy|-K'LJt$')uZ-=vy|-K'LJt$$C %))l4.@.E.I.IjY]N^NcfgNgNg'<'<'L'L|'L'LNcNcNqNqfpNqNq&r##c"'!++T0B0G!0K0K&;&;&K&Kl&K&K##C%))&;&;&I&Ij&I&I##&(# f\#.55GF:c?D11Ez'<#566::<< .
<X\K]_f@g@g@k@k@m@m .L
4(899==??.JtT**..00.46JtT4J4J4N4N4P4P.  x H H H H$ThThThTh'2{4'>'>L_a a a a &---- 	* 	* 	*!D))))))	* D!!!!s=    A4 4-B!4 E -F H: :-I'.Pc c c98c9)__doc__numpyr   _linear_constraintsr   r:   r   intfloatgenericr$   finfor)   r5   rV   r   r   r   <module>r^      s         1 1 1 1 1 1 ) ) ) ) ) ) UBJ'bhrz\" \" \" \" \"r   