-
Notifications
You must be signed in to change notification settings - Fork 0
/
INV.m
executable file
·63 lines (56 loc) · 1.08 KB
/
INV.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/local/bin/octave
function A_inv = INV(A)
% INV computes the inverse of a triangle-like square matrix.
% IN:
% A - triangle square matrix
% OUT:
% A_inv - the inverse of A
% Usage:
% A_inv = INV(A);
% Notes:
% The inverse of a triangle square matrix equals a diagonal matrix,
% whose each diagonal entry equals the reciprocal of the entry in
% the same place of the matrix.
% check square
[m,n] = size(A);
if m ~= n
error('Invalid Input Matrix: Not Square.')
end
% init inv
A_inv = zeros(m,n);
% calc diagonal
for i=1:n
A_inv(i,i) = 1.0/A(i,i);
end
% check upper or lower
t = 0;
if norm(triu(A,1)) > eps
% upper
t = 1;
else
if norm(tril(A,-1)) > eps
% lower
A = A';
t = 2;
else
% diagonal
t = 3;
end
end
% calc other entries
if t ~= 3
for i=n-1:-1:1
for j=i+1:n
temp = 0;
for k=i+1:j
temp = temp - A_inv(k,j)*A(i,k);
end
A_inv(i,j) = temp/A_inv(i,i);
end
end
end
% check lower
if t == 2
A_inv = A_inv';
end
end