# [SciPy-user] howto dot(a, b.T), some a or b coords are zeros

Nathan Bell wnbell@gmail....
Wed Jan 2 14:47:36 CST 2008

```On Jan 2, 2008 1:28 PM, dmitrey <dmitrey.kroshko@scipy.org> wrote:
> hi all,
> I have 2 vectors a and b of shape (n, 1) (n can be 1...10^3, 10^4, may
> be more); some coords of a or b usually are zeros (or both a and b, but
> b is more often); getting matrix c = dot(a, b.T) is required (c.shape =
> (n,n))
>
> What's the best way to speedup calculations (w/o using scipy, only numpy)?
> (I intend to use the feature to provide a minor enhancement for NLP/NSP
> ralg solver).

How much more expensive is dot(a,b.T) than zeros((n,n))?  Is outer()
any faster?  What proportion of a and b are zero?

You could remove all zeros from a and b, compute that outer product,
and then paste the results back into an n by n matrix.  I doubt this
would be any faster though since the outerproduct doesn't do many
FLOPs.

I know you don't want to use scipy, but time the following:

from scipy.sparse import *
asp = csr_matrix(a)
bsp = csr_matrix(b.T)

c = asp * bsp # time this

--
Nathan Bell wnbell@gmail.com
```