-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path2d-convolution.typ
139 lines (126 loc) · 3.88 KB
/
2d-convolution.typ
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
#import "@preview/cetz:0.3.2": canvas, draw
#set page(width: auto, height: auto, margin: 5pt)
#canvas({
import draw: line, content, rect, on-layer
let cell-size = 0.6
let matrix-sep = 1.5
let highlight = rgb(255, 200, 150) // orange!30
let kernel-color = rgb("#9ae7e1") // teal!30
let result-color = rgb(200, 200, 255) // blue!30
// Helper to draw a matrix cell
let draw-cell(pos, value, fill: none, name: none) = {
rect(
pos,
(pos.at(0) + cell-size, pos.at(1) + cell-size),
fill: fill,
name: name,
stroke: .5pt,
)
if value != none {
content(
(pos.at(0) + cell-size / 2, pos.at(1) + cell-size / 2),
$#value$,
)
}
}
// Helper to draw a matrix
let draw-matrix(origin, shape, values, highlights: (), name: none) = {
let (rows, cols) = shape
for ii in range(rows) {
for jj in range(cols) {
let pos = (origin.at(0) + jj * cell-size, origin.at(1) - ii * cell-size)
let idx = ii * cols + jj
let cell-name = if name != none { name + "-" + str(ii) + "-" + str(jj) }
draw-cell(
pos,
if idx < values.len() { values.at(idx) },
fill: if (ii, jj) in highlights { highlight },
name: cell-name,
)
}
}
}
// Draw input matrix I
let input-origin = (0, 4)
let input-values = (
..(0, 1, 1, 1, 0, 0, 0),
..(0, 0, 1, 1, 1, 0, 0),
..(0, 0, 0, 1, 1, 1, 0),
..(0, 0, 0, 1, 1, 0, 0),
..(0, 0, 1, 1, 0, 0, 0),
..(0, 1, 1, 0, 0, 0, 0),
..(1, 1, 0, 0, 0, 0, 0),
)
draw-matrix(
input-origin,
(7, 7),
input-values,
highlights: ((0, 3), (0, 4), (0, 5), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)),
name: "I",
)
content(
(input-origin.at(0) + 7 * cell-size / 2, 0),
$bold(I)$,
name: "I-label",
)
// Draw multiplication symbol
content((rel: (1, 0), to: "I-3-6"), text(size: 18pt)[$*$], name: "times")
// Draw kernel matrix K
let kernel-origin = (input-origin.at(0) + 7 * cell-size + matrix-sep, input-origin.at(1) - 2 * cell-size)
let kernel-values = (1, 0, 1, 0, 1, 0, 1, 0, 1)
draw-matrix(
kernel-origin,
(3, 3),
kernel-values,
name: "K",
)
// Fill kernel matrix background
rect("K-0-0.north-west", "K-2-2.south-east", fill: kernel-color, stroke: none)
// Redraw matrix on top of background
draw-matrix(kernel-origin, (3, 3), kernel-values, name: "K")
content(
(kernel-origin.at(0) + 3 * cell-size / 2, 0),
$bold(K)$,
name: "K-label",
)
// Draw equals sign
content((rel: (1, 0), to: "K-1-2"), text(size: 18pt)[$=$], name: "equals")
// Draw result matrix
let result-origin = (kernel-origin.at(0) + 3 * cell-size + matrix-sep, input-origin.at(1) - cell-size)
let result-values = (
..(1, 4, 3, 4, 1),
..(1, 2, 4, 3, 3),
..(1, 2, 3, 4, 1),
..(1, 3, 3, 1, 1),
..(3, 3, 1, 1, 0),
)
draw-matrix(result-origin, (5, 5), result-values, name: "R")
// Draw highlighted cell in result matrix
on-layer(
-1,
rect("R-0-3.north-west", "R-0-3.south-east", fill: result-color, stroke: none),
)
content(
(result-origin.at(0) + 5 * cell-size / 2, 0),
$bold(I * K)$,
name: "R-label",
)
// Draw connection lines
let dash-style = (stroke: (dash: "dashed", paint: rgb(150, 220, 200)))
line("I-0-5.north-east", "K-0-0.north-west", ..dash-style)
line("I-2-5.south-east", "K-2-0.south-west", ..dash-style)
let result-style = (stroke: (dash: "dashed", paint: rgb(150, 150, 220)))
line("K-0-2.north-east", "R-0-3.north-west", ..result-style)
line("K-2-2.south-east", "R-0-3.south-west", ..result-style)
// Add small multiplication symbols in highlighted region
for ii in range(3) {
for jj in (3, 4, 5) {
content(
("I-" + str(ii) + "-" + str(jj) + ".south-west"),
text(size: 6pt)[×#calc.rem(ii + jj, 2)],
anchor: "south-west",
padding: 1pt,
)
}
}
})