尝试使用CoCalc(sage)来绘制需要模的函数的解

2024-10-06 09:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码和错误消息。我正在做的事情似乎弄乱了hb函数中的%函数。我真的不知道该怎么解决。有什么想法吗?你知道吗

def h(n):
    if (n % 4 >= 0) and (n % 4 < 1):
            k = 1
    else:
        k = 0
    return k

def hb(n):
    if (((n/4) % 2) >= 0) and (((n/4) % 2) < 1):
        k = -1*h(n)
    else:
        k = h(n)
    return k

def dalembert(y,t):
    x = 0.5*hb(y-t)+0.5*hb(y+t) 
    return x 

import numpy as np

box1 = np.array([1,2,6,10,20])

for i in range(len(box1)):
    g=Graphics()
    g += plot(dalembert(x,box1[i]), (x, 0, 4), color='blue')
    g.show()

第18-21行出错 回溯(最近一次呼叫): 文件“/cocalc/lib/python2.7/site-packages/smc\u sagews/sage_服务器.py,第1188行,在execute中 flags=compile(flags)在名称空间中,局部变量 文件“”,第3行,在 文件“”,第2行,在dalembert中 文件“”,第2行,单位为hb 文件“sage/structure”/元素.pyx,第1925行结构元素.Element.mod(build/cythonized/sage/structure/Element.c:13956) 回归强制_模型.bin\u op(左,右,mod) 文件“sage/structure”/强制.pyx,第1182行结构强制.importionModel\u缓存_地图.bin\u op(build/cythonized/sage/structure/improve.c:9827) 返回PyObject\u CallObject(op,xy) 文件“sage/structure”/元素.pyx,第1923行结构元素.Element.mod(build/cythonized/sage/structure/Element.c:13921) 返回(左)。mod(右) 文件“sage/structure”/元素.pyx“,第1958行,in结构元素.Element.mod(build/cythonized/sage/structure/Element.c:14242) 引发bin\u op\u异常(“%”,self,other) TypeError:不支持%:“Symbolic Ring”和“Symbolic Ring”的操作数父级


Tags: 文件buildmod元素returndefelementstructure
1条回答
网友
1楼 · 发布于 2024-10-06 09:58:37

尝试替换 g += plot(dalembert(x,box1[i]), (x, 0, 4), color='blue') 类似 function d0(y): return dalembert(y,box1[i]); g += plot(d0, (x, 0, 4), color='blue')

问题是当你在你的代码中写dalembert(x,box1[i])的时候,它首先被评估,甚至在一个绘图中使用它之前,它被评估为x一个符号,它打破了其他东西。。。你知道吗

相关问题 更多 >