丢番图#
备注
For a beginner-friendly guide focused on solving Diophantine equations, refer to Solve a Diophantine Equation Algebraically.
丢番图方程#
“丢番图”这个词来自于一个名叫丢番图的数学家,他生活在公元250年左右的大城市亚历山大。他常被称为“代数之父”,他在著名的著作《算术》中提出了150个问题,标志着数论的早期开端,关于整数及其性质的研究领域。丢番图方程在数论中占有重要地位。
我们把“丢番图方程”称为形式方程, \(f(x_1, x_2, \ldots x_n) = 0\) 在哪里? \(n \geq 2\) 和 \(x_1, x_2, \ldots x_n\) 是整数变量。如果我们能找到 \(n\) 整数 \(a_1, a_2, \ldots a_n\) 这样的话 \(x_1 = a_1, x_2 = a_2, \ldots x_n = a_n\) 满足上面的方程,我们说这个方程是可解的。关于丢番图方程的更多信息,请参见 [1] 和 [2].
目前,以下五类丢番图方程可以用 diophantine()
以及丢番图模块的其他辅助函数。
线性丢番图方程: \(a_1x_1 + a_2x_2 + \ldots + a_nx_n = b\) .
一般二元二次方程: \(ax^2 + bxy + cy^2 + dx + ey + f = 0\)
齐次三元二次方程: \(ax^2 + by^2 + cz^2 + dxy + eyz + fzx = 0\)
扩展毕达哥拉斯方程: \(a_{{1}}x_{{1}}^2 + a_{{2}}x_{{2}}^2 + \ldots + a_{{n}}x_{{n}}^2 = a_{{n+1}}x_{{n+1}}^2\)
一般平方和: \(x_{{1}}^2 + x_{{2}}^2 + \ldots + x_{{n}}^2 = k\)
模块结构#
本模块包含 diophantine()
以及求解某些丢番图方程所需的辅助函数。它的结构如下。
当一个方程式给 diophantine()
,它对方程进行因子化(如果可能),并通过调用 diop_solve()
分开。然后使用 merge_solution()
.
diop_solve()
内部使用 classify_diop()
查找给定给它的方程的类型(以及其他一些细节),然后根据返回的类型调用相应的解算器函数。例如,如果 classify_diop()
返回“线性”作为方程类型,然后 diop_solve()
电话 diop_linear()
来解这个方程。
每个功能, diop_linear()
, diop_quadratic()
, diop_ternary_quadratic()
, diop_general_pythagorean()
和 diop_general_sum_of_squares()
解出一个特定类型的方程,并且可以很容易地通过它的名称来猜测类型。
除了这些功能之外,“丢番图模块”中还有相当多的其他功能,它们都列在用户功能和内部功能下。
教程#
首先,让我们导入Diophantine模块的最高API。
>>> from sympy.solvers.diophantine import diophantine
在我们开始解方程之前,我们需要定义变量。
>>> from sympy import symbols
>>> x, y, z = symbols("x, y, z", integer=True)
让我们从解最简单的丢番图方程开始,即线性丢番图方程。我们来解决 \(2x + 3y = 5\) . 注意,虽然我们用上面的形式来写方程,但是当我们把方程输入丢番图模块中的任何函数时,它必须是这种形式 \(eq = 0\) .
>>> diophantine(2*x + 3*y - 5)
{(3*t_0 - 5, 5 - 2*t_0)}
请注意,在最高API下再多一级,我们可以通过调用 diop_solve()
.
>>> from sympy.solvers.diophantine.diophantine import diop_solve
>>> diop_solve(2*x + 3*y - 5)
(3*t_0 - 5, 5 - 2*t_0)
注意,它返回一个元组而不是一个集合。 diophantine()
总是返回一组元组。但是 diop_solve()
可能返回单个元组或一组元组,具体取决于给定方程的类型。
我们也可以通过调用 diop_linear()
,这是什么 diop_solve()
内部通话。
>>> from sympy.solvers.diophantine.diophantine import diop_linear
>>> diop_linear(2*x + 3*y - 5)
(3*t_0 - 5, 5 - 2*t_0)
如果给定的方程没有解,则输出如下所示。
>>> diophantine(2*x + 4*y - 3)
set()
>>> diop_solve(2*x + 4*y - 3)
(None, None)
>>> diop_linear(2*x + 4*y - 3)
(None, None)
注意,除了最高级别的API,在没有解决方案的情况下,一个元组 \(None\) 返回。元组的大小与变量的数量相同。此外,还可以通过传递自定义参数来具体设置要在解决方案中使用的参数。考虑以下示例:
>>> m = symbols("m", integer=True)
>>> diop_solve(2*x + 3*y - 5, m)
(3*m_0 - 5, 5 - 2*m_0)
对于线性丢番图方程,自定义参数是解中每个自由变量的前缀。考虑以下示例:
>>> diop_solve(2*x + 3*y - 5*z + 7, m)
(m_0, m_0 + 5*m_1 - 14, m_0 + 3*m_1 - 7)
在上述解中,m_0和m_1是自变量。
请注意,目前用户只能为线性丢番图方程和二元二次方程设置参数。
让我们试着解一个二元二次方程,它是一个二元二次方程。尝试用这些方程来求解这些方程会有很多帮助。请参考 [3] 和 [4] 详细分析了不同案例的性质和解决方案。让我们来定义一下 \(\Delta = b^2 - 4ac\) w、 r.t.二元二次型 \(ax^2 + bxy + cy^2 + dx + ey + f = 0\) .
什么时候? \(\Delta < 0\) ,要么没有解,要么只有有限个解。
>>> diophantine(x**2 - 4*x*y + 8*y**2 - 3*x + 7*y - 5)
{(2, 1), (5, 1)}
在上面的等式中 \(\Delta = (-4)^2 - 4*1*8 = -16\) 因此只有有限数量的解存在。
什么时候? \(\Delta = 0\) 我们可能没有解或者参数化解。
>>> diophantine(3*x**2 - 6*x*y + 3*y**2 - 3*x + 7*y - 5)
set()
>>> diophantine(x**2 - 4*x*y + 4*y**2 - 3*x + 7*y - 5)
{(-2*t**2 - 7*t + 10, -t**2 - 3*t + 5)}
>>> diophantine(x**2 + 2*x*y + y**2 - 3*x - 3*y)
{(t_0, -t_0), (t_0, 3 - t_0)}
最有趣的情况是 \(\Delta > 0\) 而且它不是一个完美的广场。在这种情况下,该方程既没有解,也没有无穷多个解。考虑以下情况,其中 \(\Delta = 8\) .
>>> diophantine(x**2 - 4*x*y + 2*y**2 - 3*x + 7*y - 5)
set()
>>> from sympy import sqrt
>>> n = symbols("n", integer=True)
>>> s = diophantine(x**2 - 2*y**2 - 2*x - 4*y, n)
>>> x_1, y_1 = s.pop()
>>> x_2, y_2 = s.pop()
>>> x_n = -(-2*sqrt(2) + 3)**n/2 + sqrt(2)*(-2*sqrt(2) + 3)**n/2 - sqrt(2)*(2*sqrt(2) + 3)**n/2 - (2*sqrt(2) + 3)**n/2 + 1
>>> x_1 == x_n or x_2 == x_n
True
>>> y_n = -sqrt(2)*(-2*sqrt(2) + 3)**n/4 + (-2*sqrt(2) + 3)**n/2 + sqrt(2)*(2*sqrt(2) + 3)**n/4 + (2*sqrt(2) + 3)**n/2 - 1
>>> y_1 == y_n or y_2 == y_n
True
在这里 \(n\) 是一个整数。虽然x_n和y_n看起来不像整数,但用特定值代替n(并简化)表明它们确实是整数。例如,考虑下面的例子,我们将n设为9。
>>> from sympy import simplify
>>> simplify(x_n.subs({n: 9}))
-9369318
任何形式的二元二次方 \(ax^2 + bxy + cy^2 + dx + ey + f = 0\) 可以转换为 \(X^2 - DY^2 = N\) .
>>> from sympy.solvers.diophantine.diophantine import find_DN, diop_DN, transformation_to_DN
>>> find_DN(x**2 - 3*x*y + y**2 - 7*x + 5*y - 3)
(5, 920)
所以,上面的方程是等价的 \(X^2 - 5Y^2 = 920\) 经过线性变换。如果我们想找到线性变换,我们可以用 transformation_to_DN()
>>> A, B = transformation_to_DN(x**2 - 3*x*y + y**2 - 7*x + 5*y - 3)
这里A是2x2矩阵,B是2x1矩阵,这样变换
给出方程式 \(X^2 -5Y^2 = 920\) . 价值观 \(A\) 和 \(B\) 如下所示。
>>> A
Matrix([
[1/10, 3/10],
[ 0, 1/5]])
>>> B
Matrix([
[ 1/5],
[-11/5]])
我们可以解这个形式的方程 \(X^2 - DY^2 = N\) 旁路 \(D\) 和 \(N\) 到 diop_DN()
>>> diop_DN(5, 920)
[]
不幸的是,我们的方程没有解。
现在我们来看齐次三元二次方程。这些方程式的形式 \(ax^2 + by^2 + cz^2 + dxy + eyz + fzx = 0\) . 这类方程要么有无穷多的解,要么没有解(除了明显的解(0,0,0))
>>> diophantine(3*x**2 + 4*y**2 - 5*z**2 + 4*x*y + 6*y*z + 7*z*x)
{(0, 0, 0)}
>>> diophantine(3*x**2 + 4*y**2 - 5*z**2 + 4*x*y - 7*y*z + 7*z*x)
{(-16*p**2 + 28*p*q + 20*q**2, 3*p**2 + 38*p*q - 25*q**2, 4*p**2 - 24*p*q + 68*q**2)}
如果您只对基本解决方案而不是参数化通用解决方案(更准确地说,是通用解决方案之一)感兴趣,则可以使用 diop_ternary_quadratic()
.
>>> from sympy.solvers.diophantine.diophantine import diop_ternary_quadratic
>>> diop_ternary_quadratic(3*x**2 + 4*y**2 - 5*z**2 + 4*x*y - 7*y*z + 7*z*x)
(-4, 5, 1)
diop_ternary_quadratic()
首先将给定的方程转换为形式的等效方程 \(w^2 = AX^2 + BY^2\) 然后它使用 descent()
解后一个方程。您可以参考 transformation_to_normal()
找到更多的信息。方程式 \(w^2 = AX^2 + BY^2\) 使用上述方法可以更容易地解决 descent()
.
>>> from sympy.solvers.diophantine.diophantine import descent
>>> descent(3, 1) # solves the equation w**2 = 3*Y**2 + Z**2
(1, 0, 1)
这里的解元组的顺序是(w,Y,Z)
扩展的毕达哥拉斯方程, \(a_{{1}}x_{{1}}^2 + a_{{2}}x_{{2}}^2 + \ldots + a_{{n}}x_{{n}}^2 = a_{{n+1}}x_{{n+1}}^2\) 平方和方程, \(x_{{1}}^2 + x_{{2}}^2 + \ldots + x_{{n}}^2 = k\) 也可以使用丢番图模块求解。
>>> from sympy.abc import a, b, c, d, e, f
>>> diophantine(9*a**2 + 16*b**2 + c**2 + 49*d**2 + 4*e**2 - 25*f**2)
{(70*t1**2 + 70*t2**2 + 70*t3**2 + 70*t4**2 - 70*t5**2, 105*t1*t5, 420*t2*t5, 60*t3*t5, 210*t4*t5, 42*t1**2 + 42*t2**2 + 42*t3**2 + 42*t4**2 + 42*t5**2)}
功能 diop_general_pythagorean()
也可以直接调用来求解同一个方程。或者你可以打电话 diop_general_pythagorean()
或者使用高级API。对于一般的平方和来说,这也是正确的,但是调用 diop_general_sum_of_squares()
你可以控制返回多少个解决方案。
>>> from sympy.solvers.diophantine.diophantine import diop_general_sum_of_squares
>>> eq = a**2 + b**2 + c**2 + d**2 - 18
>>> diophantine(eq)
{(0, 0, 3, 3), (0, 1, 1, 4), (1, 2, 2, 3)}
>>> diop_general_sum_of_squares(eq, 2)
{(0, 0, 3, 3), (1, 2, 2, 3)}
这个 sum_of_squares()
例程将提供一个返回解的迭代器,可以控制解是否包含零(不包含零的解首先返回):
>>> from sympy.solvers.diophantine.diophantine import sum_of_squares
>>> sos = sum_of_squares(18, 4, zeros=True)
>>> next(sos)
(1, 2, 2, 3)
>>> next(sos)
(0, 0, 3, 3)
简单的埃及分数也可以在丢番图模中找到。例如,以下是将1/2表示为两个单位分数之和的方法:
>>> from sympy import Eq, S
>>> diophantine(Eq(1/x + 1/y, S(1)/2))
{(-2, 1), (1, -2), (3, 6), (4, 4), (6, 3)}
要更全面地了解Diophantine模块,请参阅以下博客。
工具书类#
用户功能#
此函数被导入到全局命名空间中 from sympy import *
:
- sympy.solvers.diophantine.diophantine.diophantine(eq, param=t, syms=None, permute=False)[源代码]#
简化丢番图方程的求解过程
eq
把它转换成一个应该等于零的项的乘积。解释
例如,在求解时, \(x^2 - y^2 = 0\) 这被视为 \((x + y)(x - y) = 0\) 和 \(x + y = 0\) 和 \(x - y = 0\) 独立求解和组合求解。每个术语都通过调用
diop_solve()
. (尽管可以打电话diop_solve()
直接地说,我们必须小心地以正确的形式传递一个等式,并正确地解释输出;diophantine()
是面向公众的功能。)产量
diophantine()
是一组元组。元组的元素是方程中每个变量的解,并根据变量的字母顺序排列。e、 g.对于一个有两个变量的方程, \(a\) 和 \(b\) ,元组的第一个元素是 \(a\) 第二个是 \(b\) .使用
diophantine(eq, t, syms)
:解丢番图方程eq
.t
要由使用的可选参数diop_solve()
.syms
是一个可选的符号列表,用于确定返回元组中元素的顺序。默认情况下,只返回基本解决方案。如果
permute
设置为True,则在适用时返回基解的置换和/或值的符号的置换。细节
eq
应为假定为零的表达式。t
要在解决方案中使用的参数。实例
>>> from sympy import diophantine >>> from sympy.abc import a, b >>> eq = a**4 + b**4 - (2**4 + 3**4) >>> diophantine(eq) {(2, 3)} >>> diophantine(eq, permute=True) {(-3, -2), (-3, 2), (-2, -3), (-2, 3), (2, -3), (2, 3), (3, -2), (3, 2)}
>>> from sympy.abc import x, y, z >>> diophantine(x**2 - y**2) {(t_0, -t_0), (t_0, t_0)}
>>> diophantine(x*(2*x + 3*y - z)) {(0, n1, n2), (t_0, t_1, 2*t_0 + 3*t_1)} >>> diophantine(x**2 + 3*x*y + 4*x) {(0, n1), (-3*t_0 - 4, t_0)}
这个函数是用 from sympy.solvers.diophantine import *
:
内部函数#
这些函数用于丢番图模块的内部使用。
- sympy.solvers.diophantine.diophantine.diop_solve(eq, param=t)[源代码]#
解丢番图方程
eq
.解释
不像
diophantine()
,保理eq
未尝试。使用classify_diop()
以确定方程的类型并调用相应的解算器函数。使用
diophantine()
与其他助手函数相比,建议使用。diop_solve()
根据方程式的性质,可以返回集合或元组。使用
diop_solve(eq, t)
:求解丢番图方程,eq
使用t
如果需要,作为参数。细节
eq
应为假定为零的表达式。t
是要在解决方案中使用的参数。实例
>>> from sympy.solvers.diophantine import diop_solve >>> from sympy.abc import x, y, z, w >>> diop_solve(2*x + 3*y - 5) (3*t_0 - 5, 5 - 2*t_0) >>> diop_solve(4*x + 3*y - 4*z + 5) (t_0, 8*t_0 + 4*t_1 + 5, 7*t_0 + 3*t_1 + 5) >>> diop_solve(x + 3*y - 4*z + w - 6) (t_0, t_0 + t_1, 6*t_0 + 5*t_1 + 4*t_2 - 6, 5*t_0 + 4*t_1 + 3*t_2 - 6) >>> diop_solve(x**2 + y**2 - 5) {(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)}
参见
- sympy.solvers.diophantine.diophantine.diop_linear(eq, param=t)[源代码]#
求解线性丢番图方程。
线性丢番图方程是一个形式方程 \(a_{{1}}x_{{1}} + a_{{2}}x_{{2}} + .. + a_{{n}}x_{{n}} = 0\) 在哪里? \(a_{{1}}, a_{{2}}, ..a_{{n}}\) 是整数常量和 \(x_{{1}}, x_{{2}}, ..x_{{n}}\) 是整数变量。
使用
diop_linear(eq)
:返回包含丢番图方程解的元组eq
. 元组中的值按与排序变量相同的顺序排列。细节
eq
是一个假定为零的线性丢番图方程。param
要在解决方案中使用的参数。实例
>>> from sympy.solvers.diophantine.diophantine import diop_linear >>> from sympy.abc import x, y, z >>> diop_linear(2*x - 3*y - 5) # solves equation 2*x - 3*y - 5 == 0 (3*t_0 - 5, 2*t_0 - 5)
这里=-3 t_0 - 5 and y = -2 图0-5
>>> diop_linear(2*x - 3*y - 4*z -3) (t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)
- sympy.solvers.diophantine.diophantine.base_solution_linear(c, a, b, t=None)[源代码]#
返回线性方程的基解, \(ax + by = c\) .
解释
被使用
diop_linear()
求线性丢番图方程的基解。如果t
然后返回参数化的解决方案。使用
base_solution_linear(c, a, b, t)
:a
,b
,c
系数是 \(ax + by = c\) 和t
要在解决方案中使用的参数。实例
>>> from sympy.solvers.diophantine.diophantine import base_solution_linear >>> from sympy.abc import t >>> base_solution_linear(5, 2, 3) # equation 2*x + 3*y = 5 (-5, 5) >>> base_solution_linear(0, 5, 7) # equation 5*x + 7*y = 0 (0, 0) >>> base_solution_linear(5, 2, 3, t) # equation 2*x + 3*y = 5 (3*t - 5, 5 - 2*t) >>> base_solution_linear(0, 5, 7, t) # equation 5*x + 7*y = 0 (7*t, -5*t)
- sympy.solvers.diophantine.diophantine.diop_quadratic(eq, param=t)[源代码]#
求解二次丢番图方程。
i、 e.形式方程 \(Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0\) . 返回包含元组的集合 \((x, y)\) 其中包含了解决方案。如果没有解决办法 \((None, None)\) 返回。
使用
diop_quadratic(eq, param)
:eq
是二次二元丢番图方程。param
用于指示要在解决方案中使用的参数。细节
eq
应为假定为零的表达式。param
是要在解决方案中使用的参数。实例
>>> from sympy.abc import x, y, t >>> from sympy.solvers.diophantine.diophantine import diop_quadratic >>> diop_quadratic(x**2 + y**2 + 2*x + 2*y + 2, t) {(-1, -1)}
工具书类
[R858]Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online], Available: https://www.alpertron.com.ar/METHODS.HTM
[R859]Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online], Available: https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
- sympy.solvers.diophantine.diophantine.diop_DN(D, N, t=t)[源代码]#
求解方程 \(x^2 - Dy^2 = N\) .
解释
Mainly concerned with the case \(D > 0, D\) is not a perfect square, which is the same as the generalized Pell equation. The LMM algorithm [R860] is used to solve this equation.
返回一个解决方案元组, (\(x, y)\) 对于每一类解。类的其他解可以根据
D
和N
.使用
diop_DN(D, N, t)
:D和N是整数,如 \(x^2 - Dy^2 = N\) 和t
是要在解决方案中使用的参数。细节
D
和N
对应于方程中的D和N。t
是要在解决方案中使用的参数。实例
>>> from sympy.solvers.diophantine.diophantine import diop_DN >>> diop_DN(13, -4) # Solves equation x**2 - 13*y**2 = -4 [(3, 1), (393, 109), (36, 10)]
结果可以解释为:方程有三个基本解 \(x^2 - 13y^2 = -4\) 由(3,1),(393,109)和(36,10)给出。每个元组的形式是(x,y),即解决方案(3,1)意味着 \(x = 3\) 和 \(y = 1\) .
>>> diop_DN(986, 1) # Solves equation x**2 - 986*y**2 = 1 [(49299, 1570)]
参见
工具书类
[R860] (1,2)Solving the generalized Pell equation x**2 - D*y**2 = N, John P. Robertson, July 31, 2004, Pages 16 - 17. [online], Available: https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
- sympy.solvers.diophantine.diophantine.cornacchia(a: int, b: int, m: int) set[tuple[int, int]] [源代码]#
解决方案 \(ax^2 + by^2 = m\) 在哪里? \(\gcd(a, b) = 1 = gcd(a, m)\) 和 \(a, b > 0\) .
解释
Uses the algorithm due to Cornacchia. The method only finds primitive solutions, i.e. ones with \(\gcd(x, y) = 1\). So this method cannot be used to find the solutions of \(x^2 + y^2 = 20\) since the only solution to former is \((x, y) = (4, 2)\) and it is not primitive. When \(a = b\), only the solutions with \(x \leq y\) are found. For more details, see the References.
实例
>>> from sympy.solvers.diophantine.diophantine import cornacchia >>> cornacchia(2, 3, 35) # equation 2x**2 + 3y**2 = 35 {(2, 3), (4, 1)} >>> cornacchia(1, 1, 25) # equation x**2 + y**2 = 25 {(4, 3)}
工具书类
[R861]Nitaj,“科尔纳奇亚的L'algorithme de Cornacchia”
[R862]解丢番图方程ax 2+通过 2=m,采用Cornacchia法, [在线] ,可用:http://www.numbertheory.org/php/cornacchia.html
- sympy.solvers.diophantine.diophantine.diop_bf_DN(D, N, t=t)[源代码]#
用蛮力来解方程, \(x^2 - Dy^2 = N\) .
解释
Mainly concerned with the generalized Pell equation which is the case when \(D > 0, D\) is not a perfect square. For more information on the case refer [R863]. Let \((t, u)\) be the minimal positive solution of the equation \(x^2 - Dy^2 = 1\). Then this method requires \(\sqrt{\\frac{\mid N \mid (t \pm 1)}{2D}}\) to be small.
使用
diop_bf_DN(D, N, t)
:D
和N
系数是 \(x^2 - Dy^2 = N\) 和t
是要在解决方案中使用的参数。细节
D
和N
对应于方程中的D和N。t
是要在解决方案中使用的参数。实例
>>> from sympy.solvers.diophantine.diophantine import diop_bf_DN >>> diop_bf_DN(13, -4) [(3, 1), (-3, 1), (36, 10)] >>> diop_bf_DN(986, 1) [(49299, 1570)]
参见
工具书类
[R863] (1,2)Solving the generalized Pell equation x**2 - D*y**2 = N, John P. Robertson, July 31, 2004, Page 15. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
- sympy.solvers.diophantine.diophantine.transformation_to_DN(eq)[源代码]#
这个函数转换一般的二次函数, \(ax^2 + bxy + cy^2 + dx + ey + f = 0\) 更容易处理 \(X^2 - DY^2 = N\) 形式。
解释
This is used to solve the general quadratic equation by transforming it to the latter form. Refer to [R864] for more detailed information on the transformation. This function returns a tuple (A, B) where A is a 2 X 2 matrix and B is a 2 X 1 matrix such that,
转置( [x y] )=A*转置( [X Y] )+B级
使用
transformation_to_DN(eq)
哪里eq
是要变换的二次方。实例
>>> from sympy.abc import x, y >>> from sympy.solvers.diophantine.diophantine import transformation_to_DN >>> A, B = transformation_to_DN(x**2 - 3*x*y - y**2 - 2*y + 1) >>> A Matrix([ [1/26, 3/26], [ 0, 1/13]]) >>> B Matrix([ [-6/13], [-4/13]])
A、 返回的B是这样的:转置((x y))=A*转置((x y))+B \(x\) 和 \(y\) 再做一点简化,就可以得到一个形式方程 \(x^2 - Dy^2 = N\) .
>>> from sympy.abc import X, Y >>> from sympy import Matrix, simplify >>> u = (A*Matrix([X, Y]) + B)[0] # Transformation for x >>> u X/26 + 3*Y/26 - 6/13 >>> v = (A*Matrix([X, Y]) + B)[1] # Transformation for y >>> v Y/13 - 4/13
接下来我们将用这些公式代替 \(x\) 和 \(y\) 并且做
simplify()
.>>> eq = simplify((x**2 - 3*x*y - y**2 - 2*y + 1).subs(zip((x, y), (u, v)))) >>> eq X**2/676 - Y**2/52 + 17/13
通过适当地乘以分母,我们可以得到标准形式的佩尔方程。
>>> eq * 676 X**2 - 13*Y**2 + 884
如果只需要最后的方程式,
find_DN()
可以使用。参见
工具书类
[R864] (1,2)Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0, John P.Robertson, May 8, 2003, Page 7 - 11. https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
- sympy.solvers.diophantine.diophantine.transformation_to_normal(eq)[源代码]#
Returns the transformation Matrix that converts a general ternary quadratic equation
eq
(\(ax^2 + by^2 + cz^2 + dxy + eyz + fxz\)) to a form without cross terms: \(ax^2 + by^2 + cz^2 = 0\). This is not used in solving ternary quadratics; it is only implemented for the sake of completeness.
- sympy.solvers.diophantine.diophantine.find_DN(eq)[源代码]#
此函数返回一个元组, \((D, N)\) 简化形式, \(x^2 - Dy^2 = N\) ,对应于一般的二次函数, \(ax^2 + bxy + cy^2 + dx + ey + f = 0\) .
求解一般二次方程就等于求解方程 \(X^2 - DY^2 = N\) 并使用
transformation_to_DN()
.使用
find_DN(eq)
哪里eq
是要变换的二次方。实例
>>> from sympy.abc import x, y >>> from sympy.solvers.diophantine.diophantine import find_DN >>> find_DN(x**2 - 3*x*y - y**2 - 2*y + 1) (13, -884)
对输出的解释是 \(X^2 -13Y^2 = -884\) 改造后 \(x^2 - 3xy - y^2 - 2y + 1\) 使用返回的转换
transformation_to_DN()
.工具书类
[R865]Solving the equation ax^2 + bxy + cy^2 + dx + ey + f = 0, John P.Robertson, May 8, 2003, Page 7 - 11. https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
- sympy.solvers.diophantine.diophantine.diop_ternary_quadratic(eq, parameterize=False)[源代码]#
求解一般的二次三元形式, \(ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0\) .
返回元组 \((x, y, z)\) 这是上述方程的基本解。如果没有解决办法, \((None, None, None)\) 返回。
使用
diop_ternary_quadratic(eq)
:返回包含基本解决方案的元组eq
.细节
eq
应该是三个变量中二次的齐次表达式,并且假定它为零。实例
>>> from sympy.abc import x, y, z >>> from sympy.solvers.diophantine.diophantine import diop_ternary_quadratic >>> diop_ternary_quadratic(x**2 + 3*y**2 - z**2) (1, 0, 1) >>> diop_ternary_quadratic(4*x**2 + 5*y**2 - z**2) (1, 0, 2) >>> diop_ternary_quadratic(45*x**2 - 7*y**2 - 8*x*y - z**2) (28, 45, 105) >>> diop_ternary_quadratic(x**2 - 49*y**2 - z**2 + 13*z*y -8*x*y) (9, 1, 5)
- sympy.solvers.diophantine.diophantine.square_factor(a)[源代码]#
返回一个整数 \(c\) s、 t。 \(a = c^2k, \ c,k \in Z\) . 在这里 \(k\) 是自由的。 \(a\) 可以是整数或因子字典。
实例
>>> from sympy.solvers.diophantine.diophantine import square_factor >>> square_factor(24) 2 >>> square_factor(-36*3) 6 >>> square_factor(1) 1 >>> square_factor({3: 2, 2: 1, -1: 1}) # -18 3
- sympy.solvers.diophantine.diophantine.descent(A, B)[源代码]#
返回一个非常重要的解决方案(x,y,z) \(x^2 = Ay^2 + Bz^2\) 采用格点归约的拉格朗日下降法。 \(A\) 和 \(B\) 假设存在这样的解决方案是有效的。
这比普通的拉格朗日下降算法快,因为使用了高斯约化。
实例
>>> from sympy.solvers.diophantine.diophantine import descent >>> descent(3, 1) # x**2 = 3*y**2 + z**2 (1, 0, 1)
\((x, y, z) = (1, 0, 1)\) 是上述方程的解。
>>> descent(41, -113) (-16, -3, 1)
工具书类
[R866]Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics. Mathematics of Computation, 72(243), 1417-1441. https://doi.org/10.1090/S0025-5718-02-01480-1
- sympy.solvers.diophantine.diophantine.diop_general_pythagorean(eq, param=m)[源代码]#
解出一般的毕达哥拉斯方程, \(a_{{1}}^2x_{{1}}^2 + a_{{2}}^2x_{{2}}^2 + . . . + a_{{n}}^2x_{{n}}^2 - a_{{n + 1}}^2x_{{n + 1}}^2 = 0\) .
返回一个元组,该元组包含方程的参数化解,按与输入变量相同的顺序排序。
使用
diop_general_pythagorean(eq, param)
哪里eq
是一个一般的毕达哥拉斯方程,假设为零,并且param
用于通过下标构造其他参数的基参数。实例
>>> from sympy.solvers.diophantine.diophantine import diop_general_pythagorean >>> from sympy.abc import a, b, c, d, e >>> diop_general_pythagorean(a**2 + b**2 + c**2 - d**2) (m1**2 + m2**2 - m3**2, 2*m1*m3, 2*m2*m3, m1**2 + m2**2 + m3**2) >>> diop_general_pythagorean(9*a**2 - 4*b**2 + 16*c**2 + 25*d**2 + e**2) (10*m1**2 + 10*m2**2 + 10*m3**2 - 10*m4**2, 15*m1**2 + 15*m2**2 + 15*m3**2 + 15*m4**2, 15*m1*m4, 12*m2*m4, 60*m3*m4)
- sympy.solvers.diophantine.diophantine.diop_general_sum_of_squares(eq, limit=1)[源代码]#
求解方程 \(x_{{1}}^2 + x_{{2}}^2 + . . . + x_{{n}}^2 - k = 0\) .
最多返回
limit
解决方案的数量。使用
general_sum_of_squares(eq, limit)
:这里eq
是一个假定为零的表达式。也,eq
应该在形式上, \(x_{{1}}^2 + x_{{2}}^2 + . . . + x_{{n}}^2 - k = 0\) .细节
When \(n = 3\) if \(k = 4^a(8m + 7)\) for some \(a, m \in Z\) then there will be no solutions. Refer to [R867] for more details.
实例
>>> from sympy.solvers.diophantine.diophantine import diop_general_sum_of_squares >>> from sympy.abc import a, b, c, d, e >>> diop_general_sum_of_squares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345) {(15, 22, 22, 24, 24)}
参考文献
[R867]Representing an integer as a sum of three squares, [online], Available: https://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares
- sympy.solvers.diophantine.diophantine.diop_general_sum_of_even_powers(eq, limit=1)[源代码]#
求解方程 \(x_{{1}}^e + x_{{2}}^e + . . . + x_{{n}}^e - k = 0\) 在哪里? \(e\) 是偶数的整数幂。
最多返回
limit
解决方案的数量。使用
general_sum_of_even_powers(eq, limit)
:这里eq
是一个假定为零的表达式。也,eq
应该在形式上, \(x_{{1}}^e + x_{{2}}^e + . . . + x_{{n}}^e - k = 0\) .实例
>>> from sympy.solvers.diophantine.diophantine import diop_general_sum_of_even_powers >>> from sympy.abc import a, b >>> diop_general_sum_of_even_powers(a**4 + b**4 - (2**4 + 3**4)) {(2, 3)}
- sympy.solvers.diophantine.diophantine.power_representation(n, p, k, zeros=False)[源代码]#
返回用于查找整数k元组的生成器, \((n_{{1}}, n_{{2}}, . . . n_{{k}})\) ,这样 \(n = n_{{1}}^p + n_{{2}}^p + . . . n_{{k}}^p\) .
使用
power_representation(n, p, k, zeros)
:表示非负数n
作为总数k
p
第三次幂。如果zeros
为true,则允许解包含零。实例
>>> from sympy.solvers.diophantine.diophantine import power_representation
将1729表示为两个立方体的和:
>>> f = power_representation(1729, 3, 2) >>> next(f) (9, 10) >>> next(f) (1, 12)
如果国旗 \(zeros\) 如果为True,则该解决方案可能包含带零的元组;任何此类解决方案都将在没有零的解决方案之后生成:
>>> list(power_representation(125, 2, 3, zeros=True)) [(5, 6, 8), (3, 4, 10), (0, 5, 10), (0, 2, 11)]
平分 \(p\) 这个 \(permute_sign\) 函数可用于获取所有有符号值:
>>> from sympy.utilities.iterables import permute_signs >>> list(permute_signs((1, 12))) [(1, 12), (-1, 12), (1, -12), (-1, -12)]
还可以获得所有可能的有符号排列:
>>> from sympy.utilities.iterables import signed_permutations >>> list(signed_permutations((1, 12))) [(1, 12), (-1, 12), (1, -12), (-1, -12), (12, 1), (-12, 1), (12, -1), (-12, -1)]
- sympy.solvers.diophantine.diophantine.partition(n, k=None, zeros=False)[源代码]#
返回可用于生成整数分区的生成器 \(n\) .
解释
一个分区 \(n\) 是一组加起来等于 \(n\) . 例如,3的分区是3,1+2,1+1+1。分区作为元组返回。如果
k
等于None,则返回所有可能的分区,而不考虑它们的大小,否则只返回大小为的分区k
返回。如果zero
参数设置为True,则在大小小于的每个分区的末尾添加适当数量的零k
.zero
只有在以下情况下才考虑参数k
不是没有。当分区结束时,最后一个 \(next()\) 呼叫抛出StopIteration
异常,因此此函数应始终在try-except块中使用。细节
partition(n, k)
:这里n
是一个正整数,并且k
是分区的大小,也是正整数。实例
>>> from sympy.solvers.diophantine.diophantine import partition >>> f = partition(5) >>> next(f) (1, 1, 1, 1, 1) >>> next(f) (1, 1, 1, 2) >>> g = partition(5, 3) >>> next(g) (1, 1, 3) >>> next(g) (1, 2, 2) >>> g = partition(5, 3, zeros=True) >>> next(g) (0, 0, 5)
- sympy.solvers.diophantine.diophantine.sum_of_three_squares(n)[源代码]#
Returns a 3-tuple \((a, b, c)\) such that \(a^2 + b^2 + c^2 = n\) and \(a, b, c \geq 0\).
Returns None if \(n = 4^a(8m + 7)\) for some \(a, m \in \mathbb{Z}\). See [R868] for more details.
- 参数:
n : Integer
non-negative integer
- 返回:
(int, int, int) | None : 3-tuple non-negative integers
(a, b, c)
satisfyinga**2 + b**2 + c**2 = n
.a,b,c are sorted in ascending order.
None
if no such(a,b,c)
.- 加薪:
ValueError
If
n
is a negative integer
实例
>>> from sympy.solvers.diophantine.diophantine import sum_of_three_squares >>> sum_of_three_squares(44542) (18, 37, 207)
参见
power_representation
sum_of_three_squares(n)
is one of the solutions output bypower_representation(n, 2, 3, zeros=True)
工具书类
[R868] (1,2)Representing a number as a sum of three squares, [online], Available: https://schorn.ch/lagrange.html
- sympy.solvers.diophantine.diophantine.sum_of_four_squares(n)[源代码]#
Returns a 4-tuple \((a, b, c, d)\) such that \(a^2 + b^2 + c^2 + d^2 = n\). Here \(a, b, c, d \geq 0\).
- 参数:
n : Integer
non-negative integer
- 返回:
(int, int, int, int) : 4-tuple non-negative integers
(a, b, c, d)
satisfyinga**2 + b**2 + c**2 + d**2 = n
.a,b,c,d are sorted in ascending order.
- 加薪:
ValueError
If
n
is a negative integer
实例
>>> from sympy.solvers.diophantine.diophantine import sum_of_four_squares >>> sum_of_four_squares(3456) (8, 8, 32, 48) >>> sum_of_four_squares(1294585930293) (0, 1234, 2161, 1137796)
参见
power_representation
sum_of_four_squares(n)
is one of the solutions output bypower_representation(n, 2, 4, zeros=True)
工具书类
[R869]Representing a number as a sum of four squares, [online], Available: https://schorn.ch/lagrange.html
- sympy.solvers.diophantine.diophantine.sum_of_powers(n, p, k, zeros=False)[源代码]#
返回用于查找整数k元组的生成器, \((n_{{1}}, n_{{2}}, . . . n_{{k}})\) ,这样 \(n = n_{{1}}^p + n_{{2}}^p + . . . n_{{k}}^p\) .
使用
power_representation(n, p, k, zeros)
:表示非负数n
作为总数k
p
第三次幂。如果zeros
为true,则允许解包含零。实例
>>> from sympy.solvers.diophantine.diophantine import power_representation
将1729表示为两个立方体的和:
>>> f = power_representation(1729, 3, 2) >>> next(f) (9, 10) >>> next(f) (1, 12)
如果国旗 \(zeros\) 如果为True,则该解决方案可能包含带零的元组;任何此类解决方案都将在没有零的解决方案之后生成:
>>> list(power_representation(125, 2, 3, zeros=True)) [(5, 6, 8), (3, 4, 10), (0, 5, 10), (0, 2, 11)]
平分 \(p\) 这个 \(permute_sign\) 函数可用于获取所有有符号值:
>>> from sympy.utilities.iterables import permute_signs >>> list(permute_signs((1, 12))) [(1, 12), (-1, 12), (1, -12), (-1, -12)]
还可以获得所有可能的有符号排列:
>>> from sympy.utilities.iterables import signed_permutations >>> list(signed_permutations((1, 12))) [(1, 12), (-1, 12), (1, -12), (-1, -12), (12, 1), (-12, 1), (12, -1), (-12, -1)]
- sympy.solvers.diophantine.diophantine.sum_of_squares(n, k, zeros=False)[源代码]#
返回一个生成器,该生成器生成非负值的k元组,其平方和为n。如果零为假(默认),则解决方案将不包含零。元组的非负元素被排序。
如果k==1且n为平方,则返回(n,)。
如果k==2,那么n只能写成平方和,如果n的因式分解中的每个素数的形式为4 k+3具有偶数多重性。如果n是素数,那么它只能写成两个平方和,如果它的形式是4 k+1。
如果k==3,那么n可以写成平方和,如果它没有形式4 **m* (8*k+7)。
所有整数都可以写成4平方和。
如果k>4,则n可以被分区,并且每个分区可以写为4个平方和;如果n不能被4平均整除,则只有当附加的分区可以写成平方和时,n才可以写为平方和。例如,如果k=6,那么n被分为两部分,第一部分写为4个平方和,第二个部分写为2个平方和——只有满足上述k=2的条件时才能这样做,所以这将自动拒绝n的某些分区。
实例
>>> from sympy.solvers.diophantine.diophantine import sum_of_squares >>> list(sum_of_squares(25, 2)) [(3, 4)] >>> list(sum_of_squares(25, 2, True)) [(3, 4), (0, 5)] >>> list(sum_of_squares(25, 4)) [(1, 2, 2, 4)]
- sympy.solvers.diophantine.diophantine.merge_solution(var, var_t, solution)[源代码]#
用此子方程组的全解来构造。
解释
例如在解方程时 \((x - y)(x^2 + y^2 - z^2) = 0\) ,每个方程的解 \(x - y = 0\) 和 \(x^2 + y^2 - z^2\) 是独立的。解决方案 \(x - y = 0\) 是 \((x, y) = (t, t)\) . 但是当我们输出原始方程的解时,我们应该引入一个z值。此函数转换 \((t, t)\) 进入之内 \((t, t, n_{{1}})\) 在哪里? \(n_{{1}}\) 是一个整数参数。
- sympy.solvers.diophantine.diophantine.PQa(P_0, Q_0, D)[源代码]#
返回求解Pell方程所需的有用信息。
解释
There are six sequences of integers defined related to the continued fraction representation of \(\\frac{P + \sqrt{D}}{Q}\), namely {\(P_{i}\)}, {\(Q_{i}\)}, {\(a_{i}\)},{\(A_{i}\)}, {\(B_{i}\)}, {\(G_{i}\)}.
PQa()
Returns these values as a 6-tuple in the same order as mentioned above. Refer [R870] for more detailed information.使用
PQa(P_0, Q_0, D)
:P_0
,Q_0
和D
整数与 \(P_{{0}}\) , \(Q_{{0}}\) 和 \(D\) 在连分式中 \(\\frac{{P_{{0}} + \sqrt{{D}}}}{{Q_{{0}}}}\) . 我们还假设 \(P_{{0}}^2 == D mod(|Q_{{0}}|)\) 和 \(D\) 是自由的。实例
>>> from sympy.solvers.diophantine.diophantine import PQa >>> pqa = PQa(13, 4, 5) # (13 + sqrt(5))/4 >>> next(pqa) # (P_0, Q_0, a_0, A_0, B_0, G_0) (13, 4, 3, 3, 1, -1) >>> next(pqa) # (P_1, Q_1, a_1, A_1, B_1, G_1) (-1, 1, 1, 4, 1, 3)
工具书类
[R870] (1,2)Solving the generalized Pell equation x^2 - Dy^2 = N, John P. Robertson, July 31, 2004, Pages 4 - 8. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
- sympy.solvers.diophantine.diophantine.equivalent(u, v, r, s, D, N)[源代码]#
如果有两个解决方案,则返回True \((u, v)\) 和 \((r, s)\) 属于 \(x^2 - Dy^2 = N\) 属于同一等价类,否则为False。
解释
Two solutions \((u, v)\) and \((r, s)\) to the above equation fall to the same equivalence class iff both \((ur - Dvs)\) and \((us - vr)\) are divisible by \(N\). See reference [R871]. No test is performed to test whether \((u, v)\) and \((r, s)\) are actually solutions to the equation. User should take care of this.
使用
equivalent(u, v, r, s, D, N)
: \((u, v)\) 和 \((r, s)\) 是这个方程的两个解 \(x^2 - Dy^2 = N\) 所有涉及的参数都是整数。实例
>>> from sympy.solvers.diophantine.diophantine import equivalent >>> equivalent(18, 5, -18, -5, 13, -1) True >>> equivalent(3, 1, -18, 393, 109, -4) False
工具书类
[R871] (1,2)Solving the generalized Pell equation x**2 - D*y**2 = N, John P. Robertson, July 31, 2004, Page 12. https://web.archive.org/web/20160323033128/http://www.jpr2718.org/pell.pdf
- sympy.solvers.diophantine.diophantine.parametrize_ternary_quadratic(eq)[源代码]#
返回三元二次方程的参数化一般解
eq
哪个有表格 \(ax^2 + by^2 + cz^2 + fxy + gyz + hxz = 0\) .实例
>>> from sympy import Tuple, ordered >>> from sympy.abc import x, y, z >>> from sympy.solvers.diophantine.diophantine import parametrize_ternary_quadratic
参数化的解决方案可以返回三个参数:
>>> parametrize_ternary_quadratic(2*x**2 + y**2 - 2*z**2) (p**2 - 2*q**2, -2*p**2 + 4*p*q - 4*p*r - 4*q**2, p**2 - 4*p*q + 2*q**2 - 4*q*r)
也可能只有两个参数:
>>> parametrize_ternary_quadratic(4*x**2 + 2*y**2 - 3*z**2) (2*p**2 - 3*q**2, -4*p**2 + 12*p*q - 6*q**2, 4*p**2 - 8*p*q + 6*q**2)
笔记
考虑
p
和q
在前面的2参数解中,观察到一对给定的参数可以表示多个解。如果 \(p\) 和q
不是互质的,这是微不足道的事实,因为公因子也会是解的公因子值。但即使在p
和q
是互质的:>>> sol = Tuple(*_) >>> p, q = ordered(sol.free_symbols) >>> sol.subs([(p, 3), (q, 2)]) (6, 12, 12) >>> sol.subs([(q, 1), (p, 1)]) (-1, 2, 2) >>> sol.subs([(q, 0), (p, 1)]) (2, -4, 4) >>> sol.subs([(q, 1), (p, 0)]) (-3, -6, 6)
除符号和公因子外,它们都等价于(1,2,2)的解。
工具书类
[R872]《数学解题》,剑桥大学学生数学教材,1998年版。
- sympy.solvers.diophantine.diophantine.diop_ternary_quadratic_normal(eq, parameterize=False)[源代码]#
求解二次三元丢番图方程, \(ax^2 + by^2 + cz^2 = 0\) .
解释
这里是系数 \(a\) , \(b\) 和 \(c\) 应该是非零。否则,方程将是二次二元或一元方程。如果可解,则返回元组 \((x, y, z)\) 满足给定方程。如果方程没有整数解, \((None, None, None)\) 返回。
使用
diop_ternary_quadratic_normal(eq)
哪里eq
是一个形式方程 \(ax^2 + by^2 + cz^2 = 0\) .实例
>>> from sympy.abc import x, y, z >>> from sympy.solvers.diophantine.diophantine import diop_ternary_quadratic_normal >>> diop_ternary_quadratic_normal(x**2 + 3*y**2 - z**2) (1, 0, 1) >>> diop_ternary_quadratic_normal(4*x**2 + 5*y**2 - z**2) (1, 0, 2) >>> diop_ternary_quadratic_normal(34*x**2 - 3*y**2 - 301*z**2) (4, 9, 1)
- sympy.solvers.diophantine.diophantine.ldescent(A, B)[源代码]#
Return a non-trivial solution to \(w^2 = Ax^2 + By^2\) using Lagrange's method; return None if there is no such solution.
- 参数:
A : Integer
B : Integer
non-zero integer
- 返回:
(int, int, int) | None : a tuple \((w_0, x_0, y_0)\) which is a solution to the above equation.
实例
>>> from sympy.solvers.diophantine.diophantine import ldescent >>> ldescent(1, 1) # w^2 = x^2 + y^2 (1, 1, 0) >>> ldescent(4, -7) # w^2 = 4x^2 - 7y^2 (2, -1, 0)
这意味着 \(x = -1, y = 0\) 和 \(w = 2\) 是方程的解 \(w^2 = 4x^2 - 7y^2\)
>>> ldescent(5, -1) # w^2 = 5x^2 - y^2 (2, 1, -1)
工具书类
[R873]《数学解题》,剑桥大学学生数学教材,1998年版。
[R874]Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics. Mathematics of Computation, 72(243), 1417-1441. https://doi.org/10.1090/S0025-5718-02-01480-1
- sympy.solvers.diophantine.diophantine.gaussian_reduce(w: int, a: int, b: int) tuple[int, int] [源代码]#
Returns a reduced solution \((x, z)\) to the congruence \(X^2 - aZ^2 \equiv 0 \pmod{b}\) so that \(x^2 + |a|z^2\) is as small as possible. Here
w
is a solution of the congruence \(x^2 \equiv a \pmod{b}\).This function is intended to be used only for
descent()
.- 参数:
w : int
w
s.t. \(w^2 \equiv a \pmod{b}\)a : int
square-free nonzero integer
b : int
square-free nonzero integer
解释
The Gaussian reduction can find the shortest vector for any norm. So we define the special norm for the vectors \(u = (u_1, u_2)\) and \(v = (v_1, v_2)\) as follows.
\[u \cdot v := (wu_1 + bu_2)(wv_1 + bv_2) + |a|u_1v_1\]Note that, given the mapping \(f: (u_1, u_2) \to (wu_1 + bu_2, u_1)\), \(f((u_1,u_2))\) is the solution to \(X^2 - aZ^2 \equiv 0 \pmod{b}\). In other words, finding the shortest vector in this norm will yield a solution with smaller \(X^2 + |a|Z^2\). The algorithm starts from basis vectors \((0, 1)\) and \((1, 0)\) (corresponding to solutions \((b, 0)\) and \((w, 1)\), respectively) and finds the shortest vector. The shortest vector does not necessarily correspond to the smallest solution, but since
descent()
only wants the smallest possible solution, it is sufficient.实例
>>> from sympy.solvers.diophantine.diophantine import gaussian_reduce >>> from sympy.ntheory.residue_ntheory import sqrt_mod >>> a, b = 19, 101 >>> gaussian_reduce(sqrt_mod(a, b), a, b) # 1**2 - 19*(-4)**2 = -303 (1, -4) >>> a, b = 11, 14 >>> x, z = gaussian_reduce(sqrt_mod(a, b), a, b) >>> (x**2 - a*z**2) % b == 0 True
It does not always return the smallest solution.
>>> a, b = 6, 95 >>> min_x, min_z = 1, 4 >>> x, z = gaussian_reduce(sqrt_mod(a, b), a, b) >>> (x**2 - a*z**2) % b == 0 and (min_x**2 - a*min_z**2) % b == 0 True >>> min_x**2 + abs(a)*min_z**2 < x**2 + abs(a)*z**2 True
工具书类
[R875]Gaussian lattice Reduction [online]. Available: https://web.archive.org/web/20201021115213/http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404
[R876]Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics. Mathematics of Computation, 72(243), 1417-1441. https://doi.org/10.1090/S0025-5718-02-01480-1
- sympy.solvers.diophantine.diophantine.holzer(x, y, z, a, b, c)[源代码]#
简化解决方案 \((x, y, z)\) 方程的 \(ax^2 + by^2 = cz^2\) 具有 \(a, b, c > 0\) 和 \(z^2 \geq \mid ab \mid\) 一个新的简化解 \((x', y', z')\) 这样的话 \(z'^2 \leq \mid ab \mid\) .
The algorithm is an interpretation of Mordell's reduction as described on page 8 of Cremona and Rusin's paper [R877] and the work of Mordell in reference [R878].
工具书类
[R877] (1,2)Cremona, J. E., Rusin, D. (2003). Efficient Solution of Rational Conics. Mathematics of Computation, 72(243), 1417-1441. https://doi.org/10.1090/S0025-5718-02-01480-1
- sympy.solvers.diophantine.diophantine.prime_as_sum_of_two_squares(p)[源代码]#
代表一个质数 \(p\) 作为两个平方的唯一和;这只能在质数与1模4同余的情况下实现。
- 参数:
p :整数
A prime that is congruent to 1 mod 4
- 返回:
(int, int) | None : Pair of positive integers
(x, y)
satisfyingx**2 + y**2 = p
.None if
p
is not congruent to 1 mod 4.- 加薪:
ValueError
If
p
is not prime number
实例
>>> from sympy.solvers.diophantine.diophantine import prime_as_sum_of_two_squares >>> prime_as_sum_of_two_squares(7) # can't be done >>> prime_as_sum_of_two_squares(5) (1, 2)
参考文献
[R879]Representing a number as a sum of four squares, [online], Available: https://schorn.ch/lagrange.html
- sympy.solvers.diophantine.diophantine.sqf_normal(a, b, c, steps=False)[源代码]#
返回 \(a', b', c'\) ,平方自由范式的系数 \(ax^2 + by^2 + cz^2 = 0\) 在哪里 \(a', b', c'\) 是成对素数。如果 \(steps\) 如果为True,则还返回三个元组: \(sq\) , \(sqf\) 和 \((a', b', c')\) 在哪里? \(sq\) 包含的平方因子 \(a\) , \(b\) 和 \(c\) 在移除 \(gcd(a, b, c)\) ; \(sqf\) 包含的值 \(a\) , \(b\) 和 \(c\) 在移除两个 \(gcd(a, b, c)\) 平方因子。
解决方案 \(ax^2 + by^2 + cz^2 = 0\) 可以从 \(a'x^2 + b'y^2 + c'z^2 = 0\) .
实例
>>> from sympy.solvers.diophantine.diophantine import sqf_normal >>> sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11) (11, 1, 5) >>> sqf_normal(2 * 3**2 * 5, 2 * 5 * 11, 2 * 7**2 * 11, True) ((3, 1, 7), (5, 55, 11), (11, 1, 5))
参见
工具书类
[R880]Legendre's Theorem, Legrange's Descent, https://public.csusm.edu/aitken_html/notes/legendre.pdf
Internal Classes#
These classes are intended for internal use in the Diophantine module.
- class sympy.solvers.diophantine.diophantine.DiophantineSolutionSet(symbols_seq, parameters)[源代码]#
Container for a set of solutions to a particular diophantine equation.
The base representation is a set of tuples representing each of the solutions.
- 参数:
symbols : list
List of free symbols in the original equation.
parameters: list
List of parameters to be used in the solution.
实例
Adding solutions:
>>> from sympy.solvers.diophantine.diophantine import DiophantineSolutionSet >>> from sympy.abc import x, y, t, u >>> s1 = DiophantineSolutionSet([x, y], [t, u]) >>> s1 set() >>> s1.add((2, 3)) >>> s1.add((-1, u)) >>> s1 {(-1, u), (2, 3)} >>> s2 = DiophantineSolutionSet([x, y], [t, u]) >>> s2.add((3, 4)) >>> s1.update(*s2) >>> s1 {(-1, u), (2, 3), (3, 4)}
Conversion of solutions into dicts:
>>> list(s1.dict_iterator()) [{x: -1, y: u}, {x: 2, y: 3}, {x: 3, y: 4}]
Substituting values:
>>> s3 = DiophantineSolutionSet([x, y], [t, u]) >>> s3.add((t**2, t + u)) >>> s3 {(t**2, t + u)} >>> s3.subs({t: 2, u: 3}) {(4, 5)} >>> s3.subs(t, -1) {(1, u - 1)} >>> s3.subs(t, 3) {(9, u + 3)}
Evaluation at specific values. Positional arguments are given in the same order as the parameters:
>>> s3(-2, 3) {(4, 1)} >>> s3(5) {(25, u + 5)} >>> s3(None, 2) {(t**2, t + 2)}
- class sympy.solvers.diophantine.diophantine.DiophantineEquationType(equation, free_symbols=None)[源代码]#
Internal representation of a particular diophantine equation type.
- 参数:
equation :
The diophantine equation that is being solved.
free_symbols : list (optional)
The symbols being solved for.
属性
total_degree :
The maximum of the degrees of all terms in the equation
homogeneous :
Does the equation contain a term of degree 0
homogeneous_order :
Does the equation contain any coefficient that is in the symbols being solved for
dimension :
The number of symbols being solved for
- class sympy.solvers.diophantine.diophantine.Univariate(equation, free_symbols=None)[源代码]#
Representation of a univariate diophantine equation.
A univariate diophantine equation is an equation of the form \(a_{0} + a_{1}x + a_{2}x^2 + .. + a_{n}x^n = 0\) where \(a_{1}, a_{2}, ..a_{n}\) are integer constants and \(x\) is an integer variable.
实例
>>> from sympy.solvers.diophantine.diophantine import Univariate >>> from sympy.abc import x >>> Univariate((x - 2)*(x - 3)**2).solve() # solves equation (x - 2)*(x - 3)**2 == 0 {(2,), (3,)}
- class sympy.solvers.diophantine.diophantine.Linear(equation, free_symbols=None)[源代码]#
Representation of a linear diophantine equation.
线性丢番图方程是一个形式方程 \(a_{{1}}x_{{1}} + a_{{2}}x_{{2}} + .. + a_{{n}}x_{{n}} = 0\) 在哪里? \(a_{{1}}, a_{{2}}, ..a_{{n}}\) 是整数常量和 \(x_{{1}}, x_{{2}}, ..x_{{n}}\) 是整数变量。
实例
>>> from sympy.solvers.diophantine.diophantine import Linear >>> from sympy.abc import x, y, z >>> l1 = Linear(2*x - 3*y - 5) >>> l1.matches() # is this equation linear True >>> l1.solve() # solves equation 2*x - 3*y - 5 == 0 {(3*t_0 - 5, 2*t_0 - 5)}
这里=-3 t_0 - 5 and y = -2 图0-5
>>> Linear(2*x - 3*y - 4*z -3).solve() {(t_0, 2*t_0 + 4*t_1 + 3, -t_0 - 3*t_1 - 3)}
- class sympy.solvers.diophantine.diophantine.BinaryQuadratic(equation, free_symbols=None)[源代码]#
Representation of a binary quadratic diophantine equation.
A binary quadratic diophantine equation is an equation of the form \(Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0\), where \(A, B, C, D, E, F\) are integer constants and \(x\) and \(y\) are integer variables.
实例
>>> from sympy.abc import x, y >>> from sympy.solvers.diophantine.diophantine import BinaryQuadratic >>> b1 = BinaryQuadratic(x**3 + y**2 + 1) >>> b1.matches() False >>> b2 = BinaryQuadratic(x**2 + y**2 + 2*x + 2*y + 2) >>> b2.matches() True >>> b2.solve() {(-1, -1)}
工具书类
[R881]Methods to solve Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, [online], Available: https://www.alpertron.com.ar/METHODS.HTM
[R882]Solving the equation ax^2+ bxy + cy^2 + dx + ey + f= 0, [online], Available: https://web.archive.org/web/20160323033111/http://www.jpr2718.org/ax2p.pdf
- class sympy.solvers.diophantine.diophantine.InhomogeneousTernaryQuadratic(equation, free_symbols=None)[源代码]#
Representation of an inhomogeneous ternary quadratic.
No solver is currently implemented for this equation type.
- class sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadraticNormal(equation, free_symbols=None)[源代码]#
Representation of a homogeneous ternary quadratic normal diophantine equation.
实例
>>> from sympy.abc import x, y, z >>> from sympy.solvers.diophantine.diophantine import HomogeneousTernaryQuadraticNormal >>> HomogeneousTernaryQuadraticNormal(4*x**2 - 5*y**2 + z**2).solve() {(1, 2, 4)}
- class sympy.solvers.diophantine.diophantine.HomogeneousTernaryQuadratic(equation, free_symbols=None)[源代码]#
Representation of a homogeneous ternary quadratic diophantine equation.
实例
>>> from sympy.abc import x, y, z >>> from sympy.solvers.diophantine.diophantine import HomogeneousTernaryQuadratic >>> HomogeneousTernaryQuadratic(x**2 + y**2 - 3*z**2 + x*y).solve() {(-1, 2, 1)} >>> HomogeneousTernaryQuadratic(3*x**2 + y**2 - 3*z**2 + 5*x*y + y*z).solve() {(3, 12, 13)}
- class sympy.solvers.diophantine.diophantine.InhomogeneousGeneralQuadratic(equation, free_symbols=None)[源代码]#
Representation of an inhomogeneous general quadratic.
No solver is currently implemented for this equation type.
- class sympy.solvers.diophantine.diophantine.HomogeneousGeneralQuadratic(equation, free_symbols=None)[源代码]#
Representation of a homogeneous general quadratic.
No solver is currently implemented for this equation type.
- class sympy.solvers.diophantine.diophantine.GeneralSumOfSquares(equation, free_symbols=None)[源代码]#
Representation of the diophantine equation
\(x_{1}^2 + x_{2}^2 + . . . + x_{n}^2 - k = 0\).
细节
When \(n = 3\) if \(k = 4^a(8m + 7)\) for some \(a, m \in Z\) then there will be no solutions. Refer [R883] for more details.
实例
>>> from sympy.solvers.diophantine.diophantine import GeneralSumOfSquares >>> from sympy.abc import a, b, c, d, e >>> GeneralSumOfSquares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345).solve() {(15, 22, 22, 24, 24)}
By default only 1 solution is returned. Use the \(limit\) keyword for more:
>>> sorted(GeneralSumOfSquares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345).solve(limit=3)) [(15, 22, 22, 24, 24), (16, 19, 24, 24, 24), (16, 20, 22, 23, 26)]
工具书类
[R883] (1,2)Representing an integer as a sum of three squares, [online], Available: https://www.proofwiki.org/wiki/Integer_as_Sum_of_Three_Squares
- class sympy.solvers.diophantine.diophantine.GeneralPythagorean(equation, free_symbols=None)[源代码]#
Representation of the general pythagorean equation, \(a_{1}^2x_{1}^2 + a_{2}^2x_{2}^2 + . . . + a_{n}^2x_{n}^2 - a_{n + 1}^2x_{n + 1}^2 = 0\).
实例
>>> from sympy.solvers.diophantine.diophantine import GeneralPythagorean >>> from sympy.abc import a, b, c, d, e, x, y, z, t >>> GeneralPythagorean(a**2 + b**2 + c**2 - d**2).solve() {(t_0**2 + t_1**2 - t_2**2, 2*t_0*t_2, 2*t_1*t_2, t_0**2 + t_1**2 + t_2**2)} >>> GeneralPythagorean(9*a**2 - 4*b**2 + 16*c**2 + 25*d**2 + e**2).solve(parameters=[x, y, z, t]) {(-10*t**2 + 10*x**2 + 10*y**2 + 10*z**2, 15*t**2 + 15*x**2 + 15*y**2 + 15*z**2, 15*t*x, 12*t*y, 60*t*z)}
- class sympy.solvers.diophantine.diophantine.CubicThue(equation, free_symbols=None)[源代码]#
Representation of a cubic Thue diophantine equation.
A cubic Thue diophantine equation is a polynomial of the form \(f(x, y) = r\) of degree 3, where \(x\) and \(y\) are integers and \(r\) is a rational number.
No solver is currently implemented for this equation type.
实例
>>> from sympy.abc import x, y >>> from sympy.solvers.diophantine.diophantine import CubicThue >>> c1 = CubicThue(x**3 + y**2 + 1) >>> c1.matches() True
- class sympy.solvers.diophantine.diophantine.GeneralSumOfEvenPowers(equation, free_symbols=None)[源代码]#
Representation of the diophantine equation
\(x_{1}^e + x_{2}^e + . . . + x_{n}^e - k = 0\)
where \(e\) is an even, integer power.
实例
>>> from sympy.solvers.diophantine.diophantine import GeneralSumOfEvenPowers >>> from sympy.abc import a, b >>> GeneralSumOfEvenPowers(a**4 + b**4 - (2**4 + 3**4)).solve() {(2, 3)}