Code covered by the BSD License  

Highlights from
mpower2 - A faster matrix power function.

3.5

3.5 | 2 ratings Rate this file 3 Downloads (last 30 days) File Size: 3.27 KB File ID: #25782
image thumbnail

mpower2 - A faster matrix power function.

by James Tursa

 

09 Nov 2009 (Updated 11 Nov 2009)

mpower2 evaluates matrix to an integer power faster than the MATLAB built-in function mpower.

| Watch this File

File Information
Description

mpower2 evaluates matrix to an integer power faster than the MATLAB built-in function mpower. The speed improvement apparently comes from the fact that mpower does an unnecessary matrix multiply as part of the algorithm startup, whereas mpower2 only does necessary matrix multiplies. e.g.,

  >> A = rand(2000);
  >> tic;A^1;toc
     Elapsed time is 6.047194 seconds.
  >> tic;mpower2(A,1);toc
     Elapsed time is 0.001882 seconds.
  >> tic;A^16;toc
    Elapsed time is 29.840877 seconds.
  >> tic;mpower2(A,16);toc
    Elapsed time is 23.714932 seconds.

For sparse matrices, mpower does not do the unnecessary matrix multiply. However, in this case mpower2 is apparently more memory efficient. e.g.,

  >> A = sprand(5000,5000,.01);
  >> tic;A^1;toc
     Elapsed time is 0.038530 seconds.
  >> tic;mpower2(A,1);toc
     Elapsed time is 0.036335 seconds.
  >> tic;A^2;toc
     Elapsed time is 3.248792 seconds.
  >> tic;mpower2(A,2);toc
     Elapsed time is 2.160705 seconds.
  >> tic;A^3;toc
     Elapsed time is 10.005085 seconds.
  >> tic;mpower2(A,3);toc
     Elapsed time is 10.020719 seconds.
  >> tic;A^4;toc
     ??? Error using ==> mpower
     Out of memory. Type HELP MEMORY for your options.
  >> tic;mpower2(A,4);toc
     Elapsed time is 133.682037 seconds.

Y = mpower2(X,P), is X to the power P for integer P. The power is computed by repeated squaring. If the integer is negative, X is inverted first. X must be a square matrix.

Class support for inputs X, P:
      float: double, single

Caution: Since mpower2 does not do the unnecessary startup matrix multiply that mpower does, the end result may not match mpower exactly. But the answer will be just as accurate.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
12 Nov 2009 Gianni Schena

super... but only with square matrices as far as I understant
can it work also with vectors ?
regards, gianni

12 Nov 2009 James Tursa

Gianni: Raising to a power with matrix multiplies is not mathematically defined for non-square matrices. Hence mpower2 (and MATLAB's own mpower) do not support this operation. For vectors, an operation that *is* supported in MATLAB is the element-wise power using the .^ operator.

19 Mar 2011 GAGAN  
20 Mar 2011 James Tursa

@GAGAN: Is there a particular reason why you gave this submission such a low rating?

11 Apr 2011 Sean de

@James: revenge for your informative feedback on his file.

06 Dec 2011 Jan Simon

Thanks.

Please login to add a comment or rating.
Updates
11 Nov 2009

Modified the code so that sparse matrix inputs will always result in a sparse matrix answer. Also added a few more special cases that are more efficient than the binary decomposition method.

Tag Activity for this File
Tag Applied By Date/Time
mpower James Tursa 09 Nov 2009 10:34:23
matrix James Tursa 09 Nov 2009 10:34:23
power James Tursa 09 Nov 2009 10:34:23

Contact us at files@mathworks.com