How to apply a function to an array? [message #43998] |
Fri, 13 May 2005 01:37  |
W. Eremeev
Messages: 3 Registered: February 2005
|
Junior Member |
|
|
Suppose, I have a function with one scalar argument, returning a scalar
and an array of values.
I want to get another array with the same dimensions, containing
results of application of my function to the values in the first array.
R (www.r-project.org) has such functions (apply, lapply, sapply).
They take vectors or matrices and functions as arguments and return
arrays.
What about IDL?
|
|
|
Re: How to apply a function to an array? [message #44096 is a reply to message #43998] |
Fri, 13 May 2005 02:25  |
Timm Weitkamp
Messages: 66 Registered: August 2002
|
Member |
|
|
Today at 11:05, Antonio Santiago wrote:
> W. Eremeev wrote:
>> Suppose, I have a function with one scalar argument, returning a scalar
>> and an array of values.
>> I want to get another array with the same dimensions, containing
>> results of application of my function to the values in the first array.
>>
>> R (www.r-project.org) has such functions (apply, lapply, sapply).
>> They take vectors or matrices and functions as arguments and return
>> arrays.
>>
>> What about IDL?
>>
> R is a functional programming language whereas IDL not. This means the
> working philosophy is pretty diferent.
>
> Sorry :(, but I dont know if there is a function or procedure in IDL
> that executes an specified function over a set of data. Perhaps you must
> think in terms of loops.
That said, any IDL function that returns a scalar expression for a single
scalar argument should (and usually does) also accept an array as the
argument, and return an array of the same dimensions. (Which eliminates
the need for an "apply" function.)
Example: the SIN function.
IDL> a = !pi/4
IDL> print, sin(a)
0.707107
IDL> b = [0, !pi/4, !pi/2]
IDL> print, sin(b)
0.00000 0.707107 1.00000
IDL> c = findgen(2,3,4)
IDL> help, sin(c)
<Expression> FLOAT = Array[2, 3, 4]
The situation is different for functions that reduce the dimensions of the
input array (such as MAX or TOTAL). Some of these functions (the number is
growing with every release of IDL) allow the user to specify a specific
dimension of the input array to which the function should be applied. For
those functions that don't, it is useful to have a look at Craig
Markwardt's CMAPPLY routine.
Cheers,
Timm
--
Timm Weitkamp <http://people.web.psi.ch/weitkamp>
|
|
|
Re: How to apply a function to an array? [message #44097 is a reply to message #43998] |
Fri, 13 May 2005 02:05  |
Antonio Santiago
Messages: 201 Registered: February 2004
|
Senior Member |
|
|
W. Eremeev wrote:
> Suppose, I have a function with one scalar argument, returning a scalar
> and an array of values.
> I want to get another array with the same dimensions, containing
> results of application of my function to the values in the first array.
>
> R (www.r-project.org) has such functions (apply, lapply, sapply).
> They take vectors or matrices and functions as arguments and return
> arrays.
>
> What about IDL?
>
R is a functional programming language whereas IDL not. This means the
working philosophy is pretty diferent.
Sorry :(, but I dont know if there is a function or procedure in IDL
that executes an specified function over a set of data. Perhaps you must
think in terms of loops.
--
-----------------------------------------------------
Antonio Santiago P�rez
( email: santiago<<at>>grahi.upc.edu )
( www: http://www.grahi.upc.edu/santiago )
( www: http://asantiago.blogsite.org )
-----------------------------------------------------
GRAHI - Grup de Recerca Aplicada en Hidrometeorologia
Universitat Polit�cnica de Catalunya
-----------------------------------------------------
|
|
|