
    \h̉                     0   S r SSKJr  SSKJr  SSKJrJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr   " S S\5      r " S S\5      rS rS rS S jrS!S jr S"S jr!S#S jr"S r#S r$S r%S r&S r'S r(S$S jr)S$S jr*g)%a  Tools for optimizing a linear function for a given simplex.

For the linear objective function ``f`` with linear constraints
expressed using `Le`, `Ge` or `Eq` can be found with ``lpmin`` or
``lpmax``. The symbols are **unbounded** unless specifically
constrained.

As an alternative, the matrices describing the objective and the
constraints, and an optional list of bounds can be passed to
``linprog`` which will solve for the minimization of ``C*x``
under constraints ``A*x <= b`` and/or ``Aeq*x = beq``, and
individual bounds for variables given as ``(lo, hi)``. The values
returned are **nonnegative** unless bounds are provided that
indicate otherwise.

Errors that might be raised are UnboundedLPError when there is no
finite solution for the system or InfeasibleLPError when the
constraints represent impossible conditions (i.e. a non-existent
 simplex).

Here is a simple 1-D system: minimize `x` given that ``x >= 1``.

    >>> from sympy.solvers.simplex import lpmin, linprog
    >>> from sympy.abc import x

    The function and a list with the constraint is passed directly
    to `lpmin`:

    >>> lpmin(x, [x >= 1])
    (1, {x: 1})

    For `linprog` the matrix for the objective is `[1]` and the
    uivariate constraint can be passed as a bound with None acting
    as infinity:

    >>> linprog([1], bounds=(1, None))
    (1, [1])

    Or the matrices, corresponding to ``x >= 1`` expressed as
    ``-x <= -1`` as required by the routine, can be passed:

    >>> linprog([1], [-1], [-1])
    (1, [1])

    If there is no limit for the objective, an error is raised.
    In this case there is a valid region of interest (simplex)
    but no limit to how small ``x`` can be:

    >>> lpmin(x, [])
    Traceback (most recent call last):
    ...
    sympy.solvers.simplex.UnboundedLPError:
    Objective function can assume arbitrarily large values!

    An error is raised if there is no possible solution:

    >>> lpmin(x,[x<=1,x>=2])
    Traceback (most recent call last):
    ...
    sympy.solvers.simplex.InfeasibleLPError:
    Inconsistent/False constraint
    )sympify)factor_terms)LeGeEq)S)Dummy)ordered)sign)Matrixzeros)linear_eq_to_matrixnumbered_symbols)
filldedentc                       \ rS rSrSrSrg)UnboundedLPErrorM   z
A linear programming problem is said to be unbounded if its objective
function can assume arbitrarily large values.

Example
=======

Suppose you want to maximize
    2x
subject to
    x >= 0

There's no upper limit that 2x can take.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r       M/var/www/auris/envauris/lib/python3.13/site-packages/sympy/solvers/simplex.pyr   r   M   s     	r   r   c                       \ rS rSrSrSrg)InfeasibleLPError`   a;  
A linear programming problem is considered infeasible if its
constraint set is empty. That is, if the set of all vectors
satisfying the constraints is empty, then the problem is infeasible.

Example
=======

Suppose you want to maximize
    x
subject to
    x >= 10
    x <= 9

