-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalgebra.lua
85 lines (75 loc) · 1.69 KB
/
algebra.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
local Matrix = {}
local Vector = {}
----------------------
-- Vector operations
----------------------
function Vector.map(vector, fn)
local vector_result = { }
for i=1, #vector do
vector_result[i] = fn(vector[i])
end
return vector_result
end
function Vector.addition(vector1, vector2)
local vector = { }
for i=1, #vector1 do
vector[i] = vector1[i] + vector2[i]
end
return vector
end
function Vector.scalar_product(vector1, vector2)
local sum = 0
for i=1, #vector1 do
sum = sum + (vector1[i]*vector2[i])
end
return sum
end
function Vector.max(q_values)
local max_q = q_values[1]
local max_q_indices = { 1 }
for i=2, #q_values do
if q_values[i] > max_q then
max_q = q_values[i]
max_q_indices = { i }
elseif q_values[i] == max_q then
max_q_indices[#max_q_indices + 1] = i
end
end
return max_q_indices
end
----------------------
-- Matrix operations
----------------------
function Matrix.create(rows, columns, value_generator)
local matrix = {}
for r=1, rows do
matrix[r] = { }
for c=1, columns do
matrix[r][c] = value_generator and value_generator(r, c) or 0
end
end
return matrix
end
function Matrix.map(matrix, fn)
local map = function(row)
return Vector.map(row, fn)
end
return Vector.map(matrix, map)
end
function Matrix.vector_product(matrix, vector)
local mul = function(row)
return Vector.scalar_product(row, vector)
end
return Vector.map(matrix, mul)
end
function Matrix.addition(matrix1, matrix2)
local matrix = { }
for r=1, #matrix1 do
matrix[r] = Vector.addition(matrix1[r], matrix2[r])
end
return matrix
end
return {
Vector = Vector,
Matrix = Matrix
}