2017-08-24 60 views
-1

区分我试图用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 
+0

请给整个错误消息。 –

+0

当然,在这里你去 编辑:编辑问题 – ToTom

+3

你很可能从Python的'数学'模块得到这个错误,因为'math.pow'&'math.log'计算幂函数的相应浮点数_values_和对数。他们需要_floats_作为输入。 – ForceBru

回答

0

由于@ForceBru说,使用SymPy功能:

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