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 install python sipconfig module on win7
Selenium timeout exception even after loop is executed
Converting a unique columns into SFrame headers with corresponding values
ttk progress bar freezing
pandas replace part of a column with another column
speeding up many accesses to pandas dataframe
Performing Arithmetic operations on nested dataframe containing a list
C++ Back-propagation on a Graph; heterogeneous vectors?
How to extract only one capture group using regex for pandas dataframe?
Error with “__str__ returned non-string (type int)”
Pandas: how to change a Series into nested list by index
SciPy ImportError
How to find max values?
I'm looking to print the length of the longest and shortest words but am unsure where I'm going wrong
Python and nesting classes for structure
AttributeError list object has no attribute add

Categories

HOME
ibm-watson-cognitive
admin-on-rest
mql4
project-management
npm
jsviews
scipy
blockchain
warnings
jasmine
puppet
json-ld
travis-ci
transparent
ios10
value
rebol
spring-cloud-contract
i2c
octopus-deploy
wicket
cs-cart
lstm
uisplitviewcontroller
opera-mini
rhapsody
pyephem
internet-explorer-8
jsonserializer
tortoisegit
jconsole
statusbar
rails-activerecord
prediction
servicemix
jquery-scrollify
spring-test
pdfsharp
version-numbering
spring-data-neo4j
google-api-dotnet-client
bitcoin-testnet
expand
hawtio
transient
sql-like
installshield-2012
google-account
dojox.mobile
lftp
appstore-approval
amazon-machine-learning
applepayjs
mbaas
fontconfig
unity3d-editor
gmt
bettercms
polar-coordinates
picasa
vst
dojox.charting
squirrel
codesign
proxygen
fill
yoothemes
handlebars.java
web-essentials
galaxy
license-key
affix
application-loader
sem
rx-groovy
cpu-cores
lines-of-code
mdm-zinc
virus
magento-1.12
low-level
start-job
c++-actor-framework
rhel5
kendo-mvvm
odftoolkit
grunt-contrib-concat
grunt-express
google-admin-audit-api
humanizer
bullet
ssms-addin
pic24
dynamic-binding
dynamics-ax-2009
shiva3d
graph-coloring
phpsh
visual-web-gui
msgbox
selectonemenu
hashalgorithm
text-size
newsstand-kit
goliath
paintcomponent
noir
gethashcode
linfu
microsoft.ink
odbc-sql-server-driver
evb
gears
weborb
rd

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