Repository: scipy/scipy

Issue:
linprog(method='simplex') callback bug
`linprog(method='simplex')` is not working for the following example, which is really just `test_enzo_example_b` with the example callback function. The failure isn't particular to this callback function; even an empty callback function (`pass`) produces the same error. I haven't looked deeply into the cause but I suspect there is something  wrong with `_linprog_simplex.py`:
```
        if callback is not None:
            solution[basis[:n]] = T[:n, -1]
            x = solution[:m]
            c, A_ub, b_ub, A_eq, b_eq, bounds, undo = _T_o
            x, fun, slack, con, _, _ = _postsolve(
                x, c, A_ub, b_ub, A_eq, b_eq, bounds, undo=undo, tol=tol
            )
```
specifically the `x = solution[:m]` line. `x` needs all the decision variables, and I don't think `m` of them is all of them.

### Reproducing code example:
```
import scipy
import numpy as np
from scipy.optimize import linprog

c = [2.8, 6.3, 10.8, -2.8, -6.3, -10.8]
A_eq = [[-1, -1, -1, 0, 0, 0],
        [0, 0, 0, 1, 1, 1],
        [1, 0, 0, 1, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [0, 0, 1, 0, 0, 1]]
b_eq = [-0.5, 0.4, 0.3, 0.3, 0.3]

res = linprog(c, A_eq, b_eq, callback=scipy.optimize.linprog_verbose_callback, method = "simplex")
```

### Error message:

```
Traceback (most recent call last):

  File "<ipython-input-2-63fa2cae687b>", line 1, in <module>
    runfile('/Users/matthaberland/Desktop/test_simplex_rs.py', wdir='/Users/matthaberland/Desktop')

  File "/anaconda3/envs/scipydev/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 678, in runfile
    execfile(filename, namespace)

  File "/anaconda3/envs/scipydev/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 106, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/matthaberland/Desktop/test_simplex_rs.py", line 30, in <module>
    res = linprog(c, A_eq, b_eq, callback=scipy.optimize.linprog_verbose_callback, method = "simplex")

  File "/Users/matthaberland/Desktop/scipydev/scipy/scipy/optimize/_linprog.py", line 474, in linprog
    c, c0=c0, A=A, b=b, callback=callback, _T_o=T_o, **solver_options)

  File "/Users/matthaberland/Desktop/scipydev/scipy/scipy/optimize/_linprog_simplex.py", line 583, in _linprog_simplex
    maxiter=maxiter, tol=tol, bland=bland, _T_o=_T_o)

  File "/Users/matthaberland/Desktop/scipydev/scipy/scipy/optimize/_linprog_simplex.py", line 377, in _solve_simplex
    x, c, A_ub, b_ub, A_eq, b_eq, bounds, undo=undo, tol=tol

  File "/Users/matthaberland/Desktop/scipydev/scipy/scipy/optimize/_linprog_util.py", line 1122, in _postsolve
    x[i] += lb

IndexError: index 5 is out of bounds for axis 0 with size 5
```

### Scipy/Numpy/Python version information:
```
1.3.0.dev0+5a88532 1.15.0 sys.version_info(major=3, minor=6, micro=6, releaselevel='final', serial=0)
```



Discussion:
@Kai-Striega no rush on this, but I imagine this would be an easy fix for you. Or, if you can identify the fix, I can implement it. That might be nice to avoid merge conflicts, as I'll be working heavily on `linprog` again.

Resolving patch:
diff --git a/scipy/optimize/_linprog_simplex.py b/scipy/optimize/_linprog_simplex.py
--- a/scipy/optimize/_linprog_simplex.py
+++ b/scipy/optimize/_linprog_simplex.py
@@ -324,9 +324,9 @@ def _solve_simplex(T, n, basis, maxiter=1000, phase=2, status=0, message='',
     complete = False
 
     if phase == 1:
-        m = T.shape[0]-2
+        m = T.shape[1]-2
     elif phase == 2:
-        m = T.shape[0]-1
+        m = T.shape[1]-1
     else:
         raise ValueError("Argument 'phase' to _solve_simplex must be 1 or 2")
 
@@ -370,6 +370,7 @@ def _solve_simplex(T, n, basis, maxiter=1000, phase=2, status=0, message='',
                 complete = True
 
         if callback is not None:
+            solution[:] = 0
             solution[basis[:n]] = T[:n, -1]
             x = solution[:m]
             c, A_ub, b_ub, A_eq, b_eq, bounds, undo = _T_o


Test patch:
diff --git a/scipy/optimize/tests/test_linprog.py b/scipy/optimize/tests/test_linprog.py
--- a/scipy/optimize/tests/test_linprog.py
+++ b/scipy/optimize/tests/test_linprog.py
@@ -1165,12 +1165,20 @@ def cb(res):
                 last_cb['slack'] = res['slack']
                 last_cb['con'] = res['con']
 
-        c = np.array([-3, -2])
-        A_ub = [[2, 1], [1, 1], [1, 0]]
-        b_ub = [10, 8, 4]
-        res = linprog(c, A_ub=A_ub, b_ub=b_ub, callback=cb, method=self.method)
+        c = [2.8, 6.3, 10.8, -2.8, -6.3, -10.8]
+        A_eq = [[-1, -1, -1, 0, 0, 0],
+                [0, 0, 0, 1, 1, 1],
+                [1, 0, 0, 1, 0, 0],
+                [0, 1, 0, 0, 1, 0],
+                [0, 0, 1, 0, 0, 1]]
+        b_eq = [-0.5, 0.4, 0.3, 0.3, 0.3]
+
+        desired_fun = -1.77
+        desired_x = [0.3, 0.2, 0, 0, 0.1, 0.3]
+        with pytest.warns(OptimizeWarning):
+            res = linprog(c, A_eq=A_eq, b_eq=b_eq, callback=cb, method=self.method)
 
-        _assert_success(res, desired_fun=-18.0, desired_x=[2, 6])
+        _assert_success(res, desired_fun=-1.77, desired_x=desired_x)
         assert_allclose(last_cb['fun'], res['fun'])
         assert_allclose(last_cb['x'], res['x'])
         assert_allclose(last_cb['con'], res['con'])