Hi Team!
I encountered this during the 10th Advent of Code exercise this year,
and I cannot figure out what I am doing wrong. I have a 5x5 matrix with
a 5 dimensional vector as input for the equality, and a 5 dimensional
vector for minimization. However, I get a mismatched dimensions
error???
(see bottom for copy-paste-able code)
Python 3.9.2 (default, Mar 20 2025, 02:07:39)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> sympy.__version__
'1.14.0'
>>> c = sympy.Matrix([1, 1, 1, 1, 1])
>>> A_eq = sympy.Matrix([
... [1, 0, 1, 1, 1],
... [0, 0, 1, 1, 0],
... [1, 0, 0, 0, 1],
... [1, 1, 1, 0, 0],
... [0, 1, 1, 1, 1]])
>>> b_eq = sympy.Matrix([7, 5, 12, 7, 2])
>>> sympy.solvers.linprog(c=c, A_eq=A_eq, b_eq=b_eq)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/solvers/simplex.py", line 1046, in linprog
o, p, d = _simplex(A, b, C)
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/solvers/simplex.py", line 276, in _simplex
M = Matrix([[A, B], [C, D]])
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/matrices/repmatrix.py", line 567, in __new__
return cls._new(*args, **kwargs)
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/matrices/repmatrix.py", line 578, in _new
rows, cols, flat_list = cls._handle_creation_inputs(*args, **kwargs)
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/matrices/matrixbase.py", line 3964, in _handle_creation_inputs
r, c, flatT = cls._handle_creation_inputs(
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/matrices/matrixbase.py", line 3920, in _handle_creation_inputs
raise ValueError('mismatched dimensions')
ValueError: mismatched dimensions
Now, I thought maybe the problem is that I need to transpose the
vectors (the documentation is not particularly clear on the format). If
I transpose b_eq, it raises a different error, this time about
incompatible number of columns (1 and 5):
Python 3.9.2 (default, Mar 20 2025, 02:07:39)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> sympy.__version__
'1.14.0'
>>> c = sympy.Matrix([1, 1, 1, 1, 1])
>>> A_eq = sympy.Matrix([
... [1, 0, 1, 1, 1],
... [0, 0, 1, 1, 0],
... [1, 0, 0, 0, 1],
... [1, 1, 1, 0, 0],
... [0, 1, 1, 1, 1]])
>>> b_eq = sympy.Matrix([7, 5, 12, 7, 2]).transpose()
>>> sympy.solvers.linprog(c=c, A_eq=A_eq, b_eq=b_eq)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/solvers/simplex.py", line 1013, in linprog
b = b.col_join(b_eq)
File "/home/ghorvath/work/python_venv/lib/python3.9/site-packages/sympy/matrices/matrixbase.py", line 351, in col_join
raise ShapeError(
sympy.matrices.exceptions.ShapeError: The matrices have incompatible number of columns (1 and 5)
I tried to transpose c, as well, but that did not seem to change the
output in any of these cases.
Can you help me what I am doing wrong here?
Thanks,
Gábor
PS. copy-paste-able code:
import sympy
sympy.__version__
c = sympy.Matrix([1, 1, 1, 1, 1])
A_eq = sympy.Matrix([
[1, 0, 1, 1, 1],
[0, 0, 1, 1, 0],
[1, 0, 0, 0, 1],
[1, 1, 1, 0, 0],
[0, 1, 1, 1, 1]])
b_eq = sympy.Matrix([7, 5, 12, 7, 2])
sympy.solvers.linprog(c=c, A_eq=A_eq, b_eq=b_eq)