Dear fellow Mata enthusiasts:
I am curious if there is a way to perform the following sort of function-evaluation task without looping. That is, I am curious if it is possible to construct a Meta-function that a) takes in a scalar function and a vector as an argument, and then evaluates the scalar function at all vector values without looping.
Here is the kind of task I have in mind with a loop:
I understand that I could just code the function f as:
But, I am curious if it is possible to replace Floop in the above with something like Fpar (the following doesn't work and is merely intended to suggest what I am after):
If this isn't possible, perhaps someone has some ideas about how one could write a function that would a) take in a function as an argument, and b) automatically replace all operators with parallel operators!
Best,
Matt Baker
I am curious if there is a way to perform the following sort of function-evaluation task without looping. That is, I am curious if it is possible to construct a Meta-function that a) takes in a scalar function and a vector as an argument, and then evaluates the scalar function at all vector values without looping.
Here is the kind of task I have in mind with a loop:
Code:
clear all
mata:
real scalar f(real scalar x) return(x^2-x+14)
real colvector Floop(pointer (real scalar function) scalar f, real colvector arg)
{
fx=J(rows(arg),1,1)
for (i=1;i<=rows(arg);i++) {
fx[i]=(*f)(arg[i])
}
return(fx)
}
x=runiform(100,1)
X=Floop(&f(),x)
end
Code:
real colvector f(real colvector x) return(x:^2:-x:+14)
Code:
mata:
real colvector Fpar(pointer (real scalar function) scalar f, real colvector arg)
{
F=J(rows(arg),1,f)
X=(*F)(arg)
return(X)
}
X=Fpar(&f(),x)
end
Best,
Matt Baker
