University College, DublinMathematical Physics DepartmentMaster of Computational Science Degree 2004-2005
NUMERICAL ALGORITHMS
Dr Derek O’Connor
Laboratory Exercise No. 6:
Complexity of Matrix Operations.
The standard mathematical definition of matrix multiplication,
C
=
AB
, where
A
is
m
×
n
,
B
is
n
×
p
, and
C
is
m
×
p
is as follows :
c
ij
=
n
k
=1
a
ik
×
b
kj
, i
= 1
,
2
,...,m, j
= 1
,
2
,...,p.
(1)Do the following1. Using this definition, derive an expression,
N
ops
(
m,n,p
), for the number of floating-point
additions
and
multiplications
needed to form the matrix product
C
=
AB
,2. Derive a similar expression
N
ops
(
m,n,p,q
) for
D
=
ABC
, where
A
is
m
×
n
,
B
is
n
×
p
, and
C
is
p
×
q
, and
D
is
m
×
q.
Although matrix multiplication is associative, i.e.,
A
(
BC
) = (
AB
)
C,
show that
N
ops
(
A
(
BC
))
=
N
ops
((
AB
)
C
)
,
in general
.
(2)That is,
N
ops
(
m,n,p,q
) depends on the order in which the product
ABC
is formed or
parenthesized
.3. Write a
Matlab
function
C = function MatMult(A,B)
that implements the definition above.Test and compare this function with
Matlab
’s
C = A*B
for random square matrices of size
n
= 250
,
500
,
1000
.
4. Find 3 sets of values
{
m,n,p,q
}
that demonstrate clearly that the inequality (2) above is truein general. Use both your function
MatMult
and
Matlab
’s
C = A*B
in this demonstration andcompare the results.5. Calculate the
mflops/sec
for each of the tests above. Remember to give the machine parameterswith these rates.
note:
When timing the operations above use
Matlab
’s
cputime
. Here is the
help
for this function :
CPUTIME returns the CPU time in seconds that has been usedby the MATLAB process since MATLAB started.For example:t=cputime; your_operation; cputime-treturns the cpu time used to run your_operation.The return value may overflow the internal representationand wrap around.See also ETIME, TIC, TOC, CLOCK
derek o’connor – October 10, 2008.
Add a Comment