473,505 Members | 14,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pylab, integral of sinc function

Hello,

In [19]: def simple_integral(func,a,b,dx = 0.001):
....: return sum(map(lambda x:dx*x, func(arange(a,b,dx))))
....:

In [20]: simple_integral(sin, 0, 2*pi)
Out[20]: -7.5484213527594133e-08

ok, can be thought as zero

In [21]: simple_integral(sinc, -1000, 1000)
Out[21]: 0.99979735786416357

hmm, it should be something around pi
it is a way too far from it, even with a=-10000,b=10000

In [22]: def ppp(x):
....: return sin(x)/x
....:

In [23]: simple_integral(ppp, -1000, 1000)
Out[23]: 3.1404662440661117

nice

is my sinc function in pylab broken?
is there a better way to do numerical integration in pylab?

Regards, Daniel
Feb 20 '07 #1
6 5263
my fault

In [31]: simple_integral(lambda x:sinc(x/pi), -1000, 1000)
Out[31]: 3.14046624406611
Feb 20 '07 #2
Schüle Daniel <uv**@rz.uni-karlsruhe.dewrites:
In [19]: def simple_integral(func,a,b,dx = 0.001):
....: return sum(map(lambda x:dx*x, func(arange(a,b,dx))))
Do you mean

def simple_integral(func,a,b,dx = 0.001):
return dx * sum(map(func, arange(a,b,dx)))

Feb 20 '07 #3
[...]
>In [19]: def simple_integral(func,a,b,dx = 0.001):
....: return sum(map(lambda x:dx*x, func(arange(a,b,dx))))

Do you mean

def simple_integral(func,a,b,dx = 0.001):
return dx * sum(map(func, arange(a,b,dx)))
yes, this should be faster :)
Feb 20 '07 #4
Schüle Daniel <uv**@rz.uni-karlsruhe.dewrites:
return dx * sum(map(func, arange(a,b,dx)))
yes, this should be faster :)
You should actually use itertools.imap instead of map, to avoid
creating a big intermediate list. However I was mainly concerned that
the original version might be incorrect. I don't use pylab and don't
know what happens if you pass the output of arange to a function.
I only guessed at what arange does.
Feb 20 '07 #5
Schüle Daniel wrote:
Hello,

In [19]: def simple_integral(func,a,b,dx = 0.001):
....: return sum(map(lambda x:dx*x, func(arange(a,b,dx))))
....:

In [20]: simple_integral(sin, 0, 2*pi)
Out[20]: -7.5484213527594133e-08

ok, can be thought as zero

In [21]: simple_integral(sinc, -1000, 1000)
Out[21]: 0.99979735786416357

hmm, it should be something around pi
it is a way too far from it, even with a=-10000,b=10000

In [22]: def ppp(x):
....: return sin(x)/x
....:

In [23]: simple_integral(ppp, -1000, 1000)
Out[23]: 3.1404662440661117

nice

is my sinc function in pylab broken?
A couple things:

1) The function is not from pylab, it is from numpy.

2) Look at the docstring of the function, and you will notice that the
convention that sinc() uses is different than what you think it is.

In [3]: numpy.sinc?
Type: function
Base Class: <type 'function'>
Namespace: Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.2.dev3521-py2.5-macosx-10.3-fat.egg/numpy/lib/function_base.py
Definition: numpy.sinc(x)
Docstring:
sinc(x) returns sin(pi*x)/(pi*x) at all points of array x.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Feb 20 '07 #6
Schüle Daniel wrote:
Hello,

In [19]: def simple_integral(func,a,b,dx = 0.001):
....: return sum(map(lambda x:dx*x, func(arange(a,b,dx))))
....:

In [20]: simple_integral(sin, 0, 2*pi)
Out[20]: -7.5484213527594133e-08

ok, can be thought as zero

In [21]: simple_integral(sinc, -1000, 1000)
Out[21]: 0.99979735786416357

hmm, it should be something around pi
it is a way too far from it, even with a=-10000,b=10000

In [22]: def ppp(x):
....: return sin(x)/x
....:

In [23]: simple_integral(ppp, -1000, 1000)
Out[23]: 3.1404662440661117

nice

