L is.ddlmZddlZddlmZddlmZm Z ddl m Z ddl m Z ddlmZmZddlmZdd lmZd ZGd d ZGd dZGddZGddZGddZGddZGddZGddZGddeZy)) namedtupleN)approx_derivative group_columns)HessianUpdateStrategy)LinearOperator)array_namespacexp_copy)array_api_extra)_ScalarFunctionWrapper)z2-pointz3-pointcsc&eZdZdZ ddZddZy)_ScalarGradWrapperz0 Wrapper class for gradient calculation Nc`||_||_|gn||_||_d|_d|_yNr)fungradargsfinite_diff_optionsngevnfev)selfrrrrs n/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_differentiable_functions.py__init__z_ScalarGradWrapper.__init__s5 ,BD #6   c t|jrDtj|jtj|g|j }nP|jt vr>t|j|fd|i|j\}}|xj|dz c_ |xjdz c_ S)Nf0rr) callablernp atleast_1dcopyr FD_METHODSrrrrr)rxrkwdsgdcts r__call__z_ScalarGradWrapper.__call__#s DII  idii ?TYY?@A YY* $&** FAs IIV $I Q rNNNN__name__ __module__ __qualname____doc__rr'rrrrs  $ rrcBeZdZdZ d dZd dZd dZdZdZdZ y) _ScalarHessWrapperzC Wrapper class for hess calculation via finite differences Nc||_||_|gn||_||_d|_d|_d|_d|_t|r|tj|g||_|xj dz c_tj|j r6|j|_tj|j |_yt|j t r|j"|_y|j$|_tj&tj(|j |_y|t*vr|j,|_yy)Nrr)hessrrrrnhevH _hess_funcrrr!spsissparse_sparse_callable csr_array isinstancer_linearoperator_callable_dense_callable atleast_2dasarrayr"_fd_hess)rr3x0rrrs rrz_ScalarHessWrapper.__init__9s  ,BD #6    D>"''"+--DF IINI||DFF#"&"7"7tvv.DFFN3"&"?"?#'"6"6rzz$&&'9: Z "&-- rc N|jtj||S)Nr)r6rr!)rr#rr$s rr'z_ScalarHessWrapper.__call__[srwwqzb11rc t|j|fd|i|j\|_}|xj|dz c_|jS)Nrr)rrrr5r)rr#rr$r&s rr@z_ScalarHessWrapper._fd_hess^sN' IIq  #'#;#;  S[ vv rc |xjdz c_tj|j|g|j|_|j SNr)r4r7r:r3rr5rr#r$s rr9z#_ScalarHessWrapper._sparse_callablees< Q ytyy7TYY78vv rc |xjdz c_tjtj|j|g|j |_|j SrF)r4rr>r?r3rr5rGs rr=z"_ScalarHessWrapper._dense_callablejsJ Q  JJytyy/TYY/ 0 vv rc |xjdz c_|j|g|j|_|jSrF)r4r3rr5rGs rr<z+_ScalarHessWrapper._linearoperator_callableqs3 Q 1)tyy)vv r)NNNNr)) r+r,r-r.rr'r@r9r=r<r/rrr1r15s4  $ 0D2 rr1ceZdZdZdej ej fddfdZedZedZ edZ dZ dZ d Z d Zd Zd Zd ZdZy)ScalarFunctionaScalar function and its derivatives. This class defines a scalar function F: R^n->R and methods for computing or approximating its first and second derivatives. Parameters ---------- fun : callable evaluates the scalar function. Must be of the form ``fun(x, *args)``, where ``x`` is the argument in the form of a 1-D array and ``args`` is a tuple of any additional fixed parameters needed to completely specify the function. Should return a scalar. x0 : array-like Provides an initial set of variables for evaluating fun. Array of real elements of size (n,), where 'n' is the number of independent variables. args : tuple, optional Any additional fixed parameters needed to completely specify the scalar function. grad : {callable, '2-point', '3-point', 'cs'} Method for computing the gradient vector. If it is a callable, it should be a function that returns the gradient vector: ``grad(x, *args) -> array_like, shape (n,)`` where ``x`` is an array with shape (n,) and ``args`` is a tuple with the fixed parameters. Alternatively, the keywords {'2-point', '3-point', 'cs'} can be used to select a finite difference scheme for numerical estimation of the gradient with a relative step size. These finite difference schemes obey any specified `bounds`. hess : {callable, '2-point', '3-point', 'cs', HessianUpdateStrategy} Method for computing the Hessian matrix. If it is callable, it should return the Hessian matrix: ``hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)`` where x is a (n,) ndarray and `args` is a tuple with the fixed parameters. Alternatively, the keywords {'2-point', '3-point', 'cs'} select a finite difference scheme for numerical estimation. Or, objects implementing `HessianUpdateStrategy` interface can be used to approximate the Hessian. Whenever the gradient is estimated via finite-differences, the Hessian cannot be estimated with options {'2-point', '3-point', 'cs'} and needs to be estimated using one of the quasi-Newton strategies. finite_diff_rel_step : None or array_like Relative step size to use. The absolute step size is computed as ``h = finite_diff_rel_step * sign(x0) * max(1, abs(x0))``, possibly adjusted to fit into the bounds. For ``method='3-point'`` the sign of `h` is ignored. If None then finite_diff_rel_step is selected automatically, finite_diff_bounds : tuple of array_like Lower and upper bounds on independent variables. Defaults to no bounds, (-np.inf, np.inf). Each bound must match the size of `x0` or be a scalar, in the latter case the bound will be the same for all variables. Use it to limit the range of function evaluation. epsilon : None or array_like, optional Absolute step size to use, possibly adjusted to fit into the bounds. For ``method='3-point'`` the sign of `epsilon` is ignored. By default relative steps are used, only if ``epsilon is not None`` are absolute steps used. workers : map-like callable, optional A map-like callable, such as `multiprocessing.Pool.map` for evaluating any numerical differentiation in parallel. This evaluation is carried out as ``workers(fun, iterable)``, or ``workers(grad, iterable)``, depending on what is being numerically differentiated. Alternatively, if `workers` is an int the task is subdivided into `workers` sections and the function evaluated in parallel (uses `multiprocessing.Pool `). Supply -1 to use all available CPU cores. It is recommended that a map-like be used instead of int, as repeated calls to `approx_derivative` will incur large overhead from setting up new processes. .. versionadded:: 1.16.0 Notes ----- This class implements a memoization logic. There are methods `fun`, `grad`, hess` and corresponding attributes `f`, `g` and `H`. The following things should be considered: 1. Use only public methods `fun`, `grad` and `hess`. 2. After one of the methods is called, the corresponding attribute will be set. However, a subsequent call with a different argument of *any* of the methods may overwrite the attribute. Nc nt|s|tvrtdtdt|s+|tvs#t|tstdtd|tvr|tvr tdt |x|_} tj| j|d| } | j} | j| jdr | j} t|||_||_||_||_||_| j'| | |_| |_|j(j,|_d|_d|_d|_d|_t8j:|_| xst>} i} |tvr|| d <|| d <|| d <|| d <| | d <d| d<|tvr|| d <|| d <|| d <d| d<| | d <d| d<d|_ |jCtE||j|| |_#|jIt|tr`||_%|jJjM|j.dd|_d|_'d|_(tSdddg}|dd|_*yt|r7tW|||| |_*|jTjJ|_%d|_y|tvrctW||||jF| |_*|jI|jU|j(|jX|_%d|_yy)Nz)`grad` must be either callable or one of .z@`hess` must be either callable, HessianUpdateStrategy or one of zWhenever the gradient is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.rndimxp real floatingFmethodrel_stepabs_stepboundsworkersT full_outputas_linear_operatorr)rrrr3 _FakeCounterrr4)rr4)rArr)rArrrrC)-rr" ValueErrorr;rr rPxpx atleast_ndr?float64isdtypedtyper _wrapped_fun _orig_fun _orig_grad _orig_hess_argsastyper#x_dtypesizen f_updated g_updated H_updated _lowest_xrinf _lowest_fmap_nfev _update_funr _wrapped_grad _update_gradr5 initializex_prevg_prevr _wrapped_hessr1r%)rrrArrr3finite_diff_rel_stepfinite_diff_boundsepsilonrVrP_x_dtyperrYs rrzScalarFunction.__init__s@~$j"8;J  )-4  *15  . : ,0  ).B  +.5  +8<  4 5-4  *15  .  0 !! 3    d1 2DF FF  dfff -!DNDKDK%nvv6FGL!-11!=D ~%7(; &" ++--!%#%7++(; &"!!#++DFFtvv+>!%$rcH|j|jjzSr))rprrrrs rrzScalarFunction.nfev<szzD..3333rc.|jjSr))rrrr~s rrzScalarFunction.ngev@!!&&&rc.|jjSr))rwr4r~s rr4zScalarFunction.nhevDrrct|jtr|j|j|_|j |_tj|jj|d|j}|jj||j|_d|_d|_d|_|j#ytj|jj|d|j}|jj||j|_d|_d|_d|_yNrrNF)r;rcrrsr#rur%rvr[r\rPr?rerfrirjrk _update_hessrr#r{s r _update_xzScalarFunction._update_xHs doo'< =    &&DK&&DK 2twwGBWW^^B 5DF"DN"DN"DN     2twwGBWW^^B 5DF"DN"DN"DNrc|jsf|j|j}|xjdz c_||jkr|j|_||_||_d|_yyNrT)rir`r#rprnrlf)rfxs rrqzScalarFunction._update_fun_s[~~""466*B JJ!OJDNN"!%!#DF!DNrc|jsV|jtvr|j|j |j |j |_d|_yyNrCT)rjrbr"rqrrr#rr%r~s rrszScalarFunction._update_gradjsL~~*,  "''466':DF!DN rc|js|jtvr=|j|j |j |j |_nt|jtr[|j|jj|j |jz |j |jz n |j |j |_d|_yyr) rkrcr"rsrwr#r%r5r;rupdaterurvr~s rrzScalarFunction._update_hessqs~~*,!!#++DFFtvv+>DOO-BC!!# dfft{{2DFFT[[4HI++DFF3!DNrctj||js|j||j |j Sr))r array_equalr#rrqrrr#s rrzScalarFunction.fun~s5~~a( NN1  vv rctj||js|j||j |j Sr))rrr#rrsr%rs rrzScalarFunction.grad5~~a( NN1  vv rctj||js|j||j |j Sr))rrr#rrr5rs rr3zScalarFunction.hessrrctj||js|j||j |j |j |jfSr))rrr#rrqrsrr%rs r fun_and_gradzScalarFunction.fun_and_gradsJ~~a( NN1   vvtvv~r)r+r,r-r.rrmrpropertyrrr4rrqrsrrrr3rr/rrrKrKwsXrHL&(ffWbff$5tTi&V44''''#. "" "   rrKceZdZdZdZy)_VectorFunWrapperc ||_d|_yr)rr)rrs rrz_VectorFunWrapper.__init__s rct|xjdz c_tj|j|SrF)rrr rrs rr'z_VectorFunWrapper.__call__s& Q }}TXXa[))rN)r+r,r-rr'r/rrrrs *rrc&eZdZdZ ddZddZy)_VectorJacWrapper0 Wrapper class for Jacobian calculation NcX||_||_||_||_d|_d|_yr)rjacrsparse_jacobiannjevr)rrrrrs rrz_VectorJacWrapper.__init__s0#6 .  rc t|jr'|j|}|xjdz c_nP|jtvr>t |j |fd|i|j \}}|xj|dz c_|jrtjStjr|jSt|tr|Stj |S)Nrrr)rrrr"rrrrrr7r:r8toarrayr;rrr>)rr#rr$Jr&s rr'z_VectorJacWrapper.__call__s DHH  A IINI XX #&** FAs IIV $I   ==# # \\!_99;  > *H==# #rr(r)r*r/rrrrs  $  $rrc8eZdZdZ ddZd dZd dZdZdZy) _VectorHessWrapperrNcJ||_||_||_d|_d|_yr)rr3rr4r)rr3rrs rrz_VectorHessWrapper.__init__s(  #6   rc t|jr'|xjdz c_|j||S|jtvr|j |||Sy)NrJ0)rr3r4_callable_hessr"r@)rr#vrr$s rr'z_VectorHessWrapper.__call__sV DII  IINI&&q!, , YY* $==A"=- -%rc|&|j|}|xjdz c_t|j|f|jj ||fd|j }|S)Nr)rr)rrr jac_dot_vTdotr)rr#rrr5s rr@z_VectorHessWrapper._fd_hessse :!B IINI dnna :!#!$%4 :!% 8 8 :rc|xjdz c_|j|jj|SrF)rrrrrr#rs rrz_VectorHessWrapper.jac_dot_vs, Q xx{}}  ##rc|j||}tj|rtj|St |t r|St jt j|Sr)) r3r7r8r:r;rrr>r?)rr#rr5s rrz!_VectorHessWrapper._callable_hesssQ IIaO <<?==# # > *H==A/ /r)NNr)) r+r,r-r.rr'r@rrr/rrrrs(  $ . $0rrceZdZdZddej ej fddfdZedZedZ edZ dZ dZ d Z d Zd Zd Zd ZdZy)VectorFunctionaVector function and its derivatives. This class defines a vector function F: R^n->R^m and methods for computing or approximating its first and second derivatives. Notes ----- This class implements a memoization logic. There are methods `fun`, `jac`, hess` and corresponding attributes `f`, `J` and `H`. The following things should be considered: 1. Use only public methods `fun`, `jac` and `hess`. 2. After one of the methods is called, the corresponding attribute will be set. However, a subsequent call with a different argument of *any* of the methods may overwrite the attribute. Nc  t|s|tvrtdtdt|s+|tvs#t|tstdtd|tvr|tvr tdt |x|_} tj| j|d| } | j} | j| jdr | j} ||_ ||_||_| j!| | |_| |_|j"j&|_d|_d|_d|_d |_d |_d |_| xst6} i} |tvrQ|| d <|| d <|t9|}||f| d <|| d <| | d<d| d<t;j<|j"|_|tvr3|| d <|| d <d| d<t;j<|j"|_|tvr|tvr tdtA||_!|jEt;jF|jH|_%|jJj&|_&t|r=|tO|j"|_(d|_|xj,dz c_n\|tvrTtS|jB|j"fd|jHi| \|_(}d|_|xj*|dz c_d |_*|s!|KtWjX|jPr,tWjZ|jP|_(d|_*n~tWjX|jPr |jPj]|_(n?t|jPt^rn$t;j`|jP|_(tc||jB| |jT|_2tg||jd| |_4t|s|tvri|jitO|j"|jJ|jP|_5d|_t|r|xj.dz c_yyt|trC||_5|jjjm|j(dd|_d|_7d|_8yy)Nz(`jac` must be either callable or one of rMz?`hess` must be either callable,HessianUpdateStrategy or one of zWhenever the Jacobian is estimated via finite-differences, we require the Hessian to be estimated using one of the quasi-Newton strategies.rrNrQrFrRrSsparsityrUrVTrWrXrr)rrr)rrrr3)9rr"rZr;rr rPr[r\r?r]r^r_ra _orig_jacrcrer#rfrgrhrp_njev_nhevri J_updatedrkrorrr!x_diffr fun_wrappedrq zeros_likerrmr rrrr7r8r:rrr>r jac_wrappedr hess_wrappedr5rtruJ_prev)rrrArr3rxfinite_diff_jac_sparsityryrrVrPr{r|rsparsity_groupsr&s rrzVectorFunction.__init__sS}J!6G |STUV V$*"4d$9:@@J|1NO O * !3+, , 'r**" ^^BJJrNr : ::bhh 0XXF2v&    .S  * ,/  ).B  +'3"/0H"I3K3B3D#J/,>  )-4  *15  .''$&&/DK : ,0  ).B  +8<  4 5 ''$&&/DK * !3+, , -S1 tvv& C=)DF!DN JJ!OJ J +  $&&-1VV7JKDFC"DN JJ#f+ %J$ 'CLL,@]]466*DF#'D \\$&& !VV^^%DF  / ]]466*DF,   3 00  / d&&TZ/&&wtvv466&JDF!DN~ a  3 4DF FF  dfff -!DNDKDK 5rcH|j|jjzSr))rprrr~s rrzVectorFunction.nfevszzD,,1111rcH|j|jjzSr))rrrr~s rrzVectorFunction.njevszzD--2222rc|jSr))rr~s rr4zVectorFunction.nhevs zzrcbtj||js||_d|_yy)NF)rrrrk)rrs r _update_vzVectorFunction._update_vs'~~a(DF"DN)rctj||jsTt|jt r|j |j|_|j|_ tj|jj|d|j}|jj||j|_d|_d|_d|_|j'ytj|jj|d|j}|jj||j|_d|_d|_d|_yyr)rrr#r;rcr _update_jacrurrr[r\rPr?rerfrirrkrrs rrzVectorFunction._update_xs~~a($//+@A  ""ff "ff ^^DGGOOA$6Q477KDLL9!&!&!&!!#^^DGGOOA$6Q477KDLL9!&!&!&!)rc|jsF|jt|j|_|xj dz c_d|_yyr)rirr r#rrpr~s rrqzVectorFunction._update_funs<~~%%gdffo6DF JJ!OJ!DNrc|jsu|jtvr|jn|xjdz c_|j t |j|j|_ d|_yy)NrrCT) rrr"rqrrr r#rrr~s rrzVectorFunction._update_jacs]~~~~+  " a %%gdffo$&&%ADF!DNrc`|jst|jrK|jt |j |j |_|xjdz c_n9|jtvrQ|j|jt |j |j |j|_nt|jtr|j|j|j|j |jz }|jj j#|j |jj j#|j z }|jj%||d|_yy)NrrT)rkrrcrr r#rr5rr"rrr;rrurrrr)rdelta_xdelta_gs rrzVectorFunction._update_hesss~~(**7466?DFFC a J.  "**7466?DFFtvv*NDOO-BC  ";;*t{{/F"fft{{2G"ffhhll4662T[[]]5F5Ftvv5NNGFFMM'73!DN!rcn|j||jt|jSr))rrqr rrs rrzVectorFunction.funs* q tvvrc|j||jt|jdr/|jj |jj S|jSNre)rrhasattrrrer_rs rrzVectorFunction.jacsN q  4668 $66==. .vv rc|j||j||jt|jdr/|jj |jj S|jSr)rrrrr5rer_rs rr3zVectorFunction.hesssZ q q  4668 $66==. .vv r)r+r,r-r.rrmrrrrr4rrrqrrrrr3r/rrrrs"'+T&(ffWbff$5t}~2233# '&" ""& rrc.eZdZdZdZdZdZdZdZy)LinearVectorFunctionzLinear vector function and its derivatives. Defines a linear function F = A x, where x is N-D vector and A is m-by-n matrix. The Jacobian is constant and equals to A. The Hessian is identically zero and it is returned as a csr matrix. c|s|7tj|r"tj||_d|_nftj|r|j |_d|_n4t jt j||_d|_|jj\|_ |_ t|x|_ }tj|j|d|}|j }|j#|j$dr |j$}|j'|||_||_|jj-|j(|_d|_t j2|jt4|_tj|j|jf|_y)NTFrrNrQ)r_)r7r8r:rrrrr>r?shaperrhr rPr[r\r]r^r_rer#rfrrrizerosfloatrr5)rArArrPr{r|s rrzLinearVectorFunction.__init__s? o5#,,q/]]1%DF#'D \\!_YY[DF#(D ]]2::a=1DF#(D &r**" ^^BJJrNr : ::bhh 0XXF2v& DFF#$&&./0rc tj||jsntj|j j |d|j }|j j||j|_d|_ yyr) rrr#r[r\rPr?rerfrirs rrzLinearVectorFunction._update_xs]~~a( 2twwGBWW^^B 5DF"DN)rc|j||js'|jj||_d|_|jS)NT)rrirrrrs rrzLinearVectorFunction.fun#s8 q~~VVZZ]DF!DNvv rc<|j||jSr))rrrs rrzLinearVectorFunction.jac*s qvv rcJ|j|||_|jSr))rrr5rs rr3zLinearVectorFunction.hess.s qvv rN) r+r,r-r.rrrrr3r/rrrrs  1<# rrc"eZdZdZfdZxZS)IdentityVectorFunctionzIdentity vector function and its derivatives. The Jacobian is the identity matrix, returned as a dense array when `sparse_jacobian=False` and as a csr matrix otherwise. The Hessian is identically zero and it is returned as a csr matrix. ct|}|s|tj|d}d}ntj|}d}t ||||y)Ncsr)formatTF)lenr7 eye_arrayreyesuperr)rrArrhr __class__s rrzIdentityVectorFunction.__init__;sL G o5 a.A"Oq A#O B0r)r+r,r-r.r __classcell__)rs@rrr4s 11rr) collectionsrnumpyr scipy.sparsesparser7_numdiffrr_hessian_update_strategyrscipy.sparse.linalgrscipy._lib._array_apir r scipy._libr r[scipy._lib._utilr r"rr1rKrrrrrrr/rrrs"6;.:-3* ""J??D^^B ***$*$Z2020jqqh99x111r