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

Filter in template to arrange data specifically in django
Retrieving data using Beautiful Soup
How can I get this series to a pandas dataframe?
Python - How to create a folder with a user entered name?
python win32com powerpoint ribbon xml
Read and write data to new file Python
How can I create a figure with optimal resolution for printing?
creating a for loop where xpath increases
Beautifulsoup - scraping everything but table data
Django-Haystack returns no results
How did I end up creating list from dataframe in Python Spark?
pandas: create single size & sum columns after group by multiple columns
How to check the shape of multiple arrays contained in a list?
Understanding net_surgery in caffe
Plotting a heatmap of temperatures
JWT Signature Validation Failed after deploying new backend version to server

Categories

HOME
sql-server
cntk
sass
answer-set-programming
vhdl
wsdl
nuxeo
fparsec
puppet
label
angular2-directives
ll
gspread
tumblr
spring-cloud-contract
oclint
flexbox
game-physics
x-frame-options
cosmicmind
phoenix
postmessage
ida
factor-analysis
off-canvas-menu
zoho
styles
google-ima
rworldmap
auditing
net-snmp
solidworks
dpi
blackboard
rhomobile
windows-10-iot-core
redhat-datavirt
elfinder
visualstudio.testtools
s3cmd
powermta
php-ews
context-free-language
estimation
rapidweaver
tableau-server
w3-total-cache
number-theory
windows-95
instruction-set
collapsingtoolbarlayout
cjson
angularjs-ng-pattern
tuxedo
uwp-maps
spring-lemon
django-south
asynccallback
android-recyclerview
textkit
cpu-cores
git-checkout
hiera
virus
google-earth-plugin
python-ggplot
orientation-changes
ember-components
dalekjs
funq
method-overriding
neoload
wss
ssms-addin
dvcs
gamepad
robospice
javascriptmvc
client-side-scripting
sharpmap
static-variables
datarepeater
parameterization
jpf
goliath
dashcode
audiostreamer
mvccontrib-grid
hardware-acceleration
hadoop-plugins
external-assemblies
revision
virtualquery
visual-studio-dbpro

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