<h3 dir="auto">Describe your issue.</h3>
<p dir="auto">I've noticed an inconsistency in the behavior of <code class="notranslate">scipy.sparse</code> objects when using the built-in python operators ("<em>",` "@", "/") to multiply or divide by a vector. The "</em>" operator produces a dot product, as opposed to the expected element-wise multiplication (lines 7-9). The "/" operator, on the other hand, performs the expected element-wise operation with broadcasting (lines 11-12), which may be more confusing since it is not consistent with the "*" operator.</p>
<p dir="auto">I have not tested this behavior across all <code class="notranslate">scipy.sparse</code> matrix types, just the <code class="notranslate">diags</code> used below. I'm happy to submit a PR if this is just a feature that is not yet implemented.</p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[1]&gt;&gt;&gt; import numpy as np
[2]&gt;&gt;&gt; from scipy.sparse import diags
[3]&gt;&gt;&gt; D = diags([1, 1], [-1, 0], shape=(6, 5))
[4]&gt;&gt;&gt; D.toarray()
[4]===
array([[1., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0.],
       [0., 1., 1., 0., 0.],
       [0., 0., 1., 1., 0.],
       [0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 1.]])
[5]&gt;&gt;&gt; x = 1 + np.arange(D.shape[1])
[6]&gt;&gt;&gt; x
[6]=== array([1, 2, 3, 4, 5])
[7]&gt;&gt;&gt; D * x
[7]=== array([1., 3., 5., 7., 9., 5.])
[8]&gt;&gt;&gt; D @ x
[8]=== array([1., 3., 5., 7., 9., 5.])
[9]&gt;&gt;&gt; D.toarray() * x
[9]===
array([[1., 0., 0., 0., 0.],
       [1., 2., 0., 0., 0.],
       [0., 2., 3., 0., 0.],
       [0., 0., 3., 4., 0.],
       [0., 0., 0., 4., 5.],
       [0., 0., 0., 0., 5.]])
[10]&gt;&gt;&gt; D.toarray() @ x
[10]=== array([1., 3., 5., 7., 9., 5.])
[11]&gt;&gt;&gt; D / x
[11]===
matrix([[1.    , 0.    , 0.    , 0.    , 0.    ],
        [1.    , 0.5   , 0.    , 0.    , 0.    ],
        [0.    , 0.5   , 0.3333, 0.    , 0.    ],
        [0.    , 0.    , 0.3333, 0.25  , 0.    ],
        [0.    , 0.    , 0.    , 0.25  , 0.2   ],
        [0.    , 0.    , 0.    , 0.    , 0.2   ]])
[12]&gt;&gt;&gt; D.toarray() / x
[12]===
array([[1.    , 0.    , 0.    , 0.    , 0.    ],
       [1.    , 0.5   , 0.    , 0.    , 0.    ],
       [0.    , 0.5   , 0.3333, 0.    , 0.    ],
       [0.    , 0.    , 0.3333, 0.25  , 0.    ],
       [0.    , 0.    , 0.    , 0.25  , 0.2   ],
       [0.    , 0.    , 0.    , 0.    , 0.2   ]])"><pre class="notranslate">[<span class="pl-c1">1</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
