MATRIX INVERSION TAKING FOREVER TO RUN

조회 수: 36 (최근 30일)
YOGENDRA SINGH BHANDARI
YOGENDRA SINGH BHANDARI 2020년 8월 5일
댓글: Walter Roberson 2023년 11월 7일
I have a matrix(size 200*200) which I want to keep multiplying with 10 lakhs different input vectors. Furthermore, some elements in the matrix are random and to capture their statistical behaviour, 300 monte carlo sims need to be done for each of the input vector. Now this is taking a lot of time for even 2^5 codes. If extrapolated, it will take forever for the original case. I have profiled the code, and ofcourse the inversion is most demanding. I tried generating Mex file, but it doesn't help. Though increasing the number of workers in the parallel tool box helps, but still not significantly.
Is there any intelligent way of doing this? Thanks in advance.
  댓글 수: 3
YOGENDRA SINGH BHANDARI
YOGENDRA SINGH BHANDARI 2020년 8월 5일
Sure, just that generation of A and B in my case is slightly different.
Bruno Luong
Bruno Luong 2020년 8월 5일
I simply don't believe.
For random matrix such as mine, MATLAB "\" use direct method must use the most generic factorization method, and it's not profit any advantage more than your matrix, whatever it is.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2020년 8월 5일
In general, you should not invert matrices. I suspect you're trying to work symbolically in which case you absolutely should not try to invert symbolic matrices, especially ones that large.
maxN = 7;
lengthOfExpression = zeros(1, maxN);
timeToCompute = zeros(1, maxN);
for n = 1:maxN
syms('x', [n n]);
tic
A = inv(x);
timeToCompute(n) = toc;
lengthOfExpression(n) = strlength(char(A));
end
lengthOfExpression
You can see that the length of the expression representing the inverse grows pretty quickly. The time required to compute the inverse starts off small but at the last data point it rises sharply. You can try extending the problem size to maxN = 8 if you have the desire and the time. Extrapolation can be tricky, especially when trying to extrapolate the behavior of n = 200 from data with n = 7, but even looking at the general trend shows that storing and computing your inverse may run up against the storage capacity of the universe and its lifespan.
If you're solving a system of equations, use the backslash operator (\) instead and/or substitute values in for your symbolic variables before trying to solve the system if possible.
  댓글 수: 4
Aurea94
Aurea94 2023년 11월 7일
If my matrix is symbolic and I cannot substitute the variables before solving. Is there a way in which I can spped the calculus of the inverse?
Walter Roberson
Walter Roberson 2023년 11월 7일
Let me show an example:
N = 6
N = 6
syms A B C D
M = sym(magic(N));
M(1,1) = A^2+3*C; M(2,3) = B*tan(D-5); M(3, 5) = exp(-C); M(4,2) = D*(D-1)*(D-2);
temp = solve(C^3+B*C^2-2*C+5, C, 'maxdegree', 3);
M(5,6) = temp(2);
M
M = 
rank(M)
ans = 6
tic; Minv = inv(M); toc
Elapsed time is 11.597662 seconds.
syms prox [N N]
tic; proxinv = inv(prox); proxinv = subs(proxinv, prox, M); toc
Elapsed time is 2.803145 seconds.
So when the matrix is complicated enough then you can get a fair bit ahead by taking inv() of a representative matrix the same size and substituting in the corresponding coefficients of the original matrix afterwards. In this case we take the inverse of a generic 6 x 6 matrix of just symbolic variable names, which gives us the general form for inverses of 6 x 6 matrix; then we substitute in the specific terms.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by