-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_2d.lua
More file actions
198 lines (157 loc) · 4.42 KB
/
Copy patharray_2d.lua
File metadata and controls
198 lines (157 loc) · 4.42 KB
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
local Class = require("class")
local deepcopy = require("deepcopy")
local Array2D = Class.new()
function Array2D:init(rows, cols, val)
local val = val or 0
self.rows = rows
self.cols = cols
self._data = {}
for i = 1, self.rows do
local tempRow = {}
for j = 1, self.cols do
tempRow[j] = val
end
self._data[i] = tempRow
end
end
function Array2D:setValue(val)
for i = 1, self.rows do
for j = 1, self.cols do
self._data[i][j] = val
end
end
end
function Array2D:copy()
local arrayCopy = Array2D(self.rows, self.cols)
arrayCopy._data = deepcopy(self._data)
return arrayCopy
end
function Array2D:add(val)
-- scalar or element-wise addition
local outArr = Array2D(self.rows, self.cols)
if type(val) == "number" then
for i = 1, self.rows do
for j = 1, self.cols do
outArr._data[i][j] = self._data[i][j] + val
end
end
else
if self.rows ~= val.rows then
error("Number of rows dont match")
end
if self.cols ~= val.cols then
error("Number of columns dont match")
end
for i = 1, self.rows do
for j = 1, self.cols do
outArr._data[i][j] = self._data[i][j] + val._data[i][j]
end
end
end
return outArr
end
function Array2D:mult(val)
-- scalar or element-wise multiplication
local outArr = Array2D(self.rows, self.cols)
if type(val) == "number" then
for i = 1, self.rows do
for j = 1, self.cols do
outArr._data[i][j] = self._data[i][j] * val
end
end
else
if self.rows ~= val.rows then
error("Number of rows dont match")
end
if self.cols ~= val.cols then
error("Number of columns dont match")
end
for i = 1, self.rows do
for j = 1, self.cols do
outArr._data[i][j] = self._data[i][j] * val._data[i][j]
end
end
end
return outArr
end
function Array2D:dot(other)
-- matrix multiplication
if self.cols ~= other.rows then
error("First's columns dont match second's rows")
end
local newArr = Array2D(self.rows, other.cols, 0)
for i_row = 1, self.rows do
for i_col = 1, other.cols do
for ii = 1, self.cols do
newArr._data[i_row][i_col] = newArr._data[i_row][i_col] +
self._data[i_row][ii] * other._data[ii][i_col]
end
end
end
return newArr
end
function Array2D:T()
local newArr = Array2D(self.cols, self.rows)
for i = 1, self.rows do
for j = 1, self.cols do
newArr._data[j][i] = self._data[i][j]
end
end
return newArr
end
function Array2D:flatten()
local size = self.rows * self.cols
local newArr = Array2D(size, 1)
for i = 1, self.rows do
for j = 1, self.cols do
newArr._data[(i - 1) * self.cols + j][1] = self._data[i][j]
end
end
return newArr
end
function Array2D:reshape(rows, cols)
if rows * cols ~= self.rows * self.cols then
error("Number of elements must remain unchanged")
end
local outArray = Array2D(rows, cols)
for i = 1, self.rows do
for j = 1, self.cols do
local flat_index = (i - 1) * self.cols + (j - 1)
local new_i = math.floor(flat_index / cols) + 1
local new_j = (flat_index % cols) + 1
outArray._data[new_i][new_j] = self._data[i][j]
end
end
return outArray
end
function Array2D:slice(i_start, i_end, i_step, j_start, j_end, j_step)
local dataTable = {}
for i = i_start, i_end, i_step do
local newRow = {}
for j = j_start, j_end, j_step do
table.insert(newRow, self._data[i][j])
end
table.insert(dataTable, newRow)
end
local outArr = Array2D(#dataTable, #(dataTable[1]))
outArr._data = dataTable
return outArr
end
function Array2D:__tostring()
local outString = ""
for i, row in ipairs(self._data) do
outString = outString .. table.concat(row, "\t") .. "\n"
end
return outString
end
function Array2D:__unm()
return self:mult(-1)
end
function Array2D:__add(other)
if type(self) == "number" then
return other:add(self)
else
return self:add(other)
end
end
return Array2D