[SciPy-User] Double integration with integrate.trapz
eat
e.antero.tammi at gmail.com
Mon Feb 28 06:44:34 EST 2011
Hi
On Mon, Feb 28, 2011 at 1:05 PM, bhanukiran perabathini <
bhanukiran.perabathini at gmail.com> wrote:
> Hi again!
>
> let's say we have a siple function that goes like
>
> def func(x, y):
> return x + y
>
> now I want to get this value for every point on the x-y plane, here's what
> I do
>
> x = sp.array([-2, -1, 0, 1, 2])
> y = sp.array([-2, -1, 0, 1, 2])
>
> xx = x[:, sp.newaxis]
> yy = y[sp.newaxis, :]
>
> >>> func(xx, yy)
> >>>
> array([[-4, -3, -2, -1, 0],
> [-3, -2, -1, 0, 1],
> [-2, -1, 0, 1, 2],
> [-1, 0, 1, 2, 3],
> [ 0, 1, 2, 3, 4]])
>
> works as expected right?
>
> How do we get the following function working with broadcasted arrays?
>
> def func2(x, y):
> if x > y:
> return x + y
> else:
> return x - y
>
> func2(xx, yy) raises the error
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
>
>
> How do we handle such situations?
>
For example
In []: x= arange(5)
In []: y= arange(5)
In []: xx= x[:, None]
In []: yy= y[None, :]
In []: A= xx+ yy
In []: A[xx< yy]= (xx- yy)[xx< yy]
In []: A
Out[]:
array([[ 0, -1, -2, -3, -4],
[ 1, 2, -1, -2, -3],
[ 2, 3, 4, -1, -2],
[ 3, 4, 5, 6, -1],
[ 4, 5, 6, 7, 8]])
Regards,
eat
>
>
> Thanks so much,
> Bhanukiran
>
>
> On Mon, Feb 28, 2011 at 2:48 PM, bhanukiran perabathini
> <bhanukiran.perabathini at gmail.com> wrote:
> > oh looks like this is what i've been looking for..
> >
> > http://www.scipy.org/EricsBroadcastingDoc
> >
> >
> >
> > On Mon, Feb 28, 2011 at 12:08 PM, bhanukiran perabathini
> > <bhanukiran.perabathini at gmail.com> wrote:
> >> Hi Pauli Virtanen & Giacomo Boffi,
> >>
> >> Both of your suggestions helped me. thanks so much.
> >>
> >> But I guess I'm having some troubles understanding and using
> >> broadcasting to its total power. Could you suggest some tutorial where
> >> I can read up learn this stuff?
> >>
> >> Manipulating with the axes seems a little tricky to me. I want to
> >> master them so well that I can think in terms of them.
> >>
> >> Thanks again for your time,
> >> Bhanukiran
> >>
> >>
> >>
> >> On Sun, Feb 27, 2011 at 5:04 PM, Giacomo Boffi <giacomo.boffi at polimi.it>
> wrote:
> >>> bhanukiran perabathini writes:
> >>>
> >>> > - My task is to perform a double integral (with two free variables)
> >>> > and make a filled-contour plot of the logarithm of its absolute
> >>> > square.
> >>>
> >>> notwistanding the other excellent advice you've received, please note
> >>> that your integral is symmetric with respect to the free variables, so
> >>> that you could double the resolution of your plot using the same
> >>> amount of computational resources plotting the first quadrant only
> >>>
> >>> --
> >>> anch'io la penso come me, ma -- SteO153, in IHC
> >>>
> >>> --
> >>> This message has been scanned for viruses and
> >>> dangerous content by MailScanner, and is
> >>> believed to be clean.
> >>>
> >>> _______________________________________________
> >>> SciPy-User mailing list
> >>> SciPy-User at scipy.org
> >>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >>>
> >>
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110228/0b851c6b/attachment.html>
More information about the SciPy-User
mailing list