當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sympy.harmonic()用法及代碼示例

借助於sympy.harmonic()方法,我們可以在SymPy中找到調和數。

諧波(n)

第n次諧波數由-\operatorname{H}_{n} = 1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}

用法: harmonic(n) 

參數:
n -它表示要計算的諧波數。

返回:返回nth諧波數。

範例1:

# import sympy  
from sympy import * 
  
n = 7
print("Value of n = {}".format(n)) 
   
# Use sympy.harmonic() method  
nth_harmonic = harmonic(n)   
      
print("Value of nth harmonic number : {}".format(nth_harmonic))  

輸出:

Value of n = 7
Value of nth harmonic number : 363/140
諧波(n,m)

m階的n次廣義諧波數由-\operatorname{H}_{n, m} = \sum_{k=1}^{n} \frac{1}{k^m}

用法: harmonic(n, m) 

參數:
n -它表示要計算的諧波數。
m -它表示諧波數的順序。
返回:返回m階的n次諧波。

範例2:

# import sympy  
from sympy import * 
  
n = 5
m = 2
print("Value of n = {} and m = {}".format(n, m)) 
   
# Use sympy.harmonic() method  
nth_harmonic_poly = harmonic(n, m)   
      
print("The nth harmonic number of order m : {}".format(nth_harmonic_poly))  

輸出:

Value of n = 5 and m = 2
The nth harmonic number of order m : 5269/3600


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.harmonic() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。