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

how to extract some text by use lxml?
dynamically adding functions to a Python module
Scaling the y-axis with Matplotlib in Python
Shortening a oft-used code segment for testing a return value in Python
What is the best way to internationalize a Python app with multiple i18n domains?
IPv6 decoder for pcapy/impacket
Using paver and nose together with an atypical directory structure
Load an existing many-to-many table relation with sqlalchemy
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python?
Best Python templating library to facilitate code generation [closed]
Code samples for Django + SWFUpload?
CMake output name for dynamic-loaded library?
Python: undo write to file
Python: “1-2-3-4” to [1, 2, 3, 4]
Problem sub-classing BaseException in Python
How can I render a tree structure (recursive) using a django template?

Categories

HOME
symfony
app-inventor
sass
voip
bower
parse-server
transparent
mainframe
heap-memory
hyperledger-fabric
sudo
shipping
wicket
google-shopping
ctypes
nuxt.js
symfony-forms
cloudflare
lenskit
nsmenuitem
onesignal
metis
upnp
reply
gzip
invoke-command
devforce
xor
smart-mobile-studio
geopy
boilerplate
nodeclipse
axis-labels
active-model-serializers
fax
cross-entropy
ttcn
estimation
lumen-5.3
jags
mouseclick-event
openh264
iphone-developer-program
collapsingtoolbarlayout
communication-protocol
date-range
bettercms
picasa
bacnet
remoteapp
supportmapfragment
financial
post-processing
asp.net-mvc-2
approval-tests
loose-typing
getrusage
hyperthreading
hornetq
ninject.web.mvc
mirrorlink
futuretask
sevenzipsharp
pyrocms
ffserver
pylearn
jazz
google-plus-one
jquery-autocomplete
microblaze
vcl
eclipse-classpath
ember-addon
dache
cppdepend
phpdocx
factors
cling
logentries
java.util.date
manage.py
symphony-cms
zend-lucene
hobo
jquery-selectbox
hinstance
hashalgorithm
dcpu-16
ets
server-error
nvelocity
pantheios
drawtext
sitemappath
task-management
clients
configurable

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