473,785 Members | 2,466 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.5484213527594 133e-08

ok, can be thought as zero

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

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.1404662440661 117

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 5283
my fault

In [31]: simple_integral (lambda x:sinc(x/pi), -1000, 1000)
Out[31]: 3.1404662440661 1
Feb 20 '07 #2
Schüle Daniel <uv**@rz.uni-karlsruhe.dewri tes:
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.dewri tes:
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.5484213527594 133e-08

ok, can be thought as zero

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

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.1404662440661 117

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.framewor k/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.2.dev3521-py2.5-macosx-10.3-fat.egg/numpy/lib/function_base.p y
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.5484213527594 133e-08

ok, can be thought as zero

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

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.1404662440661 117

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.com posite
S.integrate.Num pyTest S.integrate.cum trapz
S.integrate.__a ll__ S.integrate.dbl quad
S.integrate.__c lass__ S.integrate.fix ed_quad
S.integrate.__d elattr__ S.integrate.inf
S.integrate.__d ict__ S.integrate.new ton_cotes
S.integrate.__d oc__ S.integrate.ode
S.integrate.__f ile__ S.integrate.ode int
S.integrate.__g etattribute__ S.integrate.ode pack
S.integrate.__h ash__ S.integrate.qua d
S.integrate.__i nit__ S.integrate.qua d_explain
S.integrate.__n ame__ S.integrate.qua dpack
S.integrate.__n ew__ S.integrate.qua drature
S.integrate.__p ath__ S.integrate.rom b
S.integrate.__r educe__ S.integrate.rom berg
S.integrate.__r educe_ex__ S.integrate.sim ps
S.integrate.__r epr__ S.integrate.tes t
S.integrate.__s etattr__ S.integrate.tpl quad
S.integrate.__s tr__ S.integrate.tra pz
S.integrate._od epack S.integrate.vod e
S.integrate._qu adpack
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.qua d(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.124450933778 113, 6.8429604895257 158e-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.qua d(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.535454558897 3298, 1.4922039610659 907)

In [42]: S.integrate.qua d(lambda x:sinc(x/pi), -1000, 1000,limit=1000 )
Out[42]: (3.140466243937 5475, 4.5659823144674 379e-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
2697
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 configuration : Fedora Core 3 Apache 2.0
3
4064
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 MatLab-like plots. I have a matplotlib.matlab-capable Python set-up running OK on my office desktop, which I obtained after downloading and installing a few more modules (numarray and numeric, I think). Now I get the following message about...
2
7813
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 finding it a bit cumbersome. It works, but if I switch from the interactive window to the plot window and back, the plot window gets trashed.
6
17694
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 (relative humidty with ord(input)) But what im trying to do is plot the data in real time, ideally with pylab - as it looks simple to use and simple is the way i want to go! My code is below, it doesnt show a graph, I was wondering whether
2
1817
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 one? -rw-r--r-- 1 root root 80375 2006-01-24 00:28 /usr/lib/python2.3/site-packages/matplotlib/pylab.py -rw-r--r-- 1 root root 96202 2006-05-07 13:44 /usr/lib/python2.3/site-packages/matplotlib/pylab.pyc -rw-r--r-- 1 root root 96202 2006-05-07...
2
2721
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 installation, I get >>> from pylab import * Traceback (most recent call last): File "<pyshell#7>", line 1, in -toplevel-
0
1969
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
6754
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 but on executing the code =hist(eig, 10) # make a histogram I am getting an error saying "NameError: name 'hist' is not defined". Is there any other way to plot histograms over a given range?
1
2427
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 edited matplotlibrc to change interactive: to "True". In idle, I entered the commands: The terminal window closed abruptly when I typed the closing parenthesis. I have been warned not to use the show() command in interactive mode. I tried:
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
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 we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.