-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMatrix.lua
374 lines (342 loc) · 7.38 KB
/
Matrix.lua
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
Matrix = {
M = {}
}
Matrix.__index = Matrix
function Matrix:new(m, n, x)
local o = setmetatable({}, self)
if (m == nil or type(m) == "number") then
m = m or 3
n = n or 3
x = x or 0
if m <= 12 then
o.M = fysiks.createTable[m]()
else
o.M = {}
end
if false and n <= 12 then
for i = 1, m do
o.M[i] = fysiks.createTable[n]()
for j = 1, n do
o.M[i][j] = x
end
end
else
for i = 1, m do
o.M[i] = {}
for j = 1, n do
o.M[i][j] = x
end
end
end
elseif m.M ~= nil then
o.M = {}
for i = 1, #m.M do
table.insert(o.M, {})
for j = 1, #m.M[1] do
table.insert(o.M[i], m.M[i][j])
end
end
else
o.M = m
end
return o
end
function Matrix:newCheap(m)
local o = setmetatable({}, self)
o.M = m
return o
end
function Matrix:rotationX(angle)
return Matrix:newCheap({
{1, 0, 0},
{0, math.cos(angle), math.sin(angle)},
{0, - math.sin(angle), math.cos(angle)}
})
end
function Matrix:rotationY(angle)
return Matrix:newCheap({
{ math.cos(angle), 0, - math.sin(angle)},
{ 0, 1, 0},
{ math.sin(angle), 0, math.cos(angle)}
})
end
function Matrix:rotationZ(angle)
return Matrix:newCheap({
{ math.cos(angle), math.sin(angle), 0},
{ - math.sin(angle), math.cos(angle), 0},
{ 0, 0, 1}
})
end
function Matrix:rotation(angles)
local mX = self:rotationX(angles.x)
local mY = self:rotationY(angles.y)
local mZ = self:rotationZ(angles.z)
return mY * mX * mZ;
end
function Matrix:axisAngle(vec)
local nvec = vector.normalize(vec)
local x = nvec.x
local y = nvec.y
local z = nvec.z
local a = vector.length(vec)
local c = math.cos(a)
local s = math.sin(a)
local ci = 1 - c
return Matrix:newCheap({
{x * x * ci + c, x * y * ci - z * s, x * z * ci + y * s},
{y * x * ci + z * s, y * y * ci + c, y * z * ci - x * s},
{z * x * ci - y * s, z * y * ci + x * s, z * z * ci + c}
})
end
function Matrix:toEuler()
local singular = 1 - math.abs(self.M[2][3]) < 1e-6
if not singular then
local x = math.asin(self.M[2][3])
local y = math.atan2(self.M[1][3] / math.cos(x), self.M[3][3] / math.cos(x))
local z = math.atan2(self.M[2][1] / math.cos(x), self.M[2][2] / math.cos(x))
return {x = x, y = -y, z = -z}
else
local y = 0
local x = 0
local z = 0
if self.M[2][3] < 0 then
x = -math.pi / 2
z = math.atan2(self.M[1][2], self.M[1][1])
else
x = math.pi / 2
z = - math.atan2(self.M[3][1], self.M[3][2])
end
return {x = x, y = y, z = -z}
end
end
function Matrix:height()
return #self.M
end
function Matrix:width()
return #self.M[1]
end
function Matrix:get(i, j)
return self.M[i][j]
end
function Matrix:set(i, j, x)
self.M[i][j] = x
end
function Matrix:__add(other)
if self:height() ~= other:height() or self:width() ~= other:width() then
return nil
end
local m = Matrix:new(self:height(), self:width())
for i = 1, self:height() do
for j = 1, self:width() do
m.M[i][j] = self.M[i][j] + other.M[i][j]
end
end
return m
end
function Matrix:__sub(other)
if self:height() ~= other:height() or self:width() ~= other:width() then
return nil
end
local m = Matrix:new(self:height(), self:width())
for i = 1, self:height() do
for j = 1, self:width() do
m.M[i][j] = self.M[i][j] - other.M[i][j]
end
end
return m
end
function Matrix:__mul(other)
if type(other) == "table" then
if self:width() ~= other:height() then
return nil
end
if self:height() == 1 and self:width() == 1 then
return other * self.M[1][1]
elseif other:height() == 1 and other:width() == 1 then
return self * other.M[1][1]
elseif self:width() == 3 and self:height() == 3 and other:width() == 1 and other:height() == 3 then
local m = Matrix:new(3, 1, 0)
for i = 1, 3, 1 do
local x = 0
for k = 1, 3, 1 do
x = x + self.M[i][k] * other.M[k][1]
end
m.M[i][1] = x
end
return m
elseif self:width() == 3 and self:height() == 1 and other:width() == 3 and other:height() == 3 then
local m = Matrix:new(1, 3, 0)
for j = 1, 3, 1 do
local x = 0
for k = 1, 3, 1 do
x = x + self.M[1][k] * other.M[k][j]
end
m.M[1][j] = x
end
return m
else
local m = Matrix:new(self:height(), other:width())
for i = 1, self:height() do
for j = 1, other:width() do
local elem = 0
for k = 1, self:width() do
elem = elem + self.M[i][k] * other.M[k][j]
end
m.M[i][j] = elem
end
end
return m
end
else
local m = Matrix:new(self:height(), self:width())
for i = 1, self:height() do
for j = 1, self:width() do
m.M[i][j] = self.M[i][j] * other
end
end
return m
end
end
function Matrix:transpose()
local newM = {}
for i = 1, self:width() do
table.insert(newM, {})
for j = 1, self:height() do
table.insert(newM[i], self.M[j][i])
end
end
self.M = newM
end
function Matrix:transposed()
local ret = Matrix:newCheap(self.M)
ret:transpose()
return ret
end
function Matrix:submatrix(i, j)
local sub = Matrix:new(self:height() - 1, self:width() - 1)
for k = 1, self:height() do
if k ~= i then
for l = 1, self:width() do
if l ~= j then
local y = 0
if k < i then
y = k
else
y = k -1
end
local x = 0
if l < j then
x = l
else
x = l - 1
end
sub.M[y][x] = self.M[k][l]
end
end
end
end
return sub
end
function Matrix:getBlock(i, j, h, w)
local ret = Matrix:new(h, w)
for k = 1, h, 1 do
for l = 1, w, 1 do
ret.M[k][l] = self.M[i + k - 1][j + l - 1]
end
end
return ret
end
function Matrix:determinant()
if self:height() ~= self:width() then
return 0
end
local tmp = Matrix:new(self)
local det = 1
for i = 1, tmp:width() do
if tmp.M[i][i] == 0 then
local solved = false
for k = i + 1, tmp:height() do
if tmp.M[k][i] ~= 0 then
tmp:swapRows(i, k)
solved = true
det = det * -1
break
end
end
if not solved then
return 0
end
end
for j = i + 1, tmp:height() do
if tmp.M[j][i] ~= 0 then
local fac = -tmp.M[i][i] / tmp.M[j][i]
det = det * (1 / fac)
tmp:multiplyRow(j, fac)
tmp:addRows(i, j)
end
end
end
for i = 1, self:height() do
det = det * tmp.M[i][i]
end
return det
end
function Matrix:multiplyRow(row, x)
for i = 1, self:width() do
self.M[row][i] = self.M[row][i] * x
end
end
--add row1 to row2
function Matrix:addRows(row1, row2)
for i = 1, self:width() do
self.M[row2][i] = self.M[row1][i] + self.M[row2][i]
end
end
function Matrix:swapRows(row1, row2)
local tmp = self.M[row1]
self.M[row1] = self.M[row2]
self.M[row2] = tmp
end
function Matrix:minor(i, j)
return self:submatrix(i, j):determinant()
end
function Matrix:cofactor(i, j)
return math.pow(-1, i + j) * self:minor(i, j)
end
function Matrix:inverse()
if self:width() == 1 and self:height() == 1 then
return Matrix:new(1, 1, 1 / self.M[1][1])
end
local det = self:determinant()
if det == 0 then
return nil
end
local inv = self:adjugate()
inv = inv * (1 / det)
return inv
end
function Matrix:adjugate()
local adj = Matrix:new(self:height(), self:width())
for i = 1, self:height() do
for j = 1, self:width() do
adj.M[i][j] = self:cofactor(i, j)
end
end
adj:transpose()
return adj
end
function Matrix:__tostring()
local str = ""
for k, v in ipairs(self.M) do
str = str .. "|"
for k2, v2 in ipairs(v) do
local num = tostring(v2)
while string.len(num) < 10 do
num = " " .. num
end
str = str .. " " .. num
end
str = str .. "|\n"
end
return str
end