blob: 17ff3c0d793f630337990648be2f9ad376844f8f (
plain) (
blame)
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
|
type Matrix* = object
me*: seq[float64]
w: int
h: int
proc newMatrix*(me: seq[float64], w: int, h: int): Matrix =
return Matrix(
me: me,
w: w,
h: h
)
proc newIdentityMatrix*(n: int): Matrix =
var me = newSeq[float64](n * n)
for i in 0 ..< n:
me[n * i + i] = 1
return Matrix(
me: me,
w: n,
h: n
)
proc newMatrixUninitialized*(w, h: int): Matrix =
return Matrix(
me: newSeqUninitialized[float64](w * h),
w: w,
h: h
)
#TODO this is extremely inefficient
proc `*`*(a: Matrix, b: Matrix): Matrix =
assert a.w == b.h
let h = a.h
let w = b.w
let n = a.w
var c = newMatrixUninitialized(w, h)
for x in 0 ..< w:
for y in 0 ..< h:
var val: float64 = 0
for i in 0 ..< n:
val += a.me[y * a.w + i] * b.me[i * b.w + x]
c.me[y * c.w + x] = val
return c
proc `*=`*(a: var Matrix, b: Matrix) =
a = a * b
|