blob: 5563fae8c4acfc09db0359d5161900f6fe009975 (
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
|
discard """
matrix: "--mm:orc"
"""
# bug #20993
type
Dual[int] = object # must be generic (even if fully specified)
p: int
proc D(p: int): Dual[int] = Dual[int](p: p)
proc `+`(x: Dual[int], y: Dual[int]): Dual[int] = D(x.p + y.p)
type
Tensor[T] = object
buf: seq[T]
proc newTensor*[T](s: int): Tensor[T] = Tensor[T](buf: newSeq[T](s))
proc `[]`*[T](t: Tensor[T], idx: int): T = t.buf[idx]
proc `[]=`*[T](t: var Tensor[T], idx: int, val: T) = t.buf[idx] = val
proc `+.`[T](t1, t2: Tensor[T]): Tensor[T] =
let n = t1.buf.len
result = newTensor[T](n)
for i in 0 ..< n:
result[i] = t1[i] + t2[i]
proc toTensor*[T](a: sink seq[T]): Tensor[T] =
## This breaks it: Using `T` instead makes it work
type U = typeof(a[0])
var t: Tensor[U] # Tensor[T] works
t.buf = a
result = t
proc loss() =
var B = toTensor(@[D(123)])
let a = toTensor(@[D(-10)])
B = B +. a
doAssert B[0].p == 113, "I want to be 113, but I am " & $B[0].p
loss()
|