(已解决)magma代码重写为python遇到问题
本帖最后由 xqyqx 于 2024-2-21 18:34 编辑在网上找到了一段magma代码,想把它重写为Python,可是重写后的代码计算完全错误,不知问题出在何处
解决了,映射点出错了
magma代码:
w := 0;
n := 4+w+w^2;
printf "N=%o\n",4+w+w^2;
R<x,y,z> := RationalFunctionField(Rationals(),3);
problem := ((x/(y+z) + y/(x+z) + z/(x+y)) - 6) ;
// first note that we know a point after some computation (-1,4,11) that
// works but has a negative coordinate, the following function returns 0, which means that
// (x/(y+z) + y/(x+z) + z/(x+y)) - 4 = 0 (just put the -4 in the other side)
Evaluate(problem,[(w^2+1)*(3*w^3+8*w^2+14*w+11),-(w^2+2*w+2)*(3*w^3+w^2+7*w-2),w^6+3*w^5+11*w^4+17*w^3+20*w^2+12*w-1]);
// after the previous returned 0 , we know the point fits, we continue.
// we multiply by all the denominators of "problem" to get a polynomials
problem*Denominator(problem);
// we obtain a polynomial without denominators x^3 - 3*x^2*y - 3*x^2*z - 3*x*y^2 - 5*x*y*z - 3*x*z^2 + y^3 - 3*y^2*z - 3*y*z^2 + z^3
// We see is cubic, three variables, and everyterm has the same degree (3) , therefore this is a cubic
// homogeneous curve,we know there is a point which is not the solution we want
// the point (-1,4,11) fits in the original "problem" so it should fit in this new curve without denominators too (since no denominator becomes 0)
// We transform this equation to a "curve" in Projecive space of dimension 2
P2<x,y,z> := ProjectiveSpace(Rationals(),2);
C := Curve(P2,x^3 - (n-1)*x^2*y - (n-1)*x^2*z - (n-1)*x*y^2 - (2*n-3)*x*y*z - (n-1)*x*z^2 + y^3 - (n-1)*y^2*z - (n-1)*y*z^2 + z^3);
// fit the point to the curve C (no error is returned)
Pt := C![(w^2+1)*(3*w^3+8*w^2+14*w+11),-(w^2+2*w+2)*(3*w^3+w^2+7*w-2),w^6+3*w^5+11*w^4+17*w^3+20*w^2+12*w-1];
// Since all cubic homogeneous curve with at least one point define an elliptc curve, we can transform
// this curve C to an elliptc curve form and just like in cryptography, we will add this known point (mapped to the corresponded curve)
// with itself until we get only positive coordinates and go back to C (original Problem)
// Below, E is the curve, f is the map that maps Points f:C -> E(C is our original curve without denominators, both curves C,E are equivalent
// but in E we can "Add points" to get another point of E.
// and with f^-1 we can return to the point of C which is our original solution
E,f := EllipticCurve(C);
//g is the inverse g:E->C, f:C->E so g(f([-1,4,11]))=[-1,4,11]
g := f^-1;
// We try adding the known point Pt=[-1,4,11] mapped to E, 2..100 times
// to see if when mapped back the added point to C gives positive coordinates
//, this is 2*Pt, 3*Pt, ...., 100*Ptand then mapping back to C all these.
for n:= 1 to 100 do
// we calculate n times the point of C, known [-1,4,11] but mapped (via f) inside E (where we can do the "n times")
nPt_inE:=n*f(Pt);
// we take this point on E back to C via f^-1(which we renamed as g)
nPt_inC:=g(nPt_inE);
//We obtain each coordinate of this point to see if is our positive solution,
// here MAGMA scales automatically the point such as Z is one always 1,
// so it puts the same denominators in X,Y, so numerators of X,Y are our
//solutions and denominator our Z,think ofP=(a/c,b/c,1) then c*P=(a,b,c)
X := Numerator(nPt_inC);
Y := Numerator(nPt_inC);
Z := Denominator(nPt_inC);
// We check the condition for our original problem.
if ((X gt 0) and (Y gt 0)) then
printf "X=%o\nY=%o\nZ=%o\n",X,Y,Z;
printf("GOT IT!!! x=apple, y=banana, z=pineapple, check the above solution\n");
break;
else
//printf "Nee, some coordinate was negative above, I keep in the loop\n\n";
end if;
end for;
// We check the solution fits in the original problem
if Evaluate(problem, ) eq 0 then
printf "I evaluated the point to the original problem and yes, it worked!\n";
else
printf "Mmm this cannot happen!\n";
end if;
重写python代码:
from sage.all import *
from fractions import Fraction
def multiply_list_values_1(lst, number):
for i in range(len(lst)):
lst *= number
return lst
# 定义有理数域
R = RationalField()
w = 0
n = 4+w+w**2
# 定义射影平面 P2
P2 = ProjectiveSpace(2, R)
x, y, z = P2.coordinate_ring().gens()
# 定义曲线 C
#C = Curve(x**3 - (n-1)*x**2*y - (n-1)*x**2*z - (n-1)*x*y**2 - (2*n-3)*x*y*z - (n-1)*x*z**2 + y**3 - (n-1)*y**2*z - (n-1)*y*z**2 + z**3)
# 定义点 Pt
Pt =
# 创建椭圆曲线 E
E = EllipticCurve_from_cubic(x**3 - (n-1)*x**2*y - (n-1)*x**2*z - (n-1)*x*y**2 - (2*n-3)*x*y*z - (n-1)*x*z**2 + y**3 - (n-1)*y**2*z - (n-1)*y*z**2 + z**3,[(w**2+1)*(3*w**3+8*w**2+14*w+11),-(w**2+2*w+2)*(3*w**3+w**2+7*w-2),w**6+3*w**5+11*w**4+17*w**3+20*w**2+12*w-1])
g = E.inverse()
# 循环计算
for n in range(1, 101):
# 在曲线 C 上计算点 nPt
nPt_inC = multiply_list_values_1(Pt,n)
# 映射点 nPt 到椭圆曲线 E 上
nPt_inE = E(nPt_inC)
print(nPt_inC)
# 逆映射点 nPt_inE 回曲线 C 上
nPt_inC = g(nPt_inE)
# 提取坐标
X = nPt_inC.numerator()
Y = nPt_inC.numerator()
Z = nPt_inC.denominator()
print("X =", X)
print("Y =", Y)
print("Z =", Z)
# 检查条件
if X > 0 and Y > 0:
print("GOT IT!!! x=apple, y=banana, z=pineapple, check the above solution")
break
else:
print("Nee, some coordinate was negative above, I keep in the loop\n")
# 在原始问题上评估点
problem = x**3 - (n-1)*x**2*y - (n-1)*x**2*z - (n-1)*x*y**2 - (2*n-3)*x*y*z - (n-1)*x*z**2 + y**3 - (n-1)*y**2*z - (n-1)*y*z**2 + z**3
if problem(X, Y, Z) == 0:
print("I evaluated the point to the original problem and yes, it worked!")
else:
print("Mmm this cannot happen!") 厉害厉害厉害厉害厉害厉害 怎么解决的?哪里出问题
页:
[1]