Dot product of two matrices

Hi, 
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 : 

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, 

Kumar

$ performs dot product on floats: 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

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

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

<o:p> </o:p>

\t  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] 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: 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, <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 : <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, <o:p></o:p>

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


Submitted via Google Groups

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

\t a mmu b

80j

 

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

   …: a:53072 194 # 1f

   …: b: 194 1 # 1f

   …: \t a mmu b

   …:

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.  Numpy  keeps matrices in contiguous memory while q keeps each row in a separate vector.

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

sorry the previous reply does not make any sense :)

But you could still try multi thread, 

e.g. 

\s

4

mmu[;b]peach a

Xi