区分SymPy

时间:2017-08-24 14:02:48

标签: python sympy

我正在尝试使用SymPy来区分以下等式:

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收到can't convert expression to 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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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