python


Recursive integration


This is a simplified version of the actual question I am working on.
Suppose that we have a sequence of (mathematical) functions, f_0, f_1, f_2, ..., such that
We fix a function f_0, say, f_0(x)=x. In this simple case, f_n(x) = 1/((n+1)!) x^{n+1}. Is there an elegant and efficient way to program this recursively, so that Python returns the function f_n given an arbitrary function f_0?
I started out trying to return f_2, but this already failed:
from scipy.integrate import quad
f_0=lambda x: x
f_1=lambda x: quad(f_0,0,x)
f_2=lambda x: quad(f_1,0,x)
returns the error
error: Supplied function does not return a valid float.
after trying to evaluate e.g. f_2(3).
There is nothing wrong with chaining numeric integrals. The problem is what quad returns:
Returns:
y : float
The integral of func from a to b.
abserr : float
An estimate of the absolute error in the result.
So you get two return values but only the first one is interesting (unless you want to propagate the error over all integrals, but I don't know how to do that). The second integral complains because the function returns a tuple of two values instead of only one scalar.
The following small modification will fix the error by selecting the first of quad's return values:
from scipy.integrate import quad
f_0=lambda x: x
f_1=lambda x: quad(f_0,0,x)[0]
f_2=lambda x: quad(f_1,0,x)[0]
For the sake of completeness, here is a recursive definition for iterating n times:
def integrate_a_lot(f, n):
for _ in range(n):
f = lambda x, f=f: quad(f, 0, x)[0]
return f
f_2 = integrate_a_lot(f_0, 2)
f_42 = integrate_a_lot(f_0, 42)
you can use sympy: http://docs.sympy.org/dev/modules/integrals/integrals.html
something like
x = sympy.Symbol('x')
def f_0(m_x):
return m_x
def f_n(f_0,n,m_x):
if n==0:
return f_0(m_x)
return sympy.integrate(f_n(f_0(m_x),n-1,m_x))
then call it with f_n(f_0,1,x)

Related Links

Function that takes 3 list arguments and returns all the combinations [duplicate]
how to read files with special characters in python
Using the Python Request Library to get Google Oauth2 tokens
(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable
How to use multidatabase across multiproject in django?
SSLHandshakeError when connecting to Google Analytics using Google APIs Client Library for Python
Pandas Pivot Table Display in ReportLab
python multiprocessing behaviour

Categories

HOME
caching
ember.js
filterrific
freepascal
visual-studio-2013
label
playframework
survival-analysis
specflow
defragmentation
spring-cloud-contract
google-plus
future
cloudflare
lenskit
opera-mini
bar-chart
android-toolbar
aptana
google-people
styles
cruisecontrol.net
iup
novnc
hash-collision
widevine
upnp
nesc
red-black-tree
consumer
hotmail
ifstream
node-horseman
boilerplate
nlb
pmwiki
autocompletetextview
parse-android-sdk
ddms
asmx
prototypejs
rdw
walmart-electrode
drawingarea
chunked-encoding
webdatagrid
onresume
payu
aescryptoserviceprovider
tomcat5
foxit
unsatisfiedlinkerror
asp.net-mvc-partialview
magento-1.4
android-viewholder
twython
vine
ubercart
apache-mina
guzzle6
hiera
virus
cpan
temp-tables
gyroscope-framework
grunt-contrib-concat
yahoo-boss-api
emma
bullet
planetary.js
mbox
unison
ksoap2
windows-mobile-6
leptonica
javascriptmvc
grunt-contrib-compass
prng
git-filter-branch
subtract
unions
hinstance
ajax.beginform
cookieless
smooth
.app
recordset
camtasia
gwt-2.2-celltable
mvvm-foundation
system-codedom-compiler

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App