comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Anonymous Math Functions in IDL like Matlab
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Anonymous Math Functions in IDL like Matlab [message #75264] Wed, 23 February 2011 17:47 Go to next message
Ammar Yusuf is currently offline  Ammar Yusuf
Messages: 36
Registered: October 2010
Member
Hi, in Matlab you can do something like:
sqrt = @(x) x.^2;

Is there a way to do this in IDL? I was just wondering.

I tried searching google but that didn't help. Thanks!
Re: Anonymous Math Functions in IDL like Matlab [message #75310 is a reply to message #75264] Fri, 25 February 2011 16:50 Go to previous messageGo to next message
Ammar Yusuf is currently offline  Ammar Yusuf
Messages: 36
Registered: October 2010
Member
On Feb 25, 11:16 am, Michael Galloy <mgal...@gmail.com> wrote:
> On 2/24/11 8:32 pm, nata wrote:
>
>> I am just curious. What does the expression "sqrt = @(x) x.^2" means ?
>
> It's a lambda function, or anonymous function, a way of specifying a
> function without naming it, i.e., to use inline only once.
>
> For example, see how Python does this:
>
>   http://diveintopython.org/power_of_introspection/lambda_func tions.html
>
> Mike
> --www.michaelgalloy.com
> Research Mathematician
> Tech-X Corporation

That's pretty cool. I did not know that.

I know in Matlab what happens is it creates a function say sqrt. In
Matlab
you can then call sqrt(4) and it would return 2.

I found this very helpful instead of making a .m file for each
polynomial function
that I was doing for my homework.
Re: Anonymous Math Functions in IDL like Matlab [message #75324 is a reply to message #75264] Fri, 25 February 2011 08:16 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 2/24/11 8:32 pm, nata wrote:
> I am just curious. What does the expression "sqrt = @(x) x.^2" means ?

It's a lambda function, or anonymous function, a way of specifying a
function without naming it, i.e., to use inline only once.

For example, see how Python does this:

http://diveintopython.org/power_of_introspection/lambda_func tions.html

Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
Re: Anonymous Math Functions in IDL like Matlab [message #75371 is a reply to message #75310] Thu, 03 March 2011 15:13 Go to previous message
rtk is currently offline  rtk
Messages: 22
Registered: September 2008
Junior Member
I wrote a bunch of higher-order function stuff for IDL awhile back and
included a hack that simulated a lambda function. Basically, it
defined the function on the fly and returned a string with the actual
name. You then used this with the higher-order function (or any
function). So, for your example,

square = lambda('x:x^2')

but to use it directly:

ans = call_function(square, 4)

Outside the context of a higher-order function, this isn't really that
useful. Still, if you want the code, I'll send it, just email me
"oneelkruns" "hotmail" "com". The higher-order functions were DLMs
for Linux and Windows, 32-bit only.

Also, you can define a procedure/function in IDL from the command
line. No need to quit and type a new file:

IDL> .run
- function square, x
- return, x*x
- end
% Compiled module: SQUARE.
IDL> print, square(4)

Perhaps what you are really looking for?

Ron
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Plotimage and non-byte images
Next Topic: Re: Read ASCII File with tab seperator

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:27:08 PDT 2025

Total time taken to generate the page: 0.00465 seconds