为什么SageMath函数find_local_maximum在Python中对我不起作用?

时间:2020-08-27 21:38:14

标签: python jupyter-notebook spyder nameerror sage

我正在尝试使用帖子How to find the approximate basic period or GCD of a list of numbers中编写的SageMath代码。此代码可在CoCalc中工作,但无法在IDE中的Python中运行它。我已经导入了numpy和sagemath,并根据需要引用它们来定义数学函数,但是我无法使find_local_maximum函数正常工作。见下文。

代码:

import numpy as np
import sagemath as sm

def mean_x(factor, values):
    return sum([np.cos(2*np.pi*v/factor) for v in values])/len(values)

def mean_y(factor, values):
    return sum([np.sin(2*np.pi*v/factor) for v in values])/len(values)

def calculatePeriodAppeal(factor, values):
    mx = mean_x(factor, values)
    my = mean_y(factor, values)
    appeal = np.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*np.atan2(my,mx)/(2*np.pi).n()
    err = 1-np.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 - np.sqrt((mx-1)**2+my**2)/2
    return appeal

testSeq = [552,827,275,809,534] 
gcd = calculateGCDAppeal(x, testSeq)
agcd = sm.find_local_maximum(gcd,20,30)
print(agcd)
np.plot(gcd,(x, 20,30))

错误:

Traceback (most recent call last):

  File "<ipython-input-163-fecb5397afab>", line 31, in <module>
    agcd = sm.find_local_maximum(gcd,20,30)

AttributeError: module 'sagemath' has no attribute 'find_local_maximum'

1 个答案:

答案 0 :(得分:1)

您似乎运行了pip install sagemath,它会安装 PyPI的“ sagemath”软件包:

这是一个虚拟软件包,因此您没有安装SageMath。

使Sage库可点入安装的工作正在进行中,

当前,访问SageMath功能实际上需要 安装SageMath,而不是PyPI的虚拟“ sagemath”软件包。

如果目标是与Spyder一起使用,请参见答案

建议遵循最简单的路线, 通过Conda安装Sage和Spyder。