Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

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

my fault

In [31]: simple_integral(lambda x:sinc(x/pi), -1000, 1000)
Out[31]: 3.14046624406611

Schle Daniel <u@rz.uni-karlsruhe.de> writes:
> 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)))

[...]

>> 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 :)

Schle Daniel <u@rz.uni-karlsruhe.de> writes:
> >     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.

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-packag es/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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc