【问题标题】:Why doesn't the SageMath function find_local_maximum work for me in Python?为什么 SageMath 函数 find_local_maximum 在 Python 中对我不起作用?
【发布时间】:2020-12-16 20:07:10
【问题描述】:

我正在尝试使用写在帖子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'

【问题讨论】:

标签: python jupyter-notebook spyder nameerror sage


【解决方案1】:

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

这是一个虚拟包,这样做您并没有安装 SageMath。

使 Sage 库 pip 可安装的工作正在进行中,跟踪在

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2019-01-06
    • 2017-09-23
    • 2019-10-06
    相关资源
    最近更新 更多