"""Independent algebra examples with exact rational inputs."""
from fractions import Fraction
import json
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 square_parts(a, b):
return [a*a, a*b, b*a, b*b]
def squared_error(data, center):
return sum((x - center)**2 for x in data)
check("four_pieces", [2.5, 1.5], [6.25, 3.75, 3.75, 2.25], square_parts(2.5, 1.5))
check("repartition_same_area", [[2.5, 1.5], [1, 3]], [16, 16],
[sum(square_parts(a, b)) for a, b in ((2.5, 1.5), (1, 3))])
pairs = [(2, 5), (-3, 4), (Fraction(2, 3), Fraction(1, 6))]
check("two_terms_with_signs_and_fractions", [[str(a), str(b)] for a, b in pairs], [True] * 3,
[(a+b)**2 == a*a+2*a*b+b*b and (a-b)**2 == a*a-2*a*b+b*b
and (a+b)*(a-b) == a*a-b*b for a, b in pairs])
a, b, c = 2, -3, 5
check("three_terms", [a,b,c], [16,16], [(a+b+c)**2, a*a+b*b+c*c+2*a*b+2*a*c+2*b*c])
left, right = (5, -2, 4), (2, 0, -1)
check("squared_vector_difference", [left,right], [38,38], [
sum((x-y)**2 for x,y in zip(left,right,strict=True)),
sum(x*x for x in left)-2*sum(x*y for x,y in zip(left,right,strict=True))+sum(y*y for y in right)])
check("practice_square_coefficients", [5, "x=5"], [9,121], [(5-2)**2, (2*5+1)**2])
check("vertex_from_completed_square", [1,6,2], [-3,-7], [-6/2, 2-(6/2)**2])
check("completed_square_back_expands", [-4,-3,7], [True] * 3,
[x*x+6*x+2 == (x+3)**2-7 for x in (-4,-3,7)])
check("complete_square_uses_half_linear_coefficient", [-6,0,4], [[3,-9],[0,0],[-2,-4]],
[[-b/2,-(b/2)**2] for b in (-6,0,4)])
check("complete_square_reduces_weighted_quadratic", [2,8,3], [-2,-5], [-8/(2*2), 3-8**2/(4*2)])
check("collect_like_terms", [2,-5,4], 1, sum((2,-5,4)))
check("factor_out_common_term", [-4,0,5], [True] * 3,
[3*x*x+6*x == 3*x*(x+2) for x in (-4,0,5)])
check("substitute_factor_and_restore", [0,1,2,-3], [0,0,12,72],
[(x*x+1)**2-3*(x*x+1)+2 for x in (0,1,2,-3)])
check("substitution_matches_simplified_form", [0,1,2,-3], [0,0,12,72],
[x**4-x*x for x in (0,1,2,-3)])
sample = [2, 4, 5, 7, 12]
mean = Fraction(sum(sample), len(sample))
check("mean_minimizes_sum_and_mean_error", sample, ["6","58","58/5"], [
str(mean), str(squared_error(sample, mean)), str(squared_error(sample,mean)/len(sample))])
check("offset_from_mean_cost", [sample,[2,4,6,8,10]], [138,78,58,78,138],
[squared_error(sample,c) for c in (2,4,6,8,10)])
check("independent_quadratic_derivation", sample, [5,-60,238],
[len(sample), -2*sum(sample), sum(x*x for x in sample)])
check("sum_identity_off_center", [sample,-2], "378",
str(squared_error(sample,mean)+len(sample)*(-2-mean)**2))
check("empty_data_has_no_unique_minimizer", [], [0,0],
[squared_error([],c) for c in (0,17)])
check("source_data_numeric_goal", [1,3,4,8,9], [46,66,66], [squared_error((1,3,4,8,9),c) for c in (5,3,7)])
print(json.dumps(dict(version=platform.python_version(), cases=cases), ensure_ascii=False))
本例需在 Windows、Python 3.12.14、Chromium、标准浏览器 SVG。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14、Chromium、标准浏览器 SVG。
入口文件:projects/ai-math-symbolic-lab/symbolic-numerics.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
symbolic-numerics.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14、Chromium、标准浏览器 SVG。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联54章) · 含当前章节或实例 · mbskycom202612a33.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a82.zip