is my sinc function in pylab broken?
is there a better way to do numerical integration in pylab?
Pylab is mostly a plotting library, which happens (for historical reasons I
won't go into) to expose a small set of numerical algorithms, most of them
actually residing in Numpy. For a more extensive collection of scientific
and numerical algorithms, you should look into using SciPy:

In [34]: import scipy.integrate

In [35]: import scipy as S

In [36]: import scipy.integrate

In [37]: S.integrate.
S.integrate.Inf S.integrate.composite
S.integrate.NumpyTest S.integrate.cumtrapz
S.integrate.__all__ S.integrate.dblquad
S.integrate.__class__ S.integrate.fixed_quad
S.integrate.__delattr__ S.integrate.inf
S.integrate.__dict__ S.integrate.newton_cotes
S.integrate.__doc__ S.integrate.ode
S.integrate.__file__ S.integrate.odeint
S.integrate.__getattribute__ S.integrate.odepack
S.integrate.__hash__ S.integrate.quad
S.integrate.__init__ S.integrate.quad_explain
S.integrate.__name__ S.integrate.quadpack
S.integrate.__new__ S.integrate.quadrature
S.integrate.__path__ S.integrate.romb
S.integrate.__reduce__ S.integrate.romberg
S.integrate.__reduce_ex__ S.integrate.simps
S.integrate.__repr__ S.integrate.test
S.integrate.__setattr__ S.integrate.tplquad
S.integrate.__str__ S.integrate.trapz
S.integrate._odepack S.integrate.vode
S.integrate._quadpack
These will provide dramatically faster performance, and far better
algorithmic control, than the simple_integral:
In [4]: time simple_integral(lambda x:sinc(x/pi), -100, 100)
CPU times: user 7.08 s, sys: 0.42 s, total: 7.50 s
Wall time: 7.58
Out[4]: 3.1244509352

In [40]: time S.integrate.quad(lambda x:sinc(x/pi), -100, 100)
CPU times: user 0.05 s, sys: 0.00 s, total: 0.05 s
Wall time: 0.06
Out[40]: (3.124450933778113, 6.8429604895257158e-10)

Note that I used only -100,100 as the limits so I didn't have to wait
forever for simple_integral to finish.

As you know, this is a nasty, highly oscillatory integral for which almost
any 'black box' method will have problems, but at least scipy is nice
enough to let you know:

In [41]: S.integrate.quad(lambda x:sinc(x/pi), -1000, 1000)
Warning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze
the integrand in order to determine the difficulties. If the position of
a
local difficulty can be determined (singularity, discontinuity) one will
probably gain from splitting up the interval and calling the integrator
on the subranges. Perhaps a special-purpose integrator should be used.
Out[41]: (3.5354545588973298, 1.4922039610659907)

In [42]: S.integrate.quad(lambda x:sinc(x/pi), -1000, 1000,limit=1000)
Out[42]: (3.1404662439375475, 4.5659823144674379e-08)
Cheers,

f

ps - the 2nd number is the error estimate.

Feb 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2677
by: jean.rossier | last post by:
Hello All, I am facing a problem while importing pylab library(in a .py program file) via web browser however the same program works when I execute it from the command prompt. my...
3
4020
by: Dr. Colombes | last post by:
On my home laptop computer, I'm trying to install the appropriate modules so that Python version 2.3.3 and IDLE version 1.0.2 (with an "import matplotlib.matlab" statement) can produce nice...
2
7792
by: Charles Krug | last post by:
List: I'm trying to us pylab to see what I'm doing with some DSP algorithms, in case my posts about convolution and ffts weren't giving it away. I've been using pylab's plot function, but I'm...
6
17669
by: googlinggoogler | last post by:
Hiya, I've got a PIC microcontroller reading me humidity data via rs232, this is in ASCII format. I can view this data easily using hyperterminal or pyserial and convert it to its value...
2
1801
by: Gary Wessle | last post by:
Hi I use debian/testing linux Linux debian/testing 2.6.15-1-686 I found some duplicate files in my system, I don't if the are both needed, should I delete one of the groups below and which...
2
2703
by: timw.google | last post by:
Hi all. I installed matplotlib 0.87.3 under Python 2.4 on both Linux (FC3) and Windows XP Pro. On the linux install, I can import pylab, but when I try to do the same thing on the Windows...
0
1952
by: ehenlin | last post by:
Hi, How to make pylab work together with py2exe? Have anyone managed to build a stand alone exe with pylab package? I have made a small test script but that does not work. setup.py...
14
6716
by: amitsoni.1984 | last post by:
hi, I have some values(say from -a to a) stored in a vector and I want to plot a histogram for those values. How can I get it done in python. I have installed and imported the Matplotlib package...
1
2407
by: oyinbo55 | last post by:
I am trying to use the pylab plot command on my laptop running Ubuntu 6.06 (Dapper). Although the plot command works fine on my XP desktop at work, I cannot open the plot window on the laptop. I...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.