From help-octave-request at bevo dot che dot wisc dot edu Sun Dec 5 15:16:41 1999 Subject: Re: Dot product (nx1).*(nxm) From: Joao Cardoso To: jwe at bevo dot che dot wisc dot edu, "'help-octave@bevo.che.wisc.edu'" Date: Sun, 05 Dec 1999 21:16:30 +0000 Joao Cardoso wrote: > Mike Miller wrote: > > > On Sat, 4 Dec 1999, Heber Farnsworth wrote: > > > > > p(:,ones(1,m)).*v > > > > > > where p is nx1 and v is nxm. What this does is to multiply v by a nxm > > > natrix which has p as every column. > > > > Apparently, Heber's method is faster in this example than the one I > > suggested: > > > > octave:1> p=rand(100,1); > > octave:2> q=ones(1,50); > > octave:3> p2=p*q; > > octave:4> t0=cputime; for i=1:1000, p2=p*q; end , cputime-t0 > > ans = 2.4600 > > octave:5> t0=cputime; for i=1:1000, p2=p(:,q); end , cputime-t0 > > ans = 2.0100 > > > > As I never really understood the line 5 syntax, I always use the line 4 > method -- I understand it. But my timings shows the reverse conclusion: > > octave:59> p=rand(100,1); > octave:60> q=ones(1,50); > octave:61> p2=p*q; > octave:62> t0=cputime; for i=1:1000, p2=p*q; end , cputime-t0 > ans = 0.27000 > octave:63> t0=cputime; for i=1:1000, p2=p(:,q); end , cputime-t0 > ans = 0.37000 > > for greater (5X) matrices, however: > > octave:72> p=rand(500,1); > octave:73> q=ones(1,250); > octave:74> p2=p*q; > octave:75> t0=cputime; for i=1:1000, p2=p*q; end , cputime-t0 > ans = 8.2200 > octave:76> t0=cputime; for i=1:1000, p2=p(:,q); end , cputime-t0 > ans = 7.5300 > > octave:77> 8.2200/0.27000 > ans = 30.444 > octave:78> 7.5300/0.37000 > ans = 20.351 > > with still greater (10X) matrices (what? isn't a gray sunday?) > > octave:80> p=rand(1000,1); > octave:81> q=ones(1,500); > octave:82> p2=p*q; > octave:83> t0=cputime; for i=1:1000, p2=p*q; end , cputime-t0 > ans = 35.580 > octave:84> t0=cputime; for i=1:1000, p2=p(:,q); end , cputime-t0 > ans = 30.150 > > octave:85> 35.580/0.27000 > ans = 131.78 > octave:86> 30.150/0.37000 > ans = 81.486 > > The first method scales as 1.4*N^2, obviously, and the last one seems to > scale as 3.7*N*log(N), so I must learn the second method syntax! > Contributions? I'm using Octave-2.1.14 on a 500MHz PIII. > > Joao Its sunday :-) On 14-Nov-1996, SANDS at vsdec dot nl dot nuwc dot navy dot mil wrote: [and John Eaton replied:] : B=kron(v,ones(1,m)).*A; Yes, kron will definitely be slow because it uses interpreted loops. : B=v(:,ones(1,m)) .* A; This will be fairly fast. In the test I ran, with v = rand (1000,1) and m = 10, it was nearly 600 times faster than kron. For some cases though, v * ones(1,m) .* A will actually be faster, at least in Octave. If the .* operator is overloaded to do the job of row and column <--------------- !!! scaling, then I would expect that v .* A will be faster than any of the other methods. It will also use less memory, which might turn out to be important if length(v) columns(A) are both large. It might even be easier to read, too. So, why am I writing this instead of implementing that? :-) jwe [end of quotation] John, have you changed your mind? Syntax consistency reasons? Joao > > > -- > Joao Cardoso | e-mail: jcardoso at inescn dot pt > INESC, R. Jose Falcao 110 | tel: + 351 2 2094322 > 4050 Porto, Portugal | fax: + 351 2 2008487 > > ----------------------------------------------------------------------- > Octave is freely available under the terms of the GNU GPL. > > Octave's home on the web: http://www.che.wisc.edu/octave/octave.html > How to fund new projects: http://www.che.wisc.edu/octave/funding.html > Subscription information: http://www.che.wisc.edu/octave/archive.html > ----------------------------------------------------------------------- -- Joao Cardoso | e-mail: jcardoso at inescn dot pt INESC, R. Jose Falcao 110 | tel: + 351 2 2094322 4050 Porto, Portugal | fax: + 351 2 2008487 ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------