Implementieren Sie sum in Python

Versuchen Sie, die Summenformel zu implementieren

\sum_{i=m}^{n} f(x) = f(m) + f(m+1) + \cdots + f(n) \\

sigma.py


def sigma(m, n, func, s = 0) :
    if m > n: return s
    return sigma(m + 1, n, func, s + func(m))

Anwendungsbeispiel

sigma_test.py


print(sigma(1, 10, lambda x : x))
55

print(sigma(1, 3, lambda x : 3 * 5 ** (x - 1)))
93

Ich habe ein geeignetes Problem im Web gefunden und eingegeben, aber es scheint richtig zu funktionieren. Möglicherweise gibt es einen effizienteren Schreibstil, einen modischeren Schreibstil oder etwas, das bereits verfügbar ist.

Nachtrag

Da es eine mathematische Funktionsbibliothek namens NumPy gibt, scheinen die meisten Formeln vollständig zu sein. .. ..

Einführung in die Python Numerical Calculation Library NumPy http://rest-term.com/archives/2999/

Recommended Posts

Implementieren Sie sum in Python
Implementieren Sie XENO mit Python
Implementieren Sie Traceroute in Python 3
Implementieren Sie Naive Bayes in Python 3.3
Implementieren Sie Redis Mutex in Python
Implementieren Sie die Erweiterung in Python
Implementieren Sie schnelles RPC in Python
Implementieren Sie den Dijkstra-Algorithmus in Python
Implementieren Sie den Slack Chat Bot in Python
Implementieren Sie das Stacking-Lernen in Python [Kaggle]
Implementieren Sie die Funktion power.prop.test von R in Python
Implementieren Sie das Singleton-Muster in Python
Implementieren Sie die REST-API schnell in Python
Quadtree in Python --2
Python in der Optimierung
CURL in Python
Metaprogrammierung mit Python
Python 3.3 mit Anaconda
Geokodierung in Python
SendKeys in Python
Metaanalyse in Python
Unittest in Python
Epoche in Python
Deutsch in Python
DCI in Python
Quicksort in Python
nCr in Python
N-Gramm in Python
Programmieren mit Python
Plink in Python
Konstante in Python
FizzBuzz in Python
SQLite in Python
Schritt AIC in Python
LINE-Bot [0] in Python
CSV in Python
Reverse Assembler mit Python
Reflexion in Python
Konstante in Python
nCr in Python.
Format in Python
Scons in Python 3
Puyopuyo in Python
Python in Virtualenv
PPAP in Python
Quad-Tree in Python
Reflexion in Python
Chemie mit Python
Hashbar in Python
DirectLiNGAM in Python
LiNGAM in Python
In Python reduzieren
In Python flach drücken
Ich habe versucht, PLSA in Python zu implementieren
Implementieren Sie __eq__ usw. generisch in der Python-Klasse
Ich habe versucht, Permutation in Python zu implementieren
Implementieren Sie den FIR-Filter in Python und C.