I wrote a function in mata to generate a variable in such a way that
the variable has a well defined value conditional on the value of
a binary variable. So far, I am able to do this by adding a do operator
inside the function that iterates over each observation. This loop
is, however, taking a very long time. Is there a way to write my
conditional statement in a matrix so that I would not need to loop
over each observation?

Here is an example.
Code:
function geny(xx1){
y=xx1^2:+2:*xx1:+1
return(y)
}
u=runiform(sampsize,1,0,1)
Z=u:>0.5
X=rnormal(sampsize,1,0,1)
Y=geny(X)
I want to generate Y for Z=1

Thank you in advance