code: def _primal_dual(M, factor=True):
      """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
      """
      if not M:
          return (None, []), (None, [])
      if not hasattr(M, "shape"):
          if len(M) not in (3, 4):
              raise ValueError("expecting Matrix or 3 or 4 lists")
          M = _m(*M)
      m, n = [i - 1 for i in M.shape]
      A, b, c, d = _abcd(M)
      d = d[0]
      _ = lambda x: numbered_symbols(x, start=1)
      x = Matrix([i for i, j in zip(_("x"), range(n))])
      yT = Matrix([i for i, j in zip(_("y"), range(m))]).T
  
      def ineq(L, r, op):
          rv = []
          for r in (op(i, j) for i, j in zip(L, r)):
              if r == True:
                  continue
              elif r == False:
                  return [False]
              if factor:
                  f = factor_terms(r)
                  if f.lhs.is_Mul and f.rhs % f.lhs.args[0] == 0:
                      assert len(f.lhs.args) == 2, f.lhs
                      k = f.lhs.args[0]
                      r = r.func(
                          sign(k) * f.lhs.args[1], f.rhs // abs(k)
                      )
              rv.append(r)
          return rv
  
      eq = lambda x, d: x[0] - d if x else -d
      F = eq(c * x, d)
      f = eq(yT * b, d)
      return (F, ineq(A * x, b, Ge)), (f, ineq(yT * A, c, Le))
  
imports: from sympy.core import sympify
  from sympy.core.exprtools import factor_terms
  from sympy.core.relational import Le, Ge, Eq
  from sympy.core.singleton import S
  from sympy.core.symbol import Dummy
  from sympy.core.sorting import ordered
  from sympy.functions.elementary.complexes import sign
  from sympy.matrices.dense import Matrix, zeros
  from sympy.solvers.solveset import linear_eq_to_matrix
  from sympy.utilities.iterables import numbered_symbols
  from sympy.utilities.misc import filldedent
label_desc: Use image node_id 6 for calling a global function with example usage: _primal_dual(M, factor) and returns: F,  ineq,  f,  ineq, None,  list,  None,  list, rv, list