[<span class="pl-c1">2</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-s1"></span><span class="pl-c1">&gt;</span> <span class="pl-s1">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">sparse</span> <span class="pl-s1">import</span> <span class="pl-s1">diags</span>
[<span class="pl-c1">3</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span> <span class="pl-c1">=</span> <span class="pl-en">diags</span>([<span class="pl-c1">1</span>, <span class="pl-c1">1</span>], [<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">0</span>], <span class="pl-s1">shape</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>, <span class="pl-c1">5</span>))
[<span class="pl-c1">4</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span>.<span class="pl-en">toarray</span>()
[<span class="pl-c1">4</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span>
<span class="pl-en">array</span>([[<span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>]])
[<span class="pl-c1">5</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-v">D</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>])
[<span class="pl-c1">6</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span>
[<span class="pl-c1">6</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span> <span class="pl-en">array</span>([<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">4</span>, <span class="pl-c1">5</span>])
[<span class="pl-c1">7</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span> <span class="pl-c1">*</span> <span class="pl-s1">x</span>
[<span class="pl-c1">7</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span> <span class="pl-en">array</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">3.</span>, <span class="pl-c1">5.</span>, <span class="pl-c1">7.</span>, <span class="pl-c1">9.</span>, <span class="pl-c1">5.</span>])
[<span class="pl-c1">8</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span> @ <span class="pl-s1">x</span>
[<span class="pl-c1">8</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span> <span class="pl-en">array</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">3.</span>, <span class="pl-c1">5.</span>, <span class="pl-c1">7.</span>, <span class="pl-c1">9.</span>, <span class="pl-c1">5.</span>])
[<span class="pl-c1">9</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span>.<span class="pl-en">toarray</span>() <span class="pl-c1">*</span> <span class="pl-s1">x</span>
[<span class="pl-c1">9</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span>
<span class="pl-en">array</span>([[<span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">1.</span>, <span class="pl-c1">2.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">2.</span>, <span class="pl-c1">3.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">3.</span>, <span class="pl-c1">4.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">4.</span>, <span class="pl-c1">5.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">5.</span>]])
[<span class="pl-c1">10</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span>.<span class="pl-en">toarray</span>() @ <span class="pl-s1">x</span>
[<span class="pl-c1">10</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span> <span class="pl-en">array</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">3.</span>, <span class="pl-c1">5.</span>, <span class="pl-c1">7.</span>, <span class="pl-c1">9.</span>, <span class="pl-c1">5.</span>])
[<span class="pl-c1">11</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span> <span class="pl-c1">/</span> <span class="pl-s1">x</span>
[<span class="pl-c1">11</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span>
<span class="pl-en">matrix</span>([[<span class="pl-c1">1.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
        [<span class="pl-c1">1.</span>    , <span class="pl-c1">0.5</span>   , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
        [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.5</span>   , <span class="pl-c1">0.3333</span>, <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
        [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.3333</span>, <span class="pl-c1">0.25</span>  , <span class="pl-c1">0.</span>    ],
        [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.25</span>  , <span class="pl-c1">0.2</span>   ],
        [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.2</span>   ]])
[<span class="pl-c1">12</span>]<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-v">D</span>.<span class="pl-en">toarray</span>() <span class="pl-c1">/</span> <span class="pl-s1">x</span>
[<span class="pl-c1">12</span>]<span class="pl-c1">==</span><span class="pl-c1">=</span>
<span class="pl-en">array</span>([[<span class="pl-c1">1.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
       [<span class="pl-c1">1.</span>    , <span class="pl-c1">0.5</span>   , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
       [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.5</span>   , <span class="pl-c1">0.3333</span>, <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    ],
       [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.3333</span>, <span class="pl-c1">0.25</span>  , <span class="pl-c1">0.</span>    ],
       [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.25</span>  , <span class="pl-c1">0.2</span>   ],
       [<span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.</span>    , <span class="pl-c1">0.2</span>   ]])</pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="No error message."><pre class="notranslate">No error message.</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">[1]&gt;&gt;&gt; import sys, scipy, numpy; print(scipy.<strong>version</strong>, numpy.<strong>version</strong>, sys.version_info) 1.7.1 1.20.3 sys.version_info(major=3, minor=9, micro=6, releaselevel='final', serial=0)</p>
<p dir="auto">While sitting with <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/stefanv/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/stefanv">@stefanv</a> last week we discussed (and then <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/stefanv/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/stefanv">@stefanv</a> started implementing) sparse array classes that use the same code as the existing matrix classes, but instead satisfy the ndarray interface (only up to two dimensions).  For example slicing out rows would result in 1d arrays rather than 2d matrices, the <code class="notranslate">*</code> operator means elementwise multiply rather than matrix multiply, etc..</p>
<p dir="auto">This would be valuable for a number of communities and, in particular, anyone trying to use sparse arrays and numpy arrays interchangably, as happens in Pandas, Scikit-Learn, and Dask.array.</p>
<p dir="auto">I thought I'd raise this issue to track this work (and also to subtly prod <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/stefanv/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/stefanv">@stefanv</a>).</p>
<p dir="auto">Also cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/NelleV/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/NelleV">@NelleV</a> and <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/njsmith/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/njsmith">@njsmith</a></p>