Determining a sparse matrix quotient
<p>I am looking to divide two sparce matricies in python 2.7, essentially <code>k = numerator / denominator</code>, with the result as a sparse matrix of type <code>sp.csr_matrix</code>. I am using <code>scipy as sp</code> and <code>numpy as np</code>. </p>

<p>To do this, I am following linear format of taking the dot product of numerator and inverse of denominator. Both items are of format <code>sp.csr_matrix(([],([],[])),shape=[R,R])</code>.</p>

<p>The calculation for k itself is </p>

<pre><code>k = sp.csr_matrix(numerator.dot(sp.linalg.inv(denominator)))
</code></pre>

<p>Doing this returns the warning:</p>

<pre><code>SparseEfficiencyWarning: splu requires CSC matrix format
warn('splu requires CSC matrix format', SparseEfficiencyWarning)
</code></pre>

<p><strong>What does the above warning mean</strong> in relation to determining the identity of <code>k</code> as the quotient between two sparse matrices? </p>

<p><strong>Is there a more efficient way</strong> to generate the dot product of a sparse matrix and a sparse matrix inverse in python (the quotient of two sparse matrices)?</p>

<p>I had previously found <a href="http://stackoverflow.com/questions/15118177/inverting-large-sparse-matrices-with-scipy">Inverting large sparse matrices with scipy</a>, however I wonder if this may be outdated.</p>

<p>Borrowing from the 2013 answer:</p>

<pre><code>In [409]: a=np.random.rand(3,3)
In [410]: A=sparse.csr_matrix(a)
In [411]: np.linalg.inv(a)
Out[411]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])
</code></pre>

<p>The <code>np</code> inv is not <code>sparse-aware</code>:</p>

<pre><code>In [412]: np.linalg.inv(A) 
 ....
LinAlgError: 0-dimensional array given. Array must be at least two-dimensional
</code></pre>

<p>With <code>from scipy.sparse import linalg</code>:</p>

<pre><code>In [414]: linalg.inv(A).A
/usr/lib/python3/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:243: SparseEfficiencyWarning: splu requires CSC matrix format
  warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/usr/lib/python3/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:161: SparseEfficiencyWarning: spsolve is more efficient when sparse b is in the CSC matrix format
  'is in the CSC matrix format', SparseEfficiencyWarning)
Out[414]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])
</code></pre>

<p>So use the <code>csc</code> format instead of <code>csr</code>:</p>

<pre><code>In [415]: A1=sparse.csc_matrix(a)
In [416]: linalg.inv(A1).A
Out[416]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])
</code></pre>

<p>Same thing, but without the sparsity warning.  Without getting into details, the <code>inv</code> must be using a method that iterates on columns rather than rows.  It does  <code>spsolve(A, I)</code>  (<code>I</code> is a sparse <code>eye</code> matrix).</p>

python performance numpy scipy