-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_inversion.e
243 lines (222 loc) · 6.66 KB
/
matrix_inversion.e
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
note
description: "project3 application root class"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature {NONE} -- Initialization
make
-- Run application.
require
nothing: True
local
n,i,j: INTEGER
matrix : ARRAY2[REAL]
do
io.read_integer
n:=io.last_integer
create matrix.make_filled (0, n, 2*n)
--take input
from i:=1 until i>n loop
from j:=1 until j>2*n loop
if j<n+1 then
io.read_real
matrix.put (io.last_real, i, j)
else
if j-n=i then
matrix.put (1, i, j)
else
matrix.put (0,i,j)
end
end
j:=j+1
end
i:=i+1
end
--uncomment to see input
--print_matrix(matrix)
if (n=1) then
if (matrix.item (1,1)=0) then
print("INVALID%N")
else
print((1/matrix.item (1,1)).out+"%N")
end
else
--calculate_inverse
matrix:= find_inverse(matrix)
--see result
print_matrix(matrix)
end
ensure
nothing: True
rescue
print("INVALID%N")
end
exchange_rows(r1: INTEGER; r2: INTEGER; m: ARRAY2[REAL]) : ARRAY2[REAL]
require
r1>0 and r2>0 and r1<=m.height and r2<=m.height and m.height>0 and (m.height * m.height*2 = m.capacity)
local
i,n: INTEGER
tmp: REAL
do
n:=m.height
from i:=1 until i>2*n loop
tmp:=m.item(r1,i)
m.put(m.item(r2,i),r1,i)
m.put(tmp,r2,i)
i:=i+1
end
Result:=m
ensure
Result.height>0 and (Result.height * Result.height*2 = Result.capacity)
rescue
print("INVALID%N")
end
find_inverse(m: ARRAY2[REAL]): ARRAY2[REAL]
require
m.height>0 and (m.height * m.height*2 = m.capacity)
local
i, n, pivot_row ,k : INTEGER
pivot_elem:REAL
m1: ARRAY2[REAL]
do
n:=m.height
m1:=m
from i:=1 until i>n loop
pivot_elem:=m1.item (i,i)
if (pivot_elem/=0) then
m1:=subtract_row_from_all_others(i,i,m1)
else
--have to get non zero below this column
pivot_row:=i
from k:=i+1 until k>n loop
if m1.item (i, k)/=0 then
pivot_row:=k
k:=n+1
end
k:=k+1
end
if (pivot_row=i) then
--no pivot found
--print("INVALID%N")
m1:=make_full_zero(m1)
i:=n+1
else
m1:=exchange_rows(pivot_row,i,m1)
m1:=subtract_row_from_all_others(i,i,m1)
end
end
i:=i+1
end
Result:=m1
ensure
Result.height>0 and (Result.height * Result.height*2 = Result.capacity)
rescue
print("INVALID%N")
end
make_full_zero(m:ARRAY2[REAL]) : ARRAY2[REAL]
require
m.height>0 and (m.height * m.height*2 = m.capacity)
local
i,j,n: INTEGER
do
n:=m.height
from i:=1 until i>n loop
from j:=1 until j>2*n loop
m.put (0,i,j)
j:=j+1
end
i:=i+1
end
Result:=m
ensure
Result.height>0 and (Result.height * Result.height*2 = Result.capacity) and Result.filled_with (0)
rescue
print("INVALID%N")
end
subtract_row_from_all_others(r:INTEGER;c:INTEGER;m: ARRAY2[REAL]): ARRAY2[REAL]
require
r>0 and c>0 and r<=m.height and c<=m.height*2 and m.height>0 and (m.height * m.height*2 = m.capacity)
local
i,j,n:INTEGER
const:REAL
do
n:=m.height
--making all elements in this column zero except for the one in this row
from i:=1 until i>n loop
if (i/=r) then
const:=m.item (i,c)/m.item (r,c)
from j:=1 until j>2*n loop
--m[i][j]=m[i][j]-const*m[r][j]
m.put (m.item (i,j)-const*m.item (r,j), i,j)
j:=j+1
end
end
i:=i+1
end
--making the first non zero element of this row 1
const:=m.item (r,c)
from i:=1 until i>2*n loop
--m[r][i]=m[r][i]/const
m.put (m.item (r,i)/const, r,i)
i:=i+1
end
Result:=m
ensure
Result.height>0 and (Result.height * Result.height*2 = Result.capacity)
rescue
print("INVALID%N")
end
print_matrix(m: ARRAY2[REAL])
require
m.height>0 and (m.height * m.height*2 = m.capacity)
local
i,j,n,flag : INTEGER
r: REAL
do
n:=m.height
flag:=0
--checking if invalid
from i:=1 until i>n loop
from j:=1 until j>2*n loop
if m.item (i,j)/=0 then
flag:=1
j:=2*n+1
i:=n+1
end
j:=j+1
end
i:=i+1
end
from i:=1 until i>n loop
from j:=1 until j>2*n loop
if (m.item (i,j).is_nan) then
--print("YES")
flag:=0
j:=2*n+1
i:=n+1
end
j:=j+1
end
i:=i+1
end
if (flag/=0) then
from i:=1 until i>n loop
from j:=n+1 until j>2*n loop
print(m.item (i, j).out+ " ")
j:=j+1
end
print("%N")
i:=i+1
end
else
print("INVALID%N")
end
ensure
nothing: True
rescue
print("INVALID%N")
end
end