L iEIpdZddlZddlmZddlmZddlZddlm Z dZ dZ dZ d Z dd Zd Zd Zdd Zy)zn Routines for removing redundant (linearly dependent) equations from linear programming equality constraints. N)svd) interp_decompdtrsmcd}tjt||kDjdj S)a Counts the number of nonzeros in each row of input array A. Nonzeros are defined as any element with absolute value greater than tol = 1e-13. This value should probably be an input to the function. Parameters ---------- A : 2-D array An array representing a matrix Returns ------- rowcount : 1-D array Number of nonzeros in each row of A gvIh%<=axis)nparrayabssumflatten)Atols g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_remove_redundancy.py _row_countrs7" C 88SVc\&&A&. / 7 7 99cHt|}tj||zS)a Returns the index of the densest row of A. Ignores rows that are not eligible for consideration. Parameters ---------- A : 2-D array An array representing a matrix eligibleRows : 1-D logical array Values indicate whether the corresponding row of A is eligible to be considered Returns ------- i_densest : int Index of the densest row in A eligible for consideration )rr argmax)r eligibleRows rowCountss r _get_densestr#s!&1 I 99Y- ..rcd}d}t|dk(}|tj|ddf}tj||dsd}d}|tj|}||||fS)a Eliminates trivial equations from system of equations defined by Ax = b and identifies trivial infeasibilities Parameters ---------- A : 2-D array An array representing the left-hand side of a system of equations b : 1-D array An array representing the right-hand side of a system of equations Returns ------- A : 2-D array An array representing the left-hand side of a system of equations b : 1-D array An array representing the right-hand side of a system of equations status: int An integer indicating the status of the removal operation 0: No infeasibility identified 2: Trivially infeasible message : str A string descriptor of the exit status of the optimization. rNzbThere is a zero row in A_eq with a nonzero corresponding entry in b_eq. The problem is infeasible.)rr logical_notallclose)rbstatusmessagei_zeros r_remove_zero_rowsr#:st4FG ]a F ".. ! #$A ;;qy! $> ".. !A a  rc|\}}||}td||dd}|d|dz|d|dz|f<||dzd}|||f} ||dzd|fxx|| z z cc<||fS)Nr)lowerdiagr) pluperm_rvjLUpvpermulpivs rbg_update_denser1`s EB fIE aU!!,ADQqS'Bt!tQwK !A#$A QT(CqstQwKAcEK q5Lrc d}d}d}d}d}t||\}}}}|dk7r||||fS|j\}} tt|} t| } g} d} |}t j ||| zfd}t j |d||dd|df<t j |}t j||| zt }t j|jt }t j|dt j|f}|d} | D]}d||<|dkDrd||dz < | |dz }t|| |dd|f|dz }tjj%||d }||}d }tdt'||D]}||t)||zt'|}t+|dd|fj-j/|}||kDj1sa||t j2|z}|| |<d |||z <|j4j/|j7d d}tjj9|} t+|d| zz |kDr d}|}||||fcS| j;||t'| ||z k(snt=t|}!t|!t=| z }!||!ddf||!||fS#t$rgtjj!|dd| f}|\}}tt|} t#|D]\}}| || |c| |<| |<Y wxYw)a Eliminates redundant equations from system of equations defined by Ax = b and identifies infeasibilities. Parameters ---------- A : 2-D array An matrix representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations Returns ------- A : 2-D array A matrix representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations status: int An integer indicating the status of the system 0: No infeasibility identified 2: Trivially infeasible message : str A string descriptor of the exit status of the optimization. References ---------- .. [2] Andersen, Erling D. "Finding all linearly dependent rows in large-scale linear programming." Optimization Methods and Software 6.3 (1995): 219-227. :0yE>rrThere is a linear combination of rows of A_eq that results in zero, suggesting a redundant constraint. However the same linear combination of b_eq is nonzero, suggesting that the constraints conflict and the problem is infeasible.NF)orderr)dtype)trans2Fr)r#shapelistranger zeros fill_diagonalarangeintonesbooleyer1 Exceptionscipylinalg lu_factor enumeratelu_solvelenminr transposedotanyrTreshapenormappendset)"rrhs true_ranktolapiv tolprimalr r! inconsistentmnr)rdr(A_orige js_candidatesjs_maskluir*r+r,i1i2pijsbatchj_index j_indicescbibarbnormkeeps" r_remove_redundancy_pivot_densernlsz@GI FG5L 037AsFG {#vw&& 77DAq U1XA QA A F F !QU3'AQAaeH  AIIa1C0Mggm))6G  ryy| +B UF *! q5AacF @!A#A VQq!tWac:B\\ " "2q " 2 7 #QB/ G7C s2w$?@IAalO--/33B78AG   "w1-.!$!  DDHHS[[Q/0EIINN3'E5z1U7#i/&sFG33 (SVq9}-DU*X uQx=D s1v D $'?CIvw 66K @''!Q$0BEB%(^F#A, @B)/VBZ&r F2J @  @s; K77A,M'&M'cd}d}d}d}d}t||\}}}}|dk7r||||fS|j\}}tt|} t| } t t|||z} g} |} t j jt j j||fj}tj|}| D]a}|dd| f}d||<|dkDrd||dz <t j jj|j|jdd}t| t | z }tj |dd|fjj#||kDj%d}t'|dkDr||d}|| |<|j(j#|jdd}tjj+|}t!|d|zz |kDr d}|}| |||fcS| j-|dt t|}t|t | z }| |ddf||||fS) a Eliminates redundant equations from system of equations defined by Ax = b and identifies infeasibilities. Parameters ---------- A : 2-D sparse array An matrix representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations Returns ------- A : 2-D sparse array A matrix representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations status: int An integer indicating the status of the system 0: No infeasibility identified 2: Trivially infeasible message : str A string descriptor of the exit status of the optimization. References ---------- .. [2] Andersen, Erling D. "Finding all linearly dependent rows in large-scale linear programming." Optimization Methods and Software 6.3 (1995): 219-227. r3rrr4Nrr:r)r#r;r<r=rTrFsparsehstack eye_arraytocscr r>rGspsolverMrQr rNnonzerorKrPrRrS)rrUrWrXr r!rYrZr[r)rkr\r]r^rbBrerfrjr*rkrlrms r_remove_redundancy_pivot_sparserxsQBGI FG5L 037AsFG {#vw&& 77DAq U1XA QA E!QqSMA A F U\\33A6:;AACA  A,( adG! q5AacF \\ ( ( : B B2q I !CF(^VVAaeH&&(,,R0 1G ; D D Fq I q6A:1Q4AAaDDDHHS[[Q/0EIINN3'E5z1u9% 1&sFG33 Q(T uQx=D s1v D $'?CIvw 66rct||\}}}}|dk7r||||fSt|\}}}tjtj }|j t |jz|z}|j\} } | | kr|dnd} t| |kr.|dddf} tj| |dzkD} tj| r:tjtj| j||kDrd}d}ntjtj| j||dzkDrd}d }ntt|| }tj||d }tj||}t|\}}}|j\} } | | kr|dnd} t| |kr.||||fS) a Eliminates redundant equations from system of equations defined by Ax = b and identifies infeasibilities. Parameters ---------- A : 2-D array An array representing the left-hand side of a system of equations b : 1-D array An array representing the right-hand side of a system of equations Returns ------- A : 2-D array An array representing the left-hand side of a system of equations b : 1-D array An array representing the right-hand side of a system of equations status: int An integer indicating the status of the system 0: No infeasibility identified 2: Trivially infeasible message : str A string descriptor of the exit status of the optimization. References ---------- .. [2] Andersen, Erling D. "Finding all linearly dependent rows in large-scale linear programming." Optimization Methods and Software 6.3 (1995): 219-227. rr:NgcAzDue to numerical issues, redundant equality constraints could not be removed automatically. Try providing your constraint matrices as sparse matrices to activate sparse presolve, try turning off redundancy removal, or try turning off presolve altogether.drr4r ) r#rr finfofloatepsmaxr;r rOrNrdelete)rrr r!UsVhr~rrZr[s_minr)ri_removes r_remove_redundancy_svdrjsB.a3Aq&' {!VW$$1vHAq" ((5/  C %%'CL 3 &C 77DAq!VAbEE" e*s  aeHvvay3:- vvl#rvvbffQUU1X.>.D'EF%G  66"&&q"S3Y. /F8G <0 IIa * IIa "q61bww1a"Q7 e*s : a  rcvd}d}d}t||\}}}}|dk7r||||fS|j\}}|} |tjj |} t |j | |\} } tj|| d| | z|| | dsd}|}t| d| } || ddf} || } | | ||fS)aEliminates redundant equations from a system of equations. Eliminates redundant equations from system of equations defined by Ax = b and identifies infeasibilities. Parameters ---------- A : 2-D array An array representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations rank : int, optional The rank of A randomized: bool, optional True for randomized interpolative decomposition Returns ------- A : 2-D array An array representing the left-hand side of a system of equations rhs : 1-D array An array representing the right-hand side of a system of equations status: int An integer indicating the status of the system 0: No infeasibility identified 2: Trivially infeasible message : str A string descriptor of the exit status of the optimization. rrr4N)randr) r#r;r rG matrix_rankrrPrsorted)rrUrank randomizedr r!rYrZr[rvidxprojA2rhs2s r_remove_redundancy_idrs@FG5L 037AsFG {#vw&& 77DAq A | II ! !! $acc1:6IC ;;s3r7|d*CABL 9 Ra/C 36B s8D tVW $$r)N)NT)__doc__numpyr scipy.linalgrscipy.linalg.interpolativerrFscipy.linalg.blasrrrr#r1rnrxrrrrrsK 4 #:*/.#!L |7~|7~Y!xD%r