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
|
discard """
file: "tmatrix.nim"
output: "111"
"""
# Test overloading of [] with multiple indices
type
TMatrix* = object
data: seq[float]
fWidth, fHeight: int
template `|`(x, y: int): expr = y * m.fWidth + x
proc createMatrix*(width, height: int): TMatrix =
result.fWidth = width
result.fHeight = height
newSeq(result.data, width*height)
proc width*(m: TMatrix): int {.inline.} = return m.fWidth
proc height*(m: TMatrix): int {.inline.} = return m.fHeight
proc `[]`*(m: TMatrix, x, y: int): float {.inline.} =
result = m.data[x|y]
proc `[]=`*(m: var TMatrix, x, y: int, val: float) {.inline.} =
m.data[x|y] = val
proc `-|`*(m: TMatrix): TMatrix =
## transposes a matrix
result = createMatrix(m.height, m.width)
for x in 0..m.width-1:
for y in 0..m.height-1: result[y,x] = m[x,y]
#m.row(0, 2) # select row
#m.col(0, 89) # select column
const
w = 3
h = 20
var m = createMatrix(w, h)
for i in 0..w-1:
m[i, i] = 1.0
for i in 0..w-1:
stdout.write(m[i,i]) #OUT 111
|