No x can satisfy those constraints.
r   Nr   r   r   r   r    r    `   s    " 	r   r    c                     XSS24   U SS2U4   XU4   pTnUS:X  a  [        S5      eXX5-  -  -
  nX5-  XaSS24'   U* U-  USS2U4'   SU-  XaU4'   U$ )a  
The pivot element `M[i, j]` is inverted and the rest of the matrix
modified and returned as a new matrix; original is left unmodified.

Example
=======

>>> from sympy.matrices.dense import Matrix
>>> from sympy.solvers.simplex import _pivot
>>> from sympy import var
>>> Matrix(3, 3, var('a:i'))
Matrix([
[a, b, c],
[d, e, f],
[g, h, i]])
>>> _pivot(_, 1, 0)
Matrix([
[-a/d, -a*e/d + b, -a*f/d + c],
[ 1/d,        e/d,        f/d],
[-g/d,  h - e*g/d,  i - f*g/d]])
Nr   z'Tried to pivot about zero-valued entry.   )ZeroDivisionError)MijMiMjMijAs          r   _pivotr,   u   s|    , qD'1QT7AdGCB
ax57 	7	"(OAhAdGcCiAadG#gAdGHr   c                 ,   ^ ^^^ [        UU UUU4S jS9$ )Nc                 (   > TU    TU T4   -  TU    4$ Nr   )r&   r+   BY	pivot_cols    r   <lambda>#_choose_pivot_row.<locals>.<lambda>   s    adQq)|_.Dad-Kr   )key)min)r+   r0   candidate_rowsr2   r1   s   `` ``r   _choose_pivot_rowr8      s     ~#KLLr   Nc                 l  ^^ U TX#=(       d    S/4 Vs/ s H  n[        U5      PM     snu  n mp#U(       a6  [        U R                  * UR                  TR                  U* 5      u  pgnU* Xx4$ U (       a  T(       a  [        U T/X#//5      n	O&U (       d  T(       a  [        S5      e[        X#//5      n	U	R                  S-
  n
U	R
                  S-
  n[        S U	 5       5      (       d  [        [        S5      5      e[        U
5       Vs/ s H  nSU4PM	     snm[        U5       Vs/ s H  nSU4PM	     nnSn U	SS	2S	4   mU	SS	2SS	24   n [        U4S
 j[        TR
                  5       5       5      (       a  GO[        TR
                  5       H  nTU   S:  d  M    O    [        U R                  5       Vs/ s H  nU WU4   S:  d  M  UPM     nnU(       d  [        [        S5      5      e[        U4S jU 5       5      u  nn[        U R
                  5       Vs/ s H  nU UU4   S:  d  M  TU   S:  d  M  UPM     nnUR                  W5        [        U TUUU5      nUU4U:X  a  SnO%UU4n[        U	UU5      n	UU   TU   sTU'   UU'   GM]   U	SS	2S	4   mU	SS	2SS	24   n U	S	SS	24   n[        U
5       Vs/ s H  nUU   S:  d  M  UPM     nnU(       d  O[        U4S jU 5       5      u  nn[        U5       Vs/ s H  nU UU4   S:  d  M  UPM     nnU(       d  [        [        S5      5      e[        U TUUU5      n[        U	UU5      n	UU   TU   sTU'   UU'   M  S/U
-  nS/U-  n[!        T5       H   u  nu  nn
US:X  a  SUU
'   M  U	S	U4   UU
'   M"     [!        U5       H  u  nu  nn
US:X  a  SUU
'   M  XS	4   UU
'   M!     U(       a.  [        S UU-    5       5      (       d  [        [        S5      5      eU	S   * UU4$ s  snf s  snf s  snf s  snf s  snf s  snf s  snf )a  Return ``(o, x, y)`` obtained from the two-phase simplex method
using Bland's rule: ``o`` is the minimum value of primal,
``Cx - D``, under constraints ``Ax <= B`` (with ``x >= 0``) and
the maximum of the dual, ``y^{T}B - D``, under constraints
``A^{T}*y >= C^{T}`` (with ``y >= 0``). To compute the dual of
the system, pass `dual=True` and ``(o, y, x)`` will be returned.

Note: the nonnegative constraints for ``x`` and ``y`` supercede
any values of ``A`` and ``B`` that are inconsistent with that
assumption, so if a constraint of ``x >= -1`` is represented
in ``A`` and ``B``, no value will be obtained that is negative; if
a constraint of ``x <= -1`` is represented, an error will be
raised since no solution is possible.

This routine relies on the ability of determining whether an
expression is 0 or not. This is guaranteed if the input contains
only Float or Rational entries. It will raise a TypeError if
a relationship does not evaluate to True or False.

Examples
========

>>> from sympy.solvers.simplex import _simplex
>>> from sympy import Matrix

Consider the simple minimization of ``f = x + y + 1`` under the
constraint that ``y + 2*x >= 4``. This is the "standard form" of
a minimization.

In the nonnegative quadrant, this inequality describes a area above
a triangle with vertices at (0, 4), (0, 0) and (2, 0). The minimum
of ``f`` occurs at (2, 0). Define A, B, C, D for the standard
minimization:

>>> A = Matrix([[2, 1]])
>>> B = Matrix([4])
>>> C = Matrix([[1, 1]])
>>> D = Matrix([-1])

Confirm that this is the system of interest:

>>> from sympy.abc import x, y
>>> X = Matrix([x, y])
>>> (C*X - D)[0]
x + y + 1
>>> [i >= j for i, j in zip(A*X, B)]
[2*x + y >= 4]

Since `_simplex` will do a minimization for constraints given as
``A*x <= B``, the signs of ``A`` and ``B`` must be negated since
the currently correspond to a greater-than inequality:

>>> _simplex(-A, -B, C, D)
(3, [2, 0], [1/2])

The dual of minimizing ``f`` is maximizing ``F = c*y - d`` for
``a*y <= b`` where ``a``, ``b``, ``c``, ``d`` are derived from the
transpose of the matrix representation of the standard minimization:

>>> tr = lambda a, b, c, d: [i.T for i in (a, c, b, d)]
>>> a, b, c, d = tr(A, B, C, D)

This time ``a*x <= b`` is the expected inequality for the `_simplex`
method, but to maximize ``F``, the sign of ``c`` and ``d`` must be
changed (so that minimizing the negative will give the negative of
the maximum of ``F``):

>>> _simplex(a, b, -c, -d)
(-3, [1/2], [2, 0])

The negative of ``F`` and the min of ``f`` are the same. The dual
point `[1/2]` is the value of ``y`` that minimized ``F = c*y - d``
under constraints a*x <= b``:

>>> y = Matrix(['y'])
>>> (c*y - d)[0]
4*y + 1
>>> [i <= j for i, j in zip(a*y,b)]
[2*y <= 1, y <= 1]

In this 1-dimensional dual system, the more restrictive constraint is
the first which limits ``y`` between 0 and 1/2 and the maximum of
``F`` is attained at the nonzero value, hence is ``4*(1/2) + 1 = 3``.

In this case the values for ``x`` and ``y`` were the same when the
dual representation was solved. This is not always the case (though
the value of the function will be the same).

>>> l = [[1, 1], [-1, 1], [0, 1], [-1, 0]], [5, 1, 2, -1], [[1, 1]], [-1]
>>> A, B, C, D = [Matrix(i) for i in l]
>>> _simplex(A, B, -C, -D)
(-6, [3, 2], [1, 0, 0, 0])
>>> _simplex(A, B, -C, -D, dual=True)  # [5, 0] != [3, 2]
(-6, [1, 0, 0, 0], [5, 0])

In both cases the function has the same value:

>>> Matrix(C)*Matrix([3, 2]) == Matrix(C)*Matrix([5, 0])
True

See Also
========
_lp - poses min/max problem in form compatible with _simplex
lpmin - minimization which calls _lp
lpmax - maximimzation which calls _lp

References
==========

.. [1] Thomas S. Ferguson, LINEAR PROGRAMMING: A Concise Introduction
       web.tecnico.ulisboa.pt/mcasquilho/acad/or/ftp/FergusonUCLA_lp.pdf

r   zmust give A and Br#   c              3   ^   #    U  H#  oR                   =(       d    UR                  v   M%     g 7fr/   )is_Floatis_Rational.0r&   s     r   	<genexpr>_simplex.<locals>.<genexpr>  s     6Aqzz*Q]]*As   +-z@
            Only rationals and floats are allowed.
            FTNc              3   4   >#    U  H  nTU   S :  v   M     g7fr   Nr   )r>   r&   r0   s     r   r?   r@   3  s     0-Qqtqy-s   z-
                The constraint set is empty!c              3   2   >#    U  H  nTU   U4v   M     g 7fr/   r   r>   r&   Xs     r   r?   r@   C       /hAaD!9h   c              3   2   >#    U  H  nTU   U4v   M     g 7fr/   r   rE   s     r   r?   r@   m  rG   rH   zX
                Objective function can assume
                arbitrarily large values!c              3   *   #    U  H	  oS :  v   M     g7frC   r   r=   s     r   r?   r@     s     =(<1Q(<s   z
            Oscillating system led to invalid solution.
            If you believe there was a valid solution, please
            report this as a bug.rA   rA   )r   _simplexT
ValueErrorcolsrowsall	TypeErrorr   ranger    r6   appendr8   r,   r   	enumerate)r+   r0   CDdualr&   _odpr%   nmr'   r1   lastk_piv_colscpiv_rowsrargmaxargmin_dualvrF   s    `                      @r   rL   rL      s   d '(AxQC%89%8&)%89JAq!QSSD!##qssQB/qsAyQQFQF#$011QF8	
A	
A6A666 
 $  
 	
 #1X&X%X&A!!H%Hq$HA%
 D
crc2gJcrc3B3hK0%-000 qvvAtax    %QVV}<}!!Q$!A}<#J 00 %1 2 2/h//1  %QVV}I}!!Q$!A!qA}IaHa3 q6T>" D!t1aOqT1Q4
!adc h crc2gJcrc3B3hKb#2#gJ  %Qx4x!1Q4!8Ax4/h//1  %Qx7x!1QT7Q;Ax7": /- $. / / aHa31aOqT1Q4
!ad) , VaZF&1*Kq\	6Aq:F1Ir1uXKN	 " q\	6Aq9KNR%F1I	 " C=(<===
 ,% !& ' 	' fI:v{**A :4 	'%, = JH 5 8sG   PP&P;P"P" P'3P'>P'P,P,P1P1c                     S nU SS2SS24   U SS2S4   U SSS24   U SS2SS24   4nU(       d  U$ [        U Vs/ s H
  oB" U5      PM     sn5      $ s  snf )a  return parts of M as matrices or lists

Examples
========

>>> from sympy import Matrix
>>> from sympy.solvers.simplex import _abcd

>>> m = Matrix(3, 3, range(9)); m
Matrix([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a, b, c, d = _abcd(m)
>>> a
Matrix([
[0, 1],
[3, 4]])
>>> b
Matrix([
[2],
[5]])
>>> c
Matrix([[6, 7]])
>>> d
Matrix([[8]])

The matrices can be returned as compact lists, too:

>>> L = a, b, c, d = _abcd(m, list=True); L
([[0, 1], [3, 4]], [2, 5], [[6, 7]], [8])
c                 ~    U R                  5       n[        US   5      S:X  a  U V s/ s H  o S   PM	     sn $ U$ s  sn f )Nr   r#   )tolistlen)r&   ls     r   aslist_abcd.<locals>.aslist  s=    HHJqt9>"#$!QaD!$$ %s   :NrA   )tuple)r%   listrm   r]   r&   s        r   _abcdrq     su    D 	
#2#ss(QssBwZ2ss7QrsBCx[8AQ'Q&)Q'(('s   Ac                     XX#=(       d    S/4 Vs/ s H  n[        U5      PM     snu  pp#[        X/X#//5      $ s  snf )a  return Matrix([[a, b], [c, d]]) from matrices
in Matrix or list form.

Examples
========

>>> from sympy import Matrix
>>> from sympy.solvers.simplex import _abcd, _m
>>> m = Matrix(3, 3, range(9))
>>> L = _abcd(m, list=True); L
([[0, 1], [3, 4]], [2, 5], [[6, 7]], [8])
>>> _abcd(m)
(Matrix([
[0, 1],
[3, 4]]), Matrix([
[2],
[5]]), Matrix([[6, 7]]), Matrix([[8]]))
>>> assert m == _m(*L) == _m(*_)
r   )r   )abrb   rZ   r&   s        r   _mru     sF    ( '(AxQC%89%8&)%89JA!A6A6"## :s   ;c                   ^ U (       d  S/ 4S/ 44$ [        U S5      (       d"  [        U 5      S;  a  [        S5      e[        U 6 n U R                   Vs/ s H  o"S-
  PM	     snu  p4[        U 5      u  pVpxUS   nS n	[        [        U	" S5      [        U5      5       VV
s/ s H  u  p*UPM	     sn
n5      n[        [        U	" S	5      [        U5      5       VV
s/ s H  u  p*UPM	     sn
n5      R                  nU4S
 jnS nU" X{-  U5      nU" X-  U5      nX" X[-  U[        5      4UU" X-  U[        5      44$ s  snf s  sn
nf s  sn
nf )a?  return primal and dual function and constraints
assuming that ``M = Matrix([[A, b], [c, d]])`` and the
function ``c*x - d`` is being minimized with ``Ax >= b``
for nonnegative values of ``x``. The dual and its
constraints will be for maximizing `b.T*y - d` subject
to ``A.T*y <= c.T``.

Examples
========

>>> from sympy.solvers.simplex import _primal_dual, lpmin, lpmax
>>> from sympy import Matrix

The following matrix represents the primal task of
minimizing x + y + 7 for y >= x + 1 and y >= -2*x + 3.
The dual task seeks to maximize x + 3*y + 7 with
2*y - x <= 1 and and x + y <= 1:

>>> M = Matrix([
...     [-1, 1,  1],
...     [ 2, 1,  3],
...     [ 1, 1, -7]])
>>> p, d = _primal_dual(M)

The minimum of the primal and maximum of the dual are the same
(though they occur at different points):

>>> lpmin(*p)
(28/3, {x1: 2/3, x2: 5/3})
>>> lpmax(*d)
(28/3, {y1: 1/3, y2: 2/3})

If the equivalent (but canonical) inequalities are
desired, leave `factor=True`, otherwise the unmodified
inequalities for M will be returned.

>>> m = Matrix([
... [-3, -2,  4, -2],
... [ 2,  0,  0, -2],
... [ 0,  1, -3,  0]])

>>> _primal_dual(m, False)  # last condition is 2*x1 >= -2
((x2 - 3*x3,
    [-3*x1 - 2*x2 + 4*x3 >= -2, 2*x1 >= -2]),
(-2*y1 - 2*y2,
    [-3*y1 + 2*y2 <= 0, -2*y1 <= 1, 4*y1 <= -3]))

>>> _primal_dual(m)  # condition now x1 >= -1
((x2 - 3*x3,
    [-3*x1 - 2*x2 + 4*x3 >= -2, x1 >= -1]),
(-2*y1 - 2*y2,
    [-3*y1 + 2*y2 <= 0, -2*y1 <= 1, 4*y1 <= -3]))

If you pass the transpose of the matrix, the primal will be
identified as the standard minimization problem and the
dual as the standard maximization:

>>> _primal_dual(m.T)
((-2*x1 - 2*x2,
    [-3*x1 + 2*x2 >= 0, -2*x1 >= 1, 4*x1 >= -3]),
(y2 - 3*y3,
    [-3*y1 - 2*y2 + 4*y3 <= -2, y1 <= -1]))

A matrix must have some size or else None will be returned for
the functions:

>>> _primal_dual(Matrix([[1, 2]]))
((x1 - 2, []), (-2, []))

>>> _primal_dual(Matrix([]))
((None, []), (None, []))

References
==========

.. [1] David Galvin, Relations between Primal and Dual
       www3.nd.edu/~dgalvin1/30210/30210_F07/presentations/dual_opt.pdf
Nshape)      z expecting Matrix or 3 or 4 listsr#   r   c                     [        U SS9$ )Nr#   )startr   )xs    r   r3   _primal_dual.<locals>.<lambda>2  s    "1A.r   r|   yc                 p  >^ / nU4S j[        X5       5        GH  nUS:X  a  M  US:X  a  S/s  $ T(       a  [        U5      nUR                  R                  (       a  UR                  UR                  R
                  S   -  S:X  a  [        UR                  R
                  5      S:X  d   UR                  5       eUR                  R
                  S   nUR                  [        U5      UR                  R
                  S   -  UR                  [        U5      -  5      nUR                  U5        GM     U$ )Nc              3   8   >#    U  H  u  pT" X5      v   M     g 7fr/   r   )r>   r&   r'   ops      r   r?   -_primal_dual.<locals>.ineq.<locals>.<genexpr>8  s     1ytq"Q((ys   TFr      r#   )zipr   lhsis_Mulrhsargsrk   funcr   absrT   )Lrd   r   rvfr_   factors     `   r   ineq_primal_dual.<locals>.ineq6  s    1s1y1ADyew O55<<AEEAEEJJqM$9Q$>quuzz?a/66/

1AtAwA6QHAIIaL 2 	r   c                 &    U (       a  U S   U-
  $ U* $ Nr   r   )r|   rZ   s     r   r3   r}   F  s    !adQh+!+r   )hasattrrk   rN   ru   rw   rq   r   r   rS   rM   r   r   )r%   r   r&   r]   r\   r+   rt   rb   rZ   r`   r'   r|   yTr   eqFr   s    `               r   _primal_dualr     s=   ^ bzD":%%1gq6?@@F77#7aE7#DAqJA!	!A.Ac!C&%(343da345A	s1S658454tq45	6	8	8B  
,B
15!A
261AtAE1b!"QRVQ(;$<<<5 $ 55s   D9D>
E
c                 2   0 n/ n/ n[        SS[        S9n0 n/ n[        U5      nU  GH?  nUS:X  a  M  US:X  a    gUR                  [        R
                  [        R                  5      (       a  [        S5      e[        U[        [        45      (       a  UR                  UR                  -
  nUR                  n	X-
  (       a  [        SX-
  -  5      e[        U	5      S:  a  UR                  U5        M  U	(       aK  U	R!                  5       n
X;   a  M  [        US	SS
9R#                  5       nX;  a  XU
'   GM  Xj==   U-  ss'   GM  U(       a    gGM+  [%        ['        SU-  5      5      e   U H  n
UR)                  U
S5      nU(       d    gUS:X  a  UR                  U
5        M7  UR*                  UR,                  pUR.                  (       a$  [1        U5      nX-
  X*'   UR                  U5        M  UR.                  (       a-  U(       a$  [1        U5      nX-   X*'   UR                  U5        M  M  [1        U5      nUR                  U5        X-   X*'   UR                  XU-
  -
  5        M     U H%  n
[1        U5      nX-
  X*'   UR                  U5        M'     X2U4$ )a  return `(np, d, aux)` where `np` is a list of nonpositive
expressions that represent the given constraints (possibly
rewritten in terms of auxilliary variables) expressible with
nonnegative symbols, and `d` is a dictionary mapping a given
symbols to an expression with an auxilliary variable. In some
cases a symbol will be used as part of the change of variables,
e.g. x: x - z1 instead of x: z1 - z2.

If any constraint is False/empty, return None. All variables in
``constr`` are assumed to be unbounded unless explicitly indicated
otherwise with a univariate constraint, e.g. ``x >= 0`` will
restrict ``x`` to nonnegative values.

The ``syms`` must be included so all symbols can be given an
unbounded assumption if they are not otherwise bound with
univariate conditions like ``x <= 3``.

Examples
========

>>> from sympy.solvers.simplex import _rel_as_nonpos
>>> from sympy.abc import x, y
>>> _rel_as_nonpos([x >= y, x >= 0, y >= 0], (x, y))
([-x + y], {}, [])
>>> _rel_as_nonpos([x >= 3, x <= 5], [x])
([_z1 - 2], {x: _z1 + 3}, [_z1])
>>> _rel_as_nonpos([x <= 5], [x])
([], {x: 5 - _z1}, [_z1])
>>> _rel_as_nonpos([x >= 1], [x])
([], {x: _z1 + 1}, [_z1])
zr#   )r{   clsTFNz only finite bounds are permittedz&unexpected symbol(s) in constraint: %sr   )evaluatez
                only equalities like Eq(x, y) or non-strict
                inequalities like x >= y are allowed in lp, not %s)r   r	   sethasr   InfinityNegativeInfinityrN   
isinstancer   r   ltsgtsfree_symbolsrk   rT   popas_setrR   r   getinfsupis_infinitenext)constrsymsrd   npauxui
univariateunboundr&   freeir|   ivlrs   rt   us                  r   _rel_as_nonposr   L  sR   @ 	A	B
C	#QE	2BJGt9D 9:55Q//00?@@a"b""ANNE| <M  5zA~		!IIK<A.557&$'qMMS(M  J (FHI(J K L L9 D NN1d#9NN1uuaee1==RA5ADJJqM]]Hu

1 RAJJqM5AD IIaq5k"9 > Hu

1 
 #:r   c                    [        U 5      nU Vs/ s H  n[        U5      PM     nn[        R                  " / U/U-    Vs/ s H  o3R                  PM     snQ[        5       P76 n[	        [        U5      5       H_  n[        XC   [        5      (       d  M  XC   R                  XC   R                  -
  S:*  XC'   UR                  XC   R                  * S:*  5        Ma     [        XE5      nUc  [        [        S5      5      eUu  pGnUR                  U5      nU Vs/ s H  o3R                  U5      PM     nn[        [!        U5      5      U-   n	[#        XI5      u  p[#        U/U	5      u  pXXXyU4$ s  snf s  snf s  snf )a  return A, B, C, D, r, x+X, X for maximizing
objective = Cx - D with constraints Ax <= B, introducing
introducing auxilliary variables, X, as necessary to make
replacements of symbols as given in r, {xi: expression with Xj},
so all variables in x+X will take on nonnegative values.

Every univariate condition creates a semi-infinite
condition, e.g. a single ``x <= 3`` creates the
interval ``[-oo, 3]`` while ``x <= 3`` and ``x >= 2``
create an interval ``[2, 3]``. Variables not in a univariate
expression will take on nonnegative values.
r   z*
            Inconsistent/False constraint)r   r   unionr   rS   rk   r   r   r   r   rT   r   r    r   xreplacerp   r
   r   )	objectiveconstraintsr   r&   r   r   r`   rd   r   xxr+   r0   rV   rW   s                 r   _lp_matricesr     sV    		A)	*k'!*kB	*99@sRx8x!~~x8@#%@D 3r7^beR  EII		)Q.BEIIruyyjAo&  	r Ay
 ,- !. / 	/JB3 	


1A!#	$A**Q-B	$ 
gdm	s	"Br&DAsB'DAqc!!1 
+8" 
%s   E- E2
E7c                    [        X5      u  p4pVpxn	[        U 5      R                  5       n
SU
;   a  [        X4U* U* 5      u  pnU* nO!SU
;   a  [        X4XV5      u  pnO[	        S5      e[        [        X5      5      nU(       ak  UR                  5        VVs0 s H  u  nnUUR                  U5      _M     nnnUR                  U5        [        U5       Vs0 s H  oU	;  d  M
  XU   _M     nnX4$ s  snnf s  snf )a  Return the optimization (min or max) of ``f`` with the given
constraints. All variables are unbounded unless constrained.

If `min_max` is 'max' then the results corresponding to the
maximization of ``f`` will be returned, else the minimization.
The constraints can be given as Le, Ge or Eq expressions.

Examples
========

>>> from sympy.solvers.simplex import _lp as lp
>>> from sympy import Eq
>>> from sympy.abc import x, y, z
>>> f = x + y - 2*z
>>> c = [7*x + 4*y - 7*z <= 3, 3*x - y + 10*z <= 6]
>>> c += [i >= 0 for i in (x, y, z)]
>>> lp(min, f, c)
(-6/5, {x: 0, y: 0, z: 3/5})

By passing max, the maximum value for f under the constraints
is returned (if possible):

>>> lp(max, f, c)
(3/4, {x: 0, y: 3/4, z: 0})

Constraints that are equalities will require that the solution
also satisfy them:

>>> lp(max, f, c + [Eq(y - 9*x, 1)])
(5/7, {x: 0, y: 1, z: 1/7})

All symbols are reported, even if they are not in the objective
function:

>>> lp(min, x, [y + x >= 3, x >= 0])
(0, {x: 0, y: 3})
maxr6   zexpecting min or max)r   strlowerrL   rN   dictr   itemsr   updater
   )min_maxr   r   r+   r0   rV   rW   rd   r   r   howrY   r[   rZ   or_   rg   s                    r   _lpr     s    P *!4A!s
g,


C| A1"qb)qC	#1&a/00 	SZA*+'')4)$!QQ

1)4	%aj9jSLWQ!Wj9 4K 5 :s   C0	C6"	C6c                 "    [        [        X5      $ )aO  return minimum of linear equation ``f`` under
linear constraints expressed using Ge, Le or Eq.

All variables are unbounded unless constrained.

Examples
========

>>> from sympy.solvers.simplex import lpmin
>>> from sympy import Eq
>>> from sympy.abc import x, y
>>> lpmin(x, [2*x - 3*y >= -1, Eq(x + 3*y, 2), x <= 2*y])
(1/3, {x: 1/3, y: 5/9})

Negative values for variables are permitted unless explicitly
excluding, so minimizing ``x`` for ``x <= 3`` is an
unbounded problem while the following has a bounded solution:

>>> lpmin(x, [x >= 0, x <= 3])
(0, {x: 0})

Without indicating that ``x`` is nonnegative, there
is no minimum for this objective:

>>> lpmin(x, [x <= 3])
Traceback (most recent call last):
...
sympy.solvers.simplex.UnboundedLPError:
Objective function can assume arbitrarily large values!

See Also
========
linprog, lpmax
)r   r6   r   r   s     r   lpminr   .  s    F sAr   c                 "    [        [        X5      $ )a  return maximum of linear equation ``f`` under
linear constraints expressed using Ge, Le or Eq.

All variables are unbounded unless constrained.

Examples
========

>>> from sympy.solvers.simplex import lpmax
>>> from sympy import Eq
>>> from sympy.abc import x, y
>>> lpmax(x, [2*x - 3*y >= -1, Eq(x+ 3*y,2), x <= 2*y])
(4/5, {x: 4/5, y: 2/5})

Negative values for variables are permitted unless explicitly
excluding:

>>> lpmax(x, [x <= -1])
(-1, {x: -1})

If a non-negative constraint is added for x, there is no
possible solution:

>>> lpmax(x, [x <= -1, x >= 0])
Traceback (most recent call last):
...
sympy.solvers.simplex.InfeasibleLPError: inconsistent/False constraint

See Also
========
linprog, lpmin
)r   r   r   s     r   lpmaxr   T  s    B sAr   c                    S[         4S jn/ n/ n/ n[        U 5      n[        U 5       GHE  u  nu  pxUc  Uc  UR                  U5        M"  UcS  US-  nUR                  U" XVS4S/5      5        UR                  U" XVS4S/5      5        UR	                  U/U* //5        Mx  Uc\  U(       aS  US-  nUR                  U" XVS4S/5      5        UR                  U" XVS4S/5      5        UR	                  U/U* //5        M  M  US-  nUR                  U" XVS4S/5      5        UR                  U" XVS4S/5      5        UR                  U" US/5      5        UR	                  U/U* /X-
  //5        GMH     U HU  nUS-  nUR                  U" XVS4SS/5      5        UR                  U" XVS4SS	/5      5        UR	                  S
/S
//5        MW     [        U V	s/ s H  oS
/U[        U	5      -
  -  -   PM     sn	5      [        U5      4$ s  sn	f )Nlengthc                 0    S/U -  nU H	  u  p4XBU'   M     U$ r   r   )r   index_value_pairsliidxvals        r   
_make_list"_handle_bounds.<locals>._make_list|  s%    S6\)HCsG *	r   r#   )rA   r#   rA   rK   r   )rA   )r   r#   r   )intrk   rU   rT   extendr   )
boundsr   r   rowrow2b_lenr|   rs   rt   rd   s
             r   _handle_boundsr   x  s   3  G
CDKEv&	6A9NN1YQJEJJz%a&'):;<JJz%b'8)<=>KK!rd$Y


:e!fh-?@A

:e"gw-?@AaSA2$K(  QJEJJz%a&();<=JJz%b'7);<=JJz%'34KK!rdQUG,-5 ': 


:e!fgx%@AB

:e"gx%ABCaS1#J  S9SsECFN++S9:F4LHH9s   Hc                    [        U 5      nUR                  S:w  a  UR                  S:X  a  UR                  nUR                  S:w  a  [	        S5      eU(       d>  U(       a  [	        S5      e[        SUR                  5      [        UR                  S5      p!OX4 Vs/ s H  n[        U5      PM     snu  pUR                  UR                  :w  a  [	        S5      eUc  Ub  [	        S5      eOeX44 Vs/ s H  n[        U5      PM     snu  p4UR                  U5      nUR                  U* 5      nUR                  U5      nUR                  U* 5      nUGbr  U0 :X  Gdk  US:X  Gdd  [        U5      [        L a   [        U5      S	:X  a  U/UR                  -  nO[        U5      UR                  :X  a  [        S
 U 5       5      (       a  O~[        U5      [        L a^  [        S UR                  5        5       5      (       a9  UnS/UR                  -  nU(       a  UR                  5       u  pyXU'   U(       a  M  O[	        SU-  5      e[        U5      u  pU
R                  UR                  -
  nU(       a6  [        U[        UR                  U5      /U
//5      nUR                  U5      nOU
nUnUR                  [        SU5      5      nOUR                  * n[!        XU5      u  pnXSU*  4$ s  snf s  snf )a(  Return the minimization of ``c*x`` with the given
constraints ``A*x <= b`` and ``A_eq*x = b_eq``. Unless bounds
are given, variables will have nonnegative values in the solution.

If ``A`` is not given, then the dimension of the system will
be determined by the length of ``C``.

By default, all variables will be nonnegative. If ``bounds``
is given as a single tuple, ``(lo, hi)``, then all variables
will be constrained to be between ``lo`` and ``hi``. Use
None for a ``lo`` or ``hi`` if it is unconstrained in the
negative or positive direction, respectively, e.g.
``(None, 0)`` indicates nonpositive values. To set
individual ranges, pass a list with length equal to the
number of columns in ``A``, each element being a tuple; if
only a few variables take on non-default values they can be
passed as a dictionary with keys giving the corresponding
column to which the variable is assigned, e.g. ``bounds={2:
(1, 4)}`` would limit the 3rd variable to have a value in
range ``[1, 4]``.

Examples
========

>>> from sympy.solvers.simplex import linprog
>>> from sympy import symbols, Eq, linear_eq_to_matrix as M, Matrix
>>> x = x1, x2, x3, x4 = symbols('x1:5')
>>> X = Matrix(x)
>>> c, d = M(5*x2 + x3 + 4*x4 - x1, x)
>>> a, b = M([5*x2 + 2*x3 + 5*x4 - (x1 + 5)], x)
>>> aeq, beq = M([Eq(3*x2 + x4, 2), Eq(-x1 + x3 + 2*x4, 1)], x)
>>> constr = [i <= j for i,j in zip(a*X, b)]
>>> constr += [Eq(i, j) for i,j in zip(aeq*X, beq)]
>>> linprog(c, a, b, aeq, beq)
(9/2, [0, 1/2, 0, 1/2])
>>> assert all(i.subs(dict(zip(x, _[1]))) for i in constr)

See Also
========
lpmin, lpmax
r#   C must be a single row.A and b must both be givenr   'number of columns in A and C must matchN A_eq and b_eq must both be givenrC   r   c              3   n   #    U  H+  n[        U5      [        L =(       a    [        U5      S :H  v   M-     g7fr   Ntypero   rk   r=   s     r   r?   linprog.<locals>.<genexpr>  -      +B:@QQ5 0SVq[0&   35c              3   n   #    U  H+  n[        U5      [        L =(       a    [        U5      S :H  v   M-     g7fr   r   r=   s     r   r?   r     0      **(A Q5 0SVq[0(r   unexpected bounds %s)r   rP   rO   rM   rN   r   col_joinr   ro   rk   rQ   r   valuespopitemr   row_joinrL   )rb   r+   rt   A_eqb_eqr   rV   r&   dbr'   A_b_r   r   r[   rZ   s                   r   linprogr     s   X 	q	Avv{qvv{CCvv{233 9:: Qqvvq!11$%6*6aq	6*vvBCC ||?@@  +/6AfQi6
JJtJJuJJtJJuNflf	.A<5 S[A%5X&F[AFF"s +B:@+B (B (B&\T!c **** '* '* B[166)Fzz|q	 " 3f<=='ggE!&&#./"67A

2AAAJJuQ}%vvgqQGA!#h;a + 7s   K,3K1c           
         SSK Jn  [        U 5      nUR                  S:w  a  UR                  S:X  a  UR
                  nUR                  S:w  a  [        S5      eU(       d>  U(       a  [        S5      e[        SUR                  5      [        UR                  S5      p!OX4 Vs/ s H  n[        U5      PM     snu  pUR                  UR                  :w  a  [        S5      eUc  Ub  [        S5      eOX44 Vs/ s H  n[        U5      PM     snu  p4Ub  U0 :X  d  US:X  d  [        U5      [        L a   [        U5      S	:X  a  U/UR                  -  nO[        U5      UR                  :X  a  [        S
 U 5       5      (       a  O~[        U5      [        L a^  [        S UR                  5        5       5      (       a9  Un	S/UR                  -  nU	(       a  U	R                  5       u  pXU'   U	(       a  M  O[        SU-  5      e[        U" SUR                  S-   -  5      5      nX{-  S   [        X-  U5       VV
s/ s H	  u  pX:*  PM     sn
n[        X;-  U5       VV
s/ s H  u  p[!        X5      PM     sn
n-   p[#        U5       H<  u  nu  pUb  U R%                  X   U:  5        Uc  M&  U R%                  X   U:*  5        M>     X4$ s  snf s  snf s  sn
nf s  sn
nf )Nr   )symbolsr#   r   r   r   r   rC   r   c              3   n   #    U  H+  n[        U5      [        L =(       a    [        U5      S :H  v   M-     g7fr   r   r=   s     r   r?   show_linprog.<locals>.<genexpr>:  r   r   c              3   n   #    U  H+  n[        U5      [        L =(       a    [        U5      S :H  v   M-     g7fr   r   r=   s     r   r?   r   =  r   r   r   zx1:%s)sympyr   r   rP   rO   rM   rN   r   r   ro   rk   rQ   r   r   r   r   r   rU   rT   )rb   r+   rt   r   r   r   r   rV   r&   r   r'   r|   r   lohis                  r   show_linprogr     s   q	Avv{qvv{CCvv{233 9:: Qqvvq!11$%6*6aq	6*vvBCC ||?@@  +/6AfQi6
Nflf	.A<5 S[A%5X&F[AFF"s +B:@+B (B (B&\T!c **** '* '* B[166)Fzz|q	 " 3f<==ww!&&(+,-A3(Sa[1[caQT[1DFSWHX4YHXRWHX4YYa (8B>HHQT2X>HHQT2X	 )
 3JM + 7, 24Ys   #K
9K;KK
)NF)Fr/   )T)NNNNN)+r   
sympy.corer   sympy.core.exprtoolsr   sympy.core.relationalr   r   r   sympy.core.singletonr   sympy.core.symbolr	   sympy.core.sortingr
   $sympy.functions.elementary.complexesr   sympy.matrices.denser   r   sympy.solvers.solvesetr   sympy.utilities.iterablesr   sympy.utilities.miscr   	Exceptionr   r    r,   r8   rL   rq   ru   r   r   r   r   r   r   r   r   r   r   r   r   <module>r     s   =~  - , , " # & 5 . 6 6 +	y 	&		 	*BMr+p+)\$0o=dod("VBJ#L!H2IjjX7r   