Skip to main content\(\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\dimens}{\operatorname{dim}}
\DeclareMathOperator{\row}{\operatorname{row}}
\DeclareMathOperator{\col}{\operatorname{col}}
\newcommand{\im}{\operatorname{im}}
\newcommand{\nulls}{\operatorname{null}}
\newcommand{\minor}{\operatorname{minor}}
\newcommand{\spans}{\operatorname{span}}
\newcommand{\nullity}{\operatorname{nullity}}
\newcommand{\kers}{\operatorname{ker}}
\newcommand{\proj}{\operatorname{proj}}
\newcommand{\diag}{\operatorname{diag}}
\newcommand{\Tr}{\operatorname{Tr}}
\newcommand{\rank}{\operatorname{rank}}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section B.4 Elementwise Arithmetic Versus Linear Algebra
This section helps you read the difference between entry-by-entry arithmetic and linear-algebra operations.
u = np.array([1.0, 2.0])
v = np.array([3.0, 4.0])
u + v
2 * u
u * v # entry-by-entry product
u**2 # entry-by-entry square
u @ v # dot product
A = np.array([[1.0, 2.0],
[3.0, 4.0]])
A @ u # matrix-vector product
A.T # transpose
A.T @ u
u * v |
entry-by-entry product |
u @ v |
dot product |
A @ u |
matrix-vector product |
A @ B |
matrix product |
Vector arithmetic.
Read as. Vector sum, scalar multiple, and entry-by-entry square.
Shape/return. Vectors with matching shape.
Used for. Vector arithmetic and coordinatewise operations.
Transpose.
Read as. The transpose of
A.
Shape/return. A matrix with rows and columns switched.
Used for. Residual checks and shape changes.
Quadratic form.
Read as. The scalar
\(\mathbf h^T H\mathbf h\text{.}\)
Used for. Hessian quadratic forms and the quadratic part of a Taylor polynomial.
Watch for. NumPy evaluates this left to right, but for one-dimensional
h this reads as
\(\mathbf h^T H\mathbf h\text{.}\)
Sampled polynomial features.
Read as. Entry-by-entry powers or products of sampled coordinate arrays.
Shape/return. Arrays with the same shape as the sampled input arrays.
Used for. Polynomial feature columns in design matrices.
Unit 1 uses
u @ v,
K @ q,
alpha @ V, and
Y = A @ X. These are linear-algebra operations, not entry-by-entry multiplication.
Checkpoint B.4.2. Check Yourself: * versus @.
Let
u = np.array([1, 2]) and
v = np.array([3, 4]). Which expression computes the dot product,
u * v or
u @ v? What does the other expression compute?