Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matrix transpose multiplication

Subject: matrix transpose multiplication

From: Klaus Krohne

Date: 15 Jan, 2002 08:29:27

Message: 1 of 9

Hello


This might be a rather basic problem:


How do I compute the product of a transposed matrix with a vector
without computing the transposed matrix itself?


To be more exact, I have a large nxk matrix V which is not sparse and
which fits into my memory once but not twice. Within my algorithm
(Block Arnoldi) I need x*V as well as V'*x, the latter of which
give's me an out of memory error.


There are two workarounds I can think of:
- Preallocating the memory for V' will at least give me the error at
the beginning of my routine.
- implementing the transpose multiplication in a nested loop will be
very slow.


I guess I am rather looking for a modified * operator ...


Does anybody have a hint???


Klaus

Subject: matrix transpose multiplication

From: Doug Schwarz

Date: 15 Jan, 2002 09:11:56

Message: 2 of 9

In article <eea9516.-1@WebX.raydaftYaTP>,
 "Klaus Krohne" <krohne@hrz1.hrz.tu-darmstadt.de> wrote:

>I need x*V as well as V'*x, the latter of which
>give's me an out of memory error.


Klaus,

V'*x is equal to (x'*V)' so as long as x is not large you should be ok.

--
Doug Schwarz
Eastman Kodak Company
douglas.schwarz@kodak.com

Subject: matrix transpose multiplication

From: lg@2-control.dk (Lars Gregersen)

Date: 15 Jan, 2002 19:29:52

Message: 3 of 9

On Tue, 15 Jan 2002 08:29:27 -0500, "Klaus Krohne"
<krohne@hrz1.hrz.tu-darmstadt.de> wrote:

>Hello
>
>
>This might be a rather basic problem:
>
>
>How do I compute the product of a transposed matrix with a vector
>without computing the transposed matrix itself?
>
>
>To be more exact, I have a large nxk matrix V which is not sparse and
>which fits into my memory once but not twice. Within my algorithm
>(Block Arnoldi) I need x*V as well as V'*x, the latter of which
>give's me an out of memory error.
[snip]

Try the following:

a=randn(500);
b=randn(500,1);

1)
tic,for i=1:100,c=a*b;end,toc

2)
tic,for i=1:100,c=a'*b;end,toc

3)
tic,for i=1:100,c=(b'*a)';end,toc

4)
% assuming that we can get b' easily
bb = b';
tic,for i=1:100,cc=bb*a;end,toc
% now we work with cc=c' instead of c...

>I guess I am rather looking for a modified * operator ...

If you need to do something faster than this you have to use a call to
BLAS directly. Perhaps Matlab will support this in a future release -
after all the code is already there.

  Lars

M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
2-control ApS
Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/

Subject: matrix transpose multiplication

From: Penny Anderson

Date: 15 Jan, 2002 14:38:47

Message: 4 of 9

In R13 (to be released this year), MATLAB will notice you writing

C = A' * B

and will call the appropriate form of the Level 3 BLAS routine DGEMM that does NOT transpose A first before multiplying it by B.
This looks something like:
alpha = 1.0; beta = 0.0;

dgemm('T','N',m,n,k,alpha,A,lda,B,ldb,beta,Cr,ldc);

where the important part is that first argument 'T' which denotes the transpose A.

Penny Anderson
The Math Team
The MathWorks, Inc.

"Lars Gregersen" <lg@2-control.dk> wrote in message news:3c44821a.22669807@news.cybercity.dk...
> On Tue, 15 Jan 2002 08:29:27 -0500, "Klaus Krohne"
> <krohne@hrz1.hrz.tu-darmstadt.de> wrote:
>
> >Hello
> >
> >
> >This might be a rather basic problem:
> >
> >
> >How do I compute the product of a transposed matrix with a vector
> >without computing the transposed matrix itself?
> >
> >
> >To be more exact, I have a large nxk matrix V which is not sparse and
> >which fits into my memory once but not twice. Within my algorithm
> >(Block Arnoldi) I need x*V as well as V'*x, the latter of which
> >give's me an out of memory error.
> [snip]
>
> Try the following:
>
> a=randn(500);
> b=randn(500,1);
>
> 1)
> tic,for i=1:100,c=a*b;end,toc
>
> 2)
> tic,for i=1:100,c=a'*b;end,toc
>
> 3)
> tic,for i=1:100,c=(b'*a)';end,toc
>
> 4)
> % assuming that we can get b' easily
> bb = b';
> tic,for i=1:100,cc=bb*a;end,toc
> % now we work with cc=c' instead of c...
>
> >I guess I am rather looking for a modified * operator ...
>
> If you need to do something faster than this you have to use a call to
> BLAS directly. Perhaps Matlab will support this in a future release -
> after all the code is already there.
>
> Lars
>
> M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
> 2-control ApS
> Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/
>

Subject: matrix transpose multiplication

From: Nabeel

Date: 15 Jan, 2002 14:56:04

Message: 5 of 9

Hi Klaus,

I don't have any new solutions to your specific quetion; what the others
have said so far covers my thoughts on this.

But I wanted to add that there's a nice little demo in MATLAB called
CHAINGUI that, when given an expression, calcualtes the most optimal
grouping of operations, optimal being determined by flops and
intermediate matrix size. It's not going to necessarily provide a
solution to your problem, but it's nice to play with.

Just type 'chaingui' at the MATLAB prompt to get started.

enjoy,

-- Nabeel




Klaus Krohne wrote:
>
> Hello
>
> This might be a rather basic problem:
>
> How do I compute the product of a transposed matrix with a vector
> without computing the transposed matrix itself?
>
> To be more exact, I have a large nxk matrix V which is not sparse and
> which fits into my memory once but not twice. Within my algorithm
> (Block Arnoldi) I need x*V as well as V'*x, the latter of which
> give's me an out of memory error.
>
> There are two workarounds I can think of:
> - Preallocating the memory for V' will at least give me the error at
> the beginning of my routine.
> - implementing the transpose multiplication in a nested loop will be
> very slow.
>
> I guess I am rather looking for a modified * operator ...
>
> Does anybody have a hint???
>
> Klaus

Subject: matrix transpose multiplication

From: lg@2-control.dk (Lars Gregersen)

Date: 16 Jan, 2002 07:34:26

Message: 6 of 9

On Tue, 15 Jan 2002 14:38:47 -0500, "Penny Anderson"
<penny@mathworks.com> wrote:

>In R13 (to be released this year), MATLAB will notice you writing
>
>C = A' * B
>
>and will call the appropriate form of the Level 3 BLAS routine DGEMM that does NOT transpose A first before multiplying it by B.
>This looks something like:
>alpha = 1.0; beta = 0.0;
>
>dgemm('T','N',m,n,k,alpha,A,lda,B,ldb,beta,Cr,ldc);

Great!

Will it recognise cases where beta~=0?

  Lars

M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
2-control ApS
Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/

Subject: matrix transpose multiplication

From: Penny Anderson

Date: 16 Jan, 2002 13:17:26

Message: 7 of 9

"Lars Gregersen" <lg@2-control.dk> wrote in message news:3c452cd3.2957913@news.cybercity.dk...
> On Tue, 15 Jan 2002 14:38:47 -0500, "Penny Anderson"
> <penny@mathworks.com> wrote:
>
> >In R13 (to be released this year), MATLAB will notice you writing
> >
> >C = A' * B
> >
> >and will call the appropriate form of the Level 3 BLAS routine DGEMM that does NOT transpose A first before multiplying it by B.
> >This looks something like:
> >alpha = 1.0; beta = 0.0;
> >
> >dgemm('T','N',m,n,k,alpha,A,lda,B,ldb,beta,Cr,ldc);
>
> Great!
>
> Will it recognise cases where beta~=0?
>
> Lars
>
> M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
> 2-control ApS
> Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/

Meaning

C = alpha * op(A) * op(B) + beta * C;

where op(X) is one of { X, X.', X' },

or even just

C = C + op(A)*op(B);

No firm plans at the moment. We are in general looking around at ways to exploit things of this nature, though.

Penny Anderson
The MathWorks, Inc.

Subject: matrix transpose multiplication

From: lg@2-control.dk (Lars Gregersen)

Date: 16 Jan, 2002 20:45:51

Message: 8 of 9

On Wed, 16 Jan 2002 13:17:26 -0500, "Penny Anderson"
<penny@mathworks.com> wrote:

>"Lars Gregersen" <lg@2-control.dk> wrote in message news:3c452cd3.2957913@news.cybercity.dk...
>> On Tue, 15 Jan 2002 14:38:47 -0500, "Penny Anderson"
>> <penny@mathworks.com> wrote:
>>
>> >In R13 (to be released this year), MATLAB will notice you writing
>> >
>> >C = A' * B
>> >
>> >and will call the appropriate form of the Level 3 BLAS routine DGEMM that does NOT transpose A first before multiplying it by B.
>> >This looks something like:
>> >alpha = 1.0; beta = 0.0;
>> >
>> >dgemm('T','N',m,n,k,alpha,A,lda,B,ldb,beta,Cr,ldc);
>>
>> Great!
>>
>> Will it recognise cases where beta~=0?
>>
>> Lars
>>
>> M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
>> 2-control ApS
>> Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/
>
>Meaning
>
>C = alpha * op(A) * op(B) + beta * C;
>
>where op(X) is one of { X, X.', X' },
>
>or even just
>
>C = C + op(A)*op(B);

Yes, it should of course recognise any variation of the theme ;-)

>No firm plans at the moment. We are in general looking around at ways to exploit things of this nature, though.

...and of course using C in place ;-)

I know it insn't easy to do this in an automated way. Many users would
probably be happy just to gain access to the BLAS calls directly.

  Lars

M.Sc. Chem Engng. Lars Gregersen <lg@2-control.com>
2-control ApS
Windows Toolbox for Matlab version 1.0: www.2-control.com/wt/

Subject: matrix transpose multiplication

From: Nabeel

Date: 16 Jan, 2002 16:36:47

Message: 9 of 9

Hi,

> I know it insn't easy to do this in an automated way. Many users would
> probably be happy just to gain access to the BLAS calls directly.

We describe how to do this via MEX files. If you search our site using
the keywords "blas and api", the returned link titled "Creating C
Language MEX-Files (External Interfaces/API)" should take you to that
page. Alternately, you can get there directly via:

  
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch03cr15.shtml

-- Nabeel

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us