
    Џkh                         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y)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n)t        | d      r| nt        dj	                  |            	 t        j                  t
        j                        t        j                        dk7  rt        dj	                  |            j                  }t        |t              r|g}n)t        |d      r|}nt        dj	                  |            	 t        j                  |t
        j                        }t        |j                        dk7  s|j                  |k7  rt        d	j	                  |            t        |t              r|g}n)t        |d      r|}nt        d
j	                  |            	 t        j                  |t
        j                        }t        |j                        dk7  s|j                  |k7  rt        dj	                  |            t        |t              r6ddht        |j                               k  rt        |d   t              s|d   t        dj	                  |            d}|d   <t        j                  t        j                   |fd      |fd      }	t#        |	      S t%        t        j&                  t        j(                  |d   j*                  |d   j,                  z
        t.                    rt        j0                  |      | k  rt        j2                  |      |k\  r|d   j4                  }
|d   j,                  |d   j*                  z   dz  }t
        j6                  j9                  |
|t        j:                  |
      z
  d      \  }}}}t        j                  t        j                   |z   |fd      |fd      }	t#        |	      S |d   	 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   jC                  d|j4                  j                  d         |d<   |d   jC                  d|j4                  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*                  |   k7  rt        jF                  |d   |j4                  ||dz   ddf   fd      |d<   t
        jH                  |d   |j,                  |   f   |d<   t
        jH                  |d   |j*                  |   f   |d<   t        jF                  |d   |j4                  ||dz   ddf   fd      |d<   t
        jH                  |d   |j,                  |   f   |d<   t
        jH                  |d   |j*                  |   f   |d<   0 |d   j                  dkD  r5|d   j                  dkD  r#|d   j                  dkD  r |di | |di |g}nJ|d   j                  dkD  r|d   j                  dkD  r	 |di |}n|d   j                  dkD  r	 |di |}nd}t        j:                  |d         }t        j:                  |d         }t        jJ                  ||d         jM                         st        jJ                  |d   |      jM                         sot        jN                  ||d         jM                         sHt        jJ                  |      jM                         s$t        jJ                  |      jM                         r |fdfd |||      |       S t#              S t#              S # t        $ r t        dj	                  |            w xY w# t        $ r t        dj	                  |            w xY w# t        $ r t        dj	                  |            w xY w# tP        $ r t#              cY S w xY w)!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                 B    t        j                  | z
  | z
        dz  S )Nr   )npdotr   x0_cs    ^/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/_lib/pyprima/common/_project.py<lambda>z_project.<locals>.<lambda>   s    "&&T1t8*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      s   @ G "l#t	Y	GNNwWXXbzz$bjj1 4::!GNNwWXXIIE "l#t	Y	GNNwWXXbzz$bjj1 4::!tyyE1X__`ghii "l#t	Y	GNNwWXXbzz$bjj1 4::!tyyE1X__`ghii k4((K1HCP[P`P`PbLc1cK13CDT\H]HeQXXY`abbG 8$BIItTl;TBK''	R]]266+h"7":":[=R=U=U"UVX[\	]FF4LWH$)@ !##"%%H(=(@(@@AEiiooaRVVAt_)<DoIAq! BIItby$&7a@$GaP''8(1	*/<P *F (,vtv*L13RD1A2::rdCS.Lz# ,S 1 9 9!VXX^^A=N OL(o55a9JKJsO35::b>2::b>0L
4 035::b>2::b>0L
4 0699>>* M99Q<699Q</(*S8I688TUVWXYVYTY[\T\K]7^ef(gL%)+|D/A699Q</O)PL&)+|D/A699Q</O)PL&&(nnjovxxPQRSTURUPUWXPXGY5Zab&cJsO')uuZ-=vyy|-K'LJt$')uuZ-=vyy|-K'LJt$M C %%)l4.@.E.E.IjY]N^NcNcfgNg'<'L|'LNcNqfpNq&r#c"''!+T0B0G0G!0K&;&Kl&K#C%%)&;&Ij&I#&(# ff\#.5GFF:c?D1Ezz'<#56::<

<X\K]_f@g@k@k@mLL
4(89==?JJtT*..0BJJtT4J4N4N4P H$Th'24'>L_a a &--
 D!!c  bPWWX_`aab  bPWWX_`aab  bPWWX_`aabb  	*!D))	*s=   %_) #%` %`7 	Pa a )$`$`47$aa65a6)__doc__numpyr   _linear_constraintsr   r:   r   intfloatgenericr$   finfor)   r5   rV   r   r   r   <module>r^      sG     1 ) UBJJ'bhhrzz\"r   