2012-04-03 36 views

回答

1
restart: 

mmm:=proc(a::Matrix,b::Matrix) 
local c, i, j, k, m, n, p; 
(m,n,p):=op([1,1],a), op([1,2],b), op([1,2],a); 
if op([1,1],b) <> p then error "incompatible dimensions"; end if; 
c:=Matrix(m,n); 
for i from 1 to m do 
    for j from 1 to n do 
     c[i,j] := add(a[i,k]*b[k,j],k=1..p); 
    end do; 
end do: 
c; 
end proc: 

a:=LinearAlgebra:-RandomMatrix(2,3): 
b:=LinearAlgebra:-RandomMatrix(3,5): 

mmm(a,b); 
a.b; # check 

mmm(a,a^%T); 
a.a^%T; # check 

mmm(b,a); # test for dimension mismatch 
b.a; # test for dimension mismatch 

a:=LinearAlgebra:-RandomMatrix(33,33): 
b:=LinearAlgebra:-RandomMatrix(33,33): 
LinearAlgebra:-Norm(a.b - mmm(a,b));