Jump to content

Matrix/Practice/Matrix multiplication: Difference between revisions

From Wikibase
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Line 136: Line 136:
$$
$$
using several methods.
using several methods.
{{Hidden solution}}
The scalar product is the sum of the element-wise products, which is also the $1\times3$ by $3\times1$ matrix product; the norm is the square root of a vector's scalar product with itself.
<syntaxhighlight lang="python" class="pyodide" copy>
import numpy as np
u = np.array([7, 6, 3])
v = np.array([1, -1, 2])
w = np.array([2, 1, -1])
# 1. scalar product, three ways
print("u . v, np.dot          :", np.dot(u, v))
print("u . v, sum of products  :", np.sum(u * v))
print("u . v, 1x3 times 3x1    :", (u.reshape(1, 3) @ v.reshape(3, 1)).item())
# 2. norm, three ways
print("|w|, np.linalg.norm    :", np.linalg.norm(w))
print("|w|, sqrt(w . w)        :", np.sqrt(np.dot(w, w)))
print("|w|, sqrt of square sum :", np.sqrt(np.sum(w ** 2)))
</syntaxhighlight>
{{End hidden}}


== Exercise 4 ==
== Exercise 4 ==

Revision as of 09:32, 19 November 2026

Languages: English · français · Esperanto

The solutions are programmatic: the code in each