<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/1526" rel="nofollow">http://projects.scipy.org/scipy/ticket/1526</a> on 2011-09-29 by trac user tony.stillfjord, assigned to <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/wnbell/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/wnbell">@wnbell</a>.</em></p>
<p dir="auto">With a scipy.sparse.lil_matrix A, assignments of the form A[L1, L2] = 0, where L1 and L2 are two index lists, give the wrong results. If either L1 or L2 is a single index it works fine. Only accessing the matrix with A[L1, L2] also gives the right elements, it is just the assignment part that fails. Exchanging the 0 with an array of the correct length does not help.</p>
<p dir="auto">The faulty behaviour seems to be equivalent to A[min(L1):max(L1)+1, min(L2):max(L2)+1] = 0 but I'm not completely sure. The code below illustrates the bug:</p>
<p dir="auto">import numpy<br>
import scipy.sparse</p>
<p dir="auto">B = numpy.array([[1,2,3],[3,4,5],[6,7,8]])</p>
<p dir="auto">A = scipy.sparse.lil_matrix(B)</p>
<p dir="auto">print 'A:', A.todense()<br>
print 'B:', B</p>
<p dir="auto">print 'Some A-elements:', A[[0,1,2],[1,2,0]].todense()<br>
print 'Some B-elements:', B[[0,1,2],[1,2,0]]</p>
<p dir="auto">B[[0,1,2],[1,2,0]] = 0</p>
<p dir="auto">print 'New B:', B</p>
<p dir="auto">A[[0,1,2],[1,2,0]] = 0</p>
<p dir="auto">print 'New A:', A.todense()</p>
<p dir="auto">Output:</p>
<p dir="auto">A: [[1 2 3]<br>
[3 4 5]<br>
[6 7 8]]<br>
B: [[1 2 3]<br>
[3 4 5]<br>
[6 7 8]]<br>
Some A-elements: [[2 5 6]]<br>
Some B-elements: [2 5 6]<br>
New B: [[1 0 3]<br>
[3 4 0]<br>
[0 7 8]]<br>
New A: [[0 0 0]<br>
[0 0 0]<br>
[0 0 0]]</p>
<p dir="auto">When defining a list of constraints as an input for the scipy.optimize.minimize (using a default 'SLSQP' method), repeated or redundant constraints will raise messages like "Singular matrix C in LSQ subproblem", and a solution is not achieved, when in fact, these repeated/redundant constraints should just be ignored.</p>
<p dir="auto">This is very relevant when one has to define multiple inequality and equality constraints, which sometimes partially overlap for some of the constraints.</p>
<p dir="auto">To replicate the problem: Use a working example of a constrained minimization problem, but now duplicate the list of constraints (two times more constraints, but all of them repeated).</p>
<p dir="auto">Example where this is useful: Forcing a bivariate polynomial f(x,y) of a certain degree, to be f(0,y)=0 and forcing it's first derivative with respect to x, to be d f(x=1,y) / dx = 0, will provide a large set of equality constraints where a few of them are redundant.</p>