# Dot product of two matrices

**URL:** <https://forum.kx.com/t/dot-product-of-two-matrices/10886>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 13, 2016, 4:59pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886 "2016-03-13T16:59:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.kx.com/u/Krishna1)\
**Post date:** [March 13, 2016, 4:59pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/1 "2016-03-13T16:59:00Z")

</div>

Hi,&nbsp;  
I have a matrix of 53072X194(53072 rows, 194 columns) and another vector, 194X1.

I’m using the following to get the dot product of the two :&nbsp;

a:53072 194 # 1

b: 194 1 # 1

(Just as examples, the above values)

dotProduct : sum each (a\*:b)

I get the correct value, but it takes a long time, is there a more efficient approach to do this?

Thanks,&nbsp;

Kumar

---

<div class="post-metadata">

**Author:** ![TerryLynch1](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@TerryLynch1](https://forum.kx.com/u/TerryLynch1)\
**Post date:** [March 13, 2016, 5:20pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/2 "2016-03-13T17:20:00Z")

</div>

$ performs dot product on floats:&nbsp;http://code.kx.com/wiki/Reference/DollarSign

a:53072 194 # 1f

b: 194 1 # 1f

q)\ts res1:sum each (a\*:b)

1569 386039648

q)\ts res2:a$b

78 1960624

q)res1~res2

1b

Terry

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [March 13, 2016, 5:26pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/3 "2016-03-13T17:26:00Z")

</div>

a:53072 194 # 1f\<o:p\>\</o:p\>

b: 194 1 # 1f\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

\t&nbsp; sum each (a\*:b)\<o:p\>\</o:p\>

2521j\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

\t a $ b\<o:p\>\</o:p\>

80j\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

\t a mmu b\<o:p\>\</o:p\>

80j\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

It would be good to see a comparison with R, Python or matlab.\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Kim\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

**Von:** [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com) [[mailto:personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)] **Im Auftrag von** Terry Lynch  
**Gesendet:** Sonntag, 13. März 2016 18:20  
**An:** Kdb+ Personal Developers  
**Betreff:** Re: [personal kdb+] Dot product of two matrices\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

$ performs dot product on floats:&nbsp;http://code.kx.com/wiki/Reference/DollarSign\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

a:53072 194 # 1f\<o:p\>\</o:p\>

b: 194 1 # 1f\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

q)\ts res1:sum each (a\*:b)\<o:p\>\</o:p\>

1569 386039648\<o:p\>\</o:p\>

q)\ts res2:a$b\<o:p\>\</o:p\>

78 1960624\<o:p\>\</o:p\>

q)res1~res2\<o:p\>\</o:p\>

1b\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Terry\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

On Sun, Mar 13, 2016 at 12:59 PM, Krishna Kumar \<kumar.ramanathan@gmail.com\> wrote:\<o:p\>\</o:p\>

Hi,&nbsp;\<o:p\>\</o:p\>

I have a matrix of 53072X194(53072 rows, 194 columns) and another vector, 194X1.\<o:p\>\</o:p\>

I’m using the following to get the dot product of the two :&nbsp;\<o:p\>\</o:p\>

a:53072 194 # 1\<o:p\>\</o:p\>

b: 194 1 # 1\<o:p\>\</o:p\>

(Just as examples, the above values)\<o:p\>\</o:p\>

dotProduct : sum each (a\*:b)\<o:p\>\</o:p\>

I get the correct value, but it takes a long time, is there a more efficient approach to do this?\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Thanks,&nbsp;\<o:p\>\</o:p\>

Kumar\<o:p\>\</o:p\>

–  
Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![Alexander\_Belop](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Alexander\_Belop](https://forum.kx.com/u/Alexander_Belop)\
**Post date:** [March 15, 2016, 8:06pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/4 "2016-03-15T20:06:00Z")

</div>

On Sunday, March 13, 2016 at 1:25:53 PM UTC-4, kuentang wrote:

> \t a mmu b
> 
> 80j
> 
> &nbsp;
> 
> It would be good to see a comparison with R, Python or matlab.

Python (numpy) is more than 10x faster:

In [21]: a = numpy.random.rand(53072,194)

In [22]: b = numpy.random.rand(194, 1)

In [23]: %timeit a.dot(b)

100 loops, best of 3: 3.81 ms per loop

Compared to

In [24]: %%q

&nbsp; &nbsp;…: a:53072 194 # 1f

&nbsp; &nbsp;…: b: 194 1 # 1f

&nbsp; &nbsp;…: \t a mmu b

&nbsp; &nbsp;…:

Out[24]: 52

In [25]: %timeit %q a mmu b

10 loops, best of 3: 52.3 ms per loop

Note that the large difference is likely to be due to memory layout. &nbsp;Numpy &nbsp;keeps matrices in contiguous memory while q keeps each row in a separate vector.

---

<div class="post-metadata">

**Author:** ![heydi1](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@heydi1](https://forum.kx.com/u/heydi1)\
**Post date:** [March 15, 2016, 8:52pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/5 "2016-03-15T20:52:00Z")

</div>

you will see the performance dramatically improved using rand number, e.g. under q

b:194?1f  
\t a mmu b is much faster than 194#1f.

Xi

---

<div class="post-metadata">

**Author:** ![heydi1](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@heydi1](https://forum.kx.com/u/heydi1)\
**Post date:** [March 15, 2016, 9:17pm UTC](https://forum.kx.com/t/dot-product-of-two-matrices/10886/6 "2016-03-15T21:17:00Z")

</div>

sorry the previous reply does not make any sense :)

But you could still try multi thread,&nbsp;

e.g.&nbsp;

\s

4

mmu[;b]peach a

Xi
