"""Numerical examples for angles, projections and sinusoidal position encoding."""
import json
import math
import platform
cases = []
def check(name, inputs, expected, actual):
assert expected == actual, (name, expected, actual)
cases.append(dict(name=name, input=inputs, expected=expected, actual=actual, passed=True))
def dot(left, right):
return math.fsum(a * b for a, b in zip(left, right, strict=True))
def scale(factor, vector):
return [factor * component for component in vector]
def cosine(left, right):
denominator = math.hypot(*left) * math.hypot(*right)
if denominator == 0:
raise ValueError("cosine requires nonzero vectors")
return dot(left, right) / denominator
def frequency(column, width):
return 10000 ** (-2 * (column // 2) / width)
def encoding(position, column, width):
angle = position * frequency(column, width)
return math.cos(angle) if column % 2 else math.sin(angle)
a, b = (6, 8), (8, 6)
check("length_dot_cosine", [a, b], [10, 10, 96, 0.96],
[math.hypot(*a), math.hypot(*b), dot(a, b), round(cosine(a, b), 6)])
check("rescaling_changes_dot_only", [a, b, 3], [288, 0.96],
[dot(a, scale(3, b)), round(cosine(a, scale(3, b)), 6)])
check("length_345", [-3, 4], 5, math.hypot(-3, 4))
check("component_addition", [[2, -1], [-5, 4]], [-3, 3],
[a + b for a, b in zip((2, -1), (-5, 4), strict=True)])
check("scalar_branches", [[2, -3], [2, -2, 0]], [[4, -6], [-4, 6], [0, 0]],
[scale(factor, (2, -3)) for factor in (2, -2, 0)])
check("dot_three_signs", [[3, 0], [[1, 2], [0, 2], [-1, 2]]], [3, 0, -3],
[dot((3, 0), (x, 2)) for x in (1, 0, -1)])
try:
cosine((0, 0), a)
except ValueError as exc:
check("zero_vector_cosine", [[0, 0], a], "cosine requires nonzero vectors", str(exc))
else:
raise AssertionError("zero-length vector accepted")
angle = math.atan2(4, 3)
check("triangle_ratios", [3, 4, 5], [0.8, 0.6],
[round(f(angle), 9) for f in (math.sin, math.cos)])
check("unit_circle_bounds", [-720, 720], True,
all(-1 <= f(math.radians(i)) <= 1 for i in range(-720, 721) for f in (math.sin, math.cos)))
check("trig_period", [-0.8, 2 * math.pi], True,
all(math.isclose(f(-0.8), f(-0.8 + math.tau), abs_tol=1e-12) for f in (math.sin, math.cos)))
check("unit_circle_identity", [0, 3, 90, 225], True,
all(math.isclose(math.sin(t)**2 + math.cos(t)**2, 1)
for t in map(math.radians, (0, 3, 90, 225))))
check("encoding_column_pairs", [16, [0, 1, 2, 3, 4, 5]],
[1, 1, 0.316228, 0.316228, 0.1, 0.1],
[round(frequency(i, 16), 6) for i in range(6)])
check("encoding_pair_at_three", [3, [0, 1, 2, 3], 16], [0.141120, -0.989992, 0.812649, 0.582754],
[round(encoding(3, i, 16), 6) for i in range(4)])
check("encoding_pair_at_origin", [0, [0, 1], 16], [0, 1], [encoding(0, i, 16) for i in range(2)])
check("encoding_grid_bounds", [50, 16], True,
all(-1 <= encoding(pos, col, 16) <= 1 for pos in range(50) for col in range(16)))
vector = (4, 1, 7)
direction = (0.6, 0.8, 0)
distance = dot(vector, direction)
projection = scale(distance, direction)
residual = [x - y for x, y in zip(vector, projection, strict=True)]
recombined = [x + y for x, y in zip(residual, projection, strict=True)]
check("projection_reconstructs_vector", [vector, direction], [3.2, [1.92, 2.56, 0], 0, [4, 1, 7]],
[round(distance, 6), [round(c, 6) for c in projection], round(dot(residual, direction), 6), recombined])
check("basis_recombination", [[4, -2, 6], "standard basis"], [4, -2, 6],
[sum(scale(k, v)[j] for k, v in zip((4, -2, 6), ((1, 0, 0), (0, 1, 0), (0, 0, 1)), strict=True))
for j in range(3)])
check("axis_sign_matters", [-30, 0.1], True, cosine((-30, 0.1), (-1, 0)) > 0.999)
print(json.dumps(dict(version=platform.python_version(), cases=cases), ensure_ascii=False))
本例需在 Windows、Python 3.12.14、Chromium、标准浏览器 API。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14、Chromium、标准浏览器 API。
入口文件:projects/ai-math-geometry-lab/geometry-numerics.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
geometry-numerics.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14、Chromium、标准浏览器 API。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联54章) · 含当前章节或实例 · mbskycom202612a33.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a58.zip