Python 同形性鉴别

Python 同形性鉴别,python,sympy,Python,Sympy,我试图用Symphy来区分以下等式: log(n)**k import math, sympy from sympy.abc import x, y, n, k print(sympy.diff(math.pow(math.log(n, 2), k), n)) 但是我从SymPy那里得到了无法将表达式转换为float的错误。 我做错了什么 runfile('C:/Users/towis/.spyder-py3/temp.py', wdir='C:/Users/towis/.spyder-py3

我试图用Symphy来区分以下等式:

log(n)**k

import math, sympy
from sympy.abc import x, y, n, k
print(sympy.diff(math.pow(math.log(n, 2), k), n))
但是我从SymPy那里得到了
无法将表达式转换为float
的错误。 我做错了什么

runfile('C:/Users/towis/.spyder-py3/temp.py', wdir='C:/Users/towis/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-19-3728a7ec31a4>", line 1, in <module>
    runfile('C:/Users/towis/.spyder-py3/temp.py', wdir='C:/Users/towis/.spyder-py3')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/towis/.spyder-py3/temp.py", line 6, in <module>
    print(sympy.diff(math.pow(math.log(n,2), k), n))

  File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\expr.py", line 226, in __float__
    raise TypeError("can't convert expression to float")

TypeError: can't convert expression to float
runfile('C:/Users/towis/.spyder-py3/temp.py',wdir='C:/Users/towis/.spyder-py3')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('C:/Users/towis/.spyder-py3/temp.py',wdir='C:/Users/towis/.spyder-py3')
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\utils\site\site customize.py”,第866行,在runfile中
execfile(文件名、命名空间)
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/towis/.spyder-py3/temp.py”,第6行,在
打印(sympy.diff(math.pow(math.log(n,2,k),n))
文件“C:\ProgramData\Anaconda3\lib\site packages\sympy\core\expr.py”,第226行,在浮点数中__
raise TypeError(“无法将表达式转换为浮点”)
TypeError:无法将表达式转换为浮点

正如@ForceBru所说,使用SymPy函数:

>>> diff(log(n, 2)**k, n)
k∗(log(n)/log(2))∗∗k/(n∗log(n))

您可以通过以下方式更正提供的代码:

import sympy
from sympy.abc import x, y, n, k
print(sympy.diff(sympy.Pow(sympy.log(n, 2),k), n))

请给出完整的错误消息。当然,现在开始编辑:编辑问题您很可能从Python的
math
模块中得到此错误,即
math.pow
&
math.log
计算幂函数和对数的相应浮点值。它们需要浮点数作为输入。有什么办法吗?@ToTom,是的,
sympy
提供了自己的对数函数,您可以使用简单的
a**b
进行幂运算。