如何翻译此 SageMath 代码以在 Python 中运行? [英] How can I translate this SageMath code to run in Python?
问题描述
我该怎么做才能让这段代码在 Python 中运行?我从 此处.如果我在 CoCalc 上的 SageMath 工作簿中运行它,它无需调整即可工作.导入 sage 和 numpy 后在 Python 中运行时,出现各种名称和属性错误.
What do I have to do to make this code run in Python? I got the code from here. If I run it in a SageMath workbook on CoCalc, it works with no adjustment needed. When I run it in Python after importing sage and numpy, I get various name and attribute errors.
def mean_x(factor, values):
return sum([cos(2*pi*v/factor) for v in values])/len(values)
def mean_y(factor, values):
return sum([sin(2*pi*v/factor) for v in values])/len(values)
def calculatePeriodAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = sqrt(mx^2+my^2)
return appeal
def calculateBestLinear(factor, values):
mx = mean_x(factor, values).n()
my = mean_y(factor, values).n()
y0 = factor*atan2(my,mx)/(2*pi).n()
err = 1-sqrt(mx^2+my^2).n()
return [factor*x + y0, err]
def calculateGCDAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = 1 - sqrt((mx-1)^2+my^2)/2
return appeal
testSeq = [0,125,211,287,408,520,650,735,816,942,1060]
gcd = calculateGCDAppeal(x,testSeq)
agcd = find_local_maximum(gcd,2,100)
print(agcd)
plot(gcd,(x,2,100))
推荐答案
第一个错误是
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
一旦修复,就会有各种缺失的导入.
Once that is fixed, there are various missing imports.
使用 Sage 的 import_statements
函数找到每一个.
Find each of them with Sage's import_statements
function.
sage: import_statements(x)
from sage.calculus.predefined import x
sage: import_statements(sum)
from sage.misc.functional import symbolic_sum
sage: import_statements(cos)
from sage.functions.trig import cos
sage: import_statements(sin)
from sage.functions.trig import sin
最后的代码可能是这样的:
In the end the code might look like:
from sage.calculus.predefined import x
from sage.misc.functional import symbolic_sum as sum
from sage.functions.trig import cos, sin
def mean_x(factor, values):
return sum(cos(2*pi*v/factor) for v in values) / len(values)
def mean_y(factor, values):
return sum(sin(2*pi*v/factor) for v in values) / len(values)
def calculatePeriodAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = sqrt(mx**2 + my**2)
return appeal
def calculateBestLinear(factor, values):
mx = mean_x(factor, values).n()
my = mean_y(factor, values).n()
y0 = factor*atan2(my, mx)/(2*pi).n()
err = 1-sqrt(mx**2 + my**2).n()
return [factor*x + y0, err]
def calculateGCDAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = 1 - sqrt((mx - 1)**2 + my**2)/2
return appeal
testSeq = [0, 125, 211, 287, 408, 520, 650, 735, 816, 942, 1060]
gcd = calculateGCDAppeal(x, testSeq)
agcd = find_local_maximum(gcd, 2, 100)
print(agcd)
plot(gcd, (x, 2, 100))
在 Sage 中搜索import_statements",那里有一些答案更详细地讨论这个主题.
Search Ask Sage for "import_statements", some answers there go into more detail on this topic.
这篇关于如何翻译此 SageMath 代码以在 Python 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!