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 Multiplication

Subject: Matrix Multiplication

From: Vineet

Date: 11 May, 2011 21:38:04

Message: 1 of 10

Is there a way in Matlab to specify that the product (matrix multiplication) of two matrices would be symmetric so that matrix multiplication happens faster. For ex:
A*A'
or
matrix multiplication of two symmetric matrices

I know there are routines to do it in BLAS :
like: SSYMM, DSYMM, CHEMM, CSYMM, ZHEMM, ZSYMM (to multiply two symmetric matrix)
or
SSYRK, DSYRK, CHERK, CSYRK, ZHERK, ZSYRK (to multiply matrix by its adjoint)

Thanks
Vineet

Subject: Matrix Multiplication

From: Roger Stafford

Date: 11 May, 2011 21:50:27

Message: 2 of 10

"Vineet " <yadavvineet@gmail.com> wrote in message <iqevjs$cb2$1@newscl01ah.mathworks.com>...
> Is there a way in Matlab to specify that the product (matrix multiplication) of two matrices would be symmetric so that matrix multiplication happens faster. For ex:
> A*A'
> or
> matrix multiplication of two symmetric matrices
>
> I know there are routines to do it in BLAS :
> like: SSYMM, DSYMM, CHEMM, CSYMM, ZHEMM, ZSYMM (to multiply two symmetric matrix)
> or
> SSYRK, DSYRK, CHERK, CSYRK, ZHERK, ZSYRK (to multiply matrix by its adjoint)
>
> Thanks
> Vineet
- - - - - - - - -
  The product of two symmetric (or Hermitian) matrices is not necessarily symmetric (or Hermitian.)

Roger Stafford

Subject: Matrix Multiplication

From: Roger Stafford

Date: 11 May, 2011 22:14:04

Message: 3 of 10

"Roger Stafford" wrote in message <iqf0b3$e4g$1@newscl01ah.mathworks.com>...
> - - - - - - - - -
> The product of two symmetric (or Hermitian) matrices is not necessarily symmetric (or Hermitian.)
>
> Roger Stafford
- - - - - - - -
  Of course if the two symmetric (Hermitian) matrices also commute, then their product will be symmetric (Hermitian). Is something like that what you had in mind?

Roger Stafford

Subject: Matrix Multiplication

From: Vineet

Date: 11 May, 2011 22:27:02

Message: 4 of 10

"Roger Stafford" wrote in message <iqf1nc$hkb$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iqf0b3$e4g$1@newscl01ah.mathworks.com>...
> > - - - - - - - - -
> > The product of two symmetric (or Hermitian) matrices is not necessarily symmetric (or Hermitian.)
> >
> > Roger Stafford
> - - - - - - - -
> Of course if the two symmetric (Hermitian) matrices also commute, then their product will be symmetric (Hermitian). Is something like that what you had in mind?
>
> Roger Stafford
Yes, I have to perform lot of matrix multiplication where I know that the matrix multiplication would result in symmetric matrix and I think there should be a way to tell this to matlab so that computation time for matrix multiplication reduces substantially.
vineet

Subject: Matrix Multiplication

From: James Tursa

Date: 11 May, 2011 22:42:04

Message: 5 of 10

"Vineet " <yadavvineet@gmail.com> wrote in message <iqevjs$cb2$1@newscl01ah.mathworks.com>...
> Is there a way in Matlab to specify that the product (matrix multiplication) of two matrices would be symmetric so that matrix multiplication happens faster. For ex:
> A*A'
> or
> matrix multiplication of two symmetric matrices
>
> I know there are routines to do it in BLAS :
> like: SSYMM, DSYMM, CHEMM, CSYMM, ZHEMM, ZSYMM (to multiply two symmetric matrix)
> or
> SSYRK, DSYRK, CHERK, CSYRK, ZHERK, ZSYRK (to multiply matrix by its adjoint)
>
> Thanks
> Vineet

MATLAB does this automatically if you do the matrix multiply in a form that it recognizes. e.g., in your A*A' above the dgemm routine will not be called. Rather, one of the BLAS symmetric routines will be called and then the other triangular part is filled in with just a copy of the traingular part that was calculated in the BLAS routine.

James Tursa

Subject: Matrix Multiplication

From: Roger Stafford

Date: 11 May, 2011 22:44:04

Message: 6 of 10

"Vineet " <yadavvineet@gmail.com> wrote in message <iqf2fm$jid
> Yes, I have to perform lot of matrix multiplication where I know that the matrix multiplication would result in symmetric matrix and I think there should be a way to tell this to matlab so that computation time for matrix multiplication reduces substantially.
> vineet
- - - - - - - - -
  Can you please tell us how you know in advance that your matrix product will turn out to be symmetric? Knowing this might influence the kinds of suggestions you receive from others.

Roger Stafford

Subject: Matrix Multiplication

From: Vineet

Date: 12 May, 2011 09:42:04

Message: 7 of 10

"Roger Stafford" wrote in message <iqf3fk$lvv$1@newscl01ah.mathworks.com>...
> "Vineet " <yadavvineet@gmail.com> wrote in message <iqf2fm$jid
> > Yes, I have to perform lot of matrix multiplication where I know that the matrix multiplication would result in symmetric matrix and I think there should be a way to tell this to matlab so that computation time for matrix multiplication reduces substantially.
> > vineet
> - - - - - - - - -
> Can you please tell us how you know in advance that your matrix product will turn out to be symmetric? Knowing this might influence the kinds of suggestions you receive from others.
>
> Roger Stafford
Hi
Roger
 Many thanks for your quick reply

 Anyhow I want to multiply H*Q*H' where Q is positive definite and H is any arbitrary rectangular matrix. These type of matrix multiplication arise quite a lot in statistics. I know before hand that output of HQHt would be a symmetric matrix hence when I multiply HQ= H*Q; B=HQ *H'; I want to tell matlab that B is a symmetric matrix


 In my real application H is sparse (~8% full) and huge i.e. ~50000*2000000 (i cannot hold H in memory so I store it in vertical stripts on harddrive)
 and Q is blkdiag with each block being 2635*2635 so currently I am multiplying these matrices by the second method shown in the example code given below

 clc
 clear all
 n = 10;
 p = 6;
 Q = rand(n/2);
 Q = Q'*Q;% create a Positive definite matrix
 H = rand(p,n);
 Q1 = blkdiag(Q,Q);
 % Do HQHT directly
 tic
 HQHT=H*Q1*H';
 toc
 tic
 % Do HQHT indirectly
 Htemp = H(:,1:5)*Q*H(:,1:5)';
 Htemp2 = Htemp+(H(:,6:10)*Q*H(:,6:10)');
 toc

 Many thanks once again for your help
  Vineet

Subject: Matrix Multiplication

From: James Tursa

Date: 12 May, 2011 23:53:04

Message: 8 of 10

"Vineet " <yadavvineet@gmail.com> wrote in message <iqga1c$p95$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iqf3fk$lvv$1@newscl01ah.mathworks.com>...
> > "Vineet " <yadavvineet@gmail.com> wrote in message <iqf2fm$jid
> > > Yes, I have to perform lot of matrix multiplication where I know that the matrix multiplication would result in symmetric matrix and I think there should be a way to tell this to matlab so that computation time for matrix multiplication reduces substantially.
> > > vineet
> > - - - - - - - - -
> > Can you please tell us how you know in advance that your matrix product will turn out to be symmetric? Knowing this might influence the kinds of suggestions you receive from others.
> >
> > Roger Stafford
> Hi
> Roger
> Many thanks for your quick reply
>
> Anyhow I want to multiply H*Q*H' where Q is positive definite and H is any arbitrary rectangular matrix. These type of matrix multiplication arise quite a lot in statistics. I know before hand that output of HQHt would be a symmetric matrix hence when I multiply HQ= H*Q; B=HQ *H'; I want to tell matlab that B is a symmetric matrix
>
>
> In my real application H is sparse (~8% full) and huge i.e. ~50000*2000000 (i cannot hold H in memory so I store it in vertical stripts on harddrive)
> and Q is blkdiag with each block being 2635*2635 so currently I am multiplying these matrices by the second method shown in the example code given below
>
> clc
> clear all
> n = 10;
> p = 6;
> Q = rand(n/2);
> Q = Q'*Q;% create a Positive definite matrix
> H = rand(p,n);
> Q1 = blkdiag(Q,Q);
> % Do HQHT directly
> tic
> HQHT=H*Q1*H';
> toc
> tic
> % Do HQHT indirectly
> Htemp = H(:,1:5)*Q*H(:,1:5)';
> Htemp2 = Htemp+(H(:,6:10)*Q*H(:,6:10)');
> toc
>
> Many thanks once again for your help
> Vineet

I don't see any obvious specialized symmetric BLAS routines that are going to directly help with this calculation. How is Q formed? e.g., is it formed as a A*A' type of operation and do you have the pieces available?

James Tursa

Subject: Matrix Multiplication

From: Vineet

Date: 13 May, 2011 01:54:05

Message: 9 of 10

"James Tursa" wrote in message <iqhrt0$l9f$1@newscl01ah.mathworks.com>...
> "Vineet " <yadavvineet@gmail.com> wrote in message <iqga1c$p95$1@newscl01ah.mathworks.com>...
> > "Roger Stafford" wrote in message <iqf3fk$lvv$1@newscl01ah.mathworks.com>...
> > > "Vineet " <yadavvineet@gmail.com> wrote in message <iqf2fm$jid
> > > > Yes, I have to perform lot of matrix multiplication where I know that the matrix multiplication would result in symmetric matrix and I think there should be a way to tell this to matlab so that computation time for matrix multiplication reduces substantially.
> > > > vineet
> > > - - - - - - - - -
> > > Can you please tell us how you know in advance that your matrix product will turn out to be symmetric? Knowing this might influence the kinds of suggestions you receive from others.
> > >
> > > Roger Stafford
> > Hi
> > Roger
> > Many thanks for your quick reply
> >
> > Anyhow I want to multiply H*Q*H' where Q is positive definite and H is any arbitrary rectangular matrix. These type of matrix multiplication arise quite a lot in statistics. I know before hand that output of HQHt would be a symmetric matrix hence when I multiply HQ= H*Q; B=HQ *H'; I want to tell matlab that B is a symmetric matrix
> >
> >
> > In my real application H is sparse (~8% full) and huge i.e. ~50000*2000000 (i cannot hold H in memory so I store it in vertical stripts on harddrive)
> > and Q is blkdiag with each block being 2635*2635 so currently I am multiplying these matrices by the second method shown in the example code given below
> >
> > clc
> > clear all
> > n = 10;
> > p = 6;
> > Q = rand(n/2);
> > Q = Q'*Q;% create a Positive definite matrix
> > H = rand(p,n);
> > Q1 = blkdiag(Q,Q);
> > % Do HQHT directly
> > tic
> > HQHT=H*Q1*H';
> > toc
> > tic
> > % Do HQHT indirectly
> > Htemp = H(:,1:5)*Q*H(:,1:5)';
> > Htemp2 = Htemp+(H(:,6:10)*Q*H(:,6:10)');
> > toc
> >
> > Many thanks once again for your help
> > Vineet
>
> I don't see any obvious specialized symmetric BLAS routines that are going to directly help with this calculation. How is Q formed? e.g., is it formed as a A*A' type of operation and do you have the pieces available?
>
> James Tursa
Hi
James
Q is actually a positive definite covariance matrix formed by using a covariance function
if you want I can send you a detailed code to construct one . It is actually used in inverse modeling in the current application
vineet

Subject: Matrix Multiplication

From: Elliot

Date: 27 Sep, 2011 09:29:14

Message: 10 of 10

I know this post is old but thought id add anyway,

Surely you can just use the 'sparse' command for multiplying large sparse matrices. The command only stores non-zero values and can be used in matrix multiplication.

e.g. if A is 100x100 identity matrix, sparse(A) stores the diagonal of ones plus their co-ordinates.

Would that help?

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