This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 863
/
multiplying_a_matrix_by_a_vector.html
160 lines (147 loc) · 6.14 KB
/
multiplying_a_matrix_by_a_vector.html
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html data-require="math matrix matrix-input">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiplying a matrix by a vector</title>
<script src="../khan-exercise.js"></script>
</head>
<body>
<div class="exercise">
<div class="vars">
<var id="DIM_3">DIM_2</var>
<var id="DIM_4">1</var>
<var id="MAT_1">makeMatrix(randRange(-2, 5, DIM_1, DIM_2))</var>
<var id="MAT_2">makeMatrix(randRange(-2, 5, DIM_3, DIM_4))</var>
<var id="SOLN_MAT">
matrixMult(MAT_1, MAT_2)
</var>
<var id="PADDED_SOLN_MAT">matrixPad(SOLN_MAT, 3, 3)</var>
<var id="PRETTY_MAT_1_ID">"\\textbf " + randFromArray("ABCDEF")</var>
<var id="PRETTY_MAT_2_ID">"\\textbf " + randFromArray("vw")</var>
<!-- For generating hint matrices-->
<var id="ROW_COLORS">[GREEN, BLUE, GRAY]</var>
<var id="COL_COLORS">[ORANGE]</var>
<var id="FINAL_HINT_MAT">
makeMultHintMatrix(MAT_1, MAT_2, ROW_COLORS, COL_COLORS)
</var>
</div>
<div class="problem">
<p>
<code><var>PRETTY_MAT_1_ID</var> = <var>printSimpleMatrix(MAT_1)</var></code>
</p>
<p>
<code><var>PRETTY_MAT_2_ID</var> = <var>printSimpleMatrix(MAT_2)</var></code>
</p>
</div>
<p class="question">
What is <code><var>PRETTY_MAT_1_ID + PRETTY_MAT_2_ID</var></code>?
</p>
<div class="hints">
<div>
<p>
Because <code><var>PRETTY_MAT_1_ID</var></code> has dimensions <code>(<var>DIM_1 + "\\times" + DIM_2</var>)</code> and <code><var>PRETTY_MAT_2_ID</var></code> has dimensions <code>(<var>DIM_3 + "\\times" + DIM_4</var>)</code>, the answer matrix will have dimensions <code>(<var>DIM_1 + "\\times" + DIM_4</var>)</code>.
</p>
<p>
<code>
<var>PRETTY_MAT_1_ID + PRETTY_MAT_2_ID</var>
=
<var>printColoredDimMatrix(MAT_1, ROW_COLORS, true)</var>
<var>printColoredDimMatrix(MAT_2, COL_COLORS, false)</var>
=
<var>
printSimpleMatrix(maskMatrix(FINAL_HINT_MAT, []))
</var>
</code>
</p>
</div>
<div>
<p>
To find the element at any row <code>i</code>, column <code>j</code> of the answer matrix, multiply the elements in row <code>i</code> of the first matrix, <code><var>PRETTY_MAT_1_ID</var></code>, with the corresponding elements in column <code>j</code> of the second matrix, <code><var>PRETTY_MAT_2_ID</var></code>, and add the products together.
</p>
</div>
<div>
<p>
So, to find the element at row 1, column 1 of the answer matrix, multiply the first element in <code><var>colorMarkup("\\text{row }1", ROW_COLORS[0])</var></code> of <code><var>PRETTY_MAT_1_ID</var></code> with the first element in <code><var>colorMarkup("\\text{column }1", COL_COLORS[0])</var></code> of <code><var>PRETTY_MAT_2_ID</var></code>, then multiply the second element in <code><var>colorMarkup("\\text{row }1", ROW_COLORS[0])</var></code> of <code><var>PRETTY_MAT_1_ID</var></code> with the second element in <code><var>colorMarkup("\\text{column }1", COL_COLORS[0])</var></code> of <code><var>PRETTY_MAT_2_ID</var></code>, and so on. Add the products together.
</p>
<p>
<code>
<var>
printSimpleMatrix(
maskMatrix(FINAL_HINT_MAT, [[1, 1]])
)
</var>
</code>
</p>
</div>
<div>
<p>
Likewise, to find the element at row 2, column 1 of the answer matrix, multiply the elements in <code><var>colorMarkup("\\text{row }2", ROW_COLORS[1])</var></code> of <code><var>PRETTY_MAT_1_ID</var></code> with the corresponding elements in <code><var>colorMarkup("\\text{column }1", COL_COLORS[0])</var></code> of <code><var>PRETTY_MAT_2_ID</var></code> and add the products together.
</p>
<p>
<code>
<var>
printSimpleMatrix(
maskMatrix(FINAL_HINT_MAT, [[1, 1], [2, 1]])
)
</var>
</code>
</p>
</div>
<div>
<p>
Fill out the rest:
</p>
<p>
<code>
<var>
printSimpleMatrix(FINAL_HINT_MAT)
</var>
</code>
</p>
</div>
<div>
<p>After simplifying, we end up with:</p>
<div>
<code>
<var>printSimpleMatrix(SOLN_MAT)</var>
</code>
</div>
</div>
</div>
<div class="solution" data-type="multiple">
<div data-each="PADDED_SOLN_MAT as row" class="matrix-row">
<span data-each="row as elem">
<span data-if="elem !== ''" class="sol" data-forms="integer">
<var>elem</var>
</span>
<span data-else data-type="text" class="sol">
<var>elem</var>
</span>
</span>
</div>
</div>
<div class="problems">
<!-- 2x1 solution dimensions -->
<div id="2x2-2x1">
<div class="vars" data-apply="prependVars">
<var id="DIM_1">2</var>
<var id="DIM_2">2</var>
</div>
</div>
<div id="2x3-3x1">
<div class="vars" data-apply="prependVars">
<var id="DIM_1">2</var>
<var id="DIM_2">3</var>
</div>
</div>
<!-- 3x1 solution dimensions -->
<div id="3x2-2x1">
<div class="vars" data-apply="prependVars">
<var id="DIM_1">3</var>
<var id="DIM_2">2</var>
</div>
</div>
</div>
</div>
</body>
</html>