forked from exo7math/deepmath-exo7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfig-pythonconv-01.tikz
More file actions
101 lines (88 loc) · 3.89 KB
/
fig-pythonconv-01.tikz
File metadata and controls
101 lines (88 loc) · 3.89 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
% From https://tex.stackexchange.com/questions/437007/drawing-a-convolution-with-tikz
% By jakun
\begin{tikzpicture}[scale=1.1]
\newcommand\numRowsK{3}
\newcommand\numColsK{3}
\newcommand{\myK}[2]{% #1: row, #2: col
\edef\Kcol##1##2##3{###2}%
\edef\Krow##1##2##3{\noexpand\Kcol###1}%
\Krow
{3 0 1}
{0 1 2}
{2 0 1}%
}
% ------- style -------
\tikzset{%
parenthesized/.style={%
left delimiter = (,
right delimiter = ),
},
node distance = 10mu,
}
% ------- equation -------
\matrix[matrix of math nodes, nodes in empty cells, align=center,text depth=.2ex,text height=1.3ex,text width=0.8em,] (I) {
|[gray]| 0 &|[gray]| 0& |[gray]| 0 &|[gray]| 0 &|[gray]| 0 & |[gray]| 0 \\
|[gray]| 0 & 2 & 1 & 3 & 0 & |[gray]| 0\\
|[gray]| 0 & 1 & 1 & 0 & 5 & |[gray]| 0 \\
|[gray]| 0 & 3 & 3 & 1 & 0 & |[gray]| 0\\
|[gray]| 0 & 2 & 0 & 0 & 2 & |[gray]| 0 \\
|[gray]| 0 &|[gray]| 0 & |[gray]| 0& |[gray]| 0 & |[gray]| 0 & |[gray]| 0\\
};
% \node (*) [right = of I] {${}*{}$};
\node (*) [right =1cm of I] {};
\newcommand\Cmatrix{}
\foreach \row in {1, ..., 3} {
\gdef \sep {}
\foreach \col in {1, ..., 3} {%
\xdef \Cmatrix {\unexpanded\expandafter{\Cmatrix}\unexpanded\expandafter{\sep}\noexpand \myK{\row}{\col}}
\gdef \sep { \& }
}
\xdef \Cmatrix {\unexpanded\expandafter{\Cmatrix}\noexpand\\}
}
\matrix[matrix of math nodes, parenthesized, ampersand replacement=\&] (K) [right = of *] {
\Cmatrix
};
% \node (=) [right = of K] {${}={}$};
\node (=) [right =1.5cm of K] {};
\matrix[matrix of math nodes, parenthesized, nodes in empty cells, align=center,text depth=.2ex,text height=1.5ex,text width=1em,] (I*K) [right = of {=}] {
5 & 9 & 10 & 0 \\
7 & 17 & 19 & 16 \\
10 & 12 & 11 & 0 \\
5 & 10 & 13 & 5 \\
};
% ------- highlighting -------
\newcommand\rowResult{1}
\newcommand\colResult{1}
\begin{scope}[on background layer]
\newcommand{\padding}{1pt}
\coordinate (Is-nw) at ([xshift=-\padding, yshift=0] I-\rowResult-\colResult.north west);
\coordinate (Is-se) at ([xshift=+\padding, yshift=-1.5*\padding] I-\the\numexpr\rowResult+\numRowsK-1\relax-\the\numexpr\colResult+\numColsK-1\relax.south east);
\coordinate (Is-sw) at (Is-nw |- Is-se);
\coordinate (Is-ne) at (Is-se |- Is-nw);
\filldraw[blue, fill opacity=.1] (Is-nw) rectangle (Is-se);
\filldraw[red, fill opacity=.1] (I*K-\rowResult-\colResult.north west) rectangle (I*K-\rowResult-\colResult.south east);
\draw[green!70!black, dotted, thick]
(Is-nw) -- (K.north west)
(Is-se) -- (K.south east)
(Is-sw) -- (K.south west)
(Is-ne) -- (K.north east)
;
\draw[red, dotted, thick]
(I*K-\rowResult-\colResult.north west) -- (K.north west)
(I*K-\rowResult-\colResult.south east) -- (K.south east)
(I*K-\rowResult-\colResult.south west) -- (K.south west)
(I*K-\rowResult-\colResult.north east) -- (K.north east)
;
\draw[green!70!black, fill=green!10!white] (K.north west) rectangle (K.south east);
\foreach \row [evaluate=\row as \rowI using int(\row+\rowResult-1)] in {1, ..., \numRowsK} {%
\foreach \col [evaluate=\col as \colI using int(\col+\colResult-1)] in {1, ..., \numColsK} {%
\node[green!70!black] at (I-\rowI-\colI.south east) [xshift=-.3em,yshift=+0.2ex] {\tiny$\times \myK{\row}{\col}$};
}
}
\end{scope}
% ------- labels -------
\tikzset{node distance=0em}
\node[below=of I] (I-label) {$A$};
\node at (K |- I-label) {$M$ retourné};
\node at (I*K |- I-label) {Convolution de même taille $A \star M$};
\end{tikzpicture}