# st: RE: [Mata] passing a function to mata

 From "Jann Ben" To Subject st: RE: [Mata] passing a function to mata Date Thu, 25 Aug 2005 20:56:08 +0200

```Antoine, to pass a function, you have to type "&"
followed by the function's name followed by "()".
First of all, in your program change

s=w:/sqrt(pi()):*(funct)

to

s=w:/sqrt(pi()):*(funct)(x)

then define

function squareofx(x) return(x:^2)

and then type

gaussher(12 , &squareofx())

See -help m2_ftof-.

ben

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of
> Antoine Terracol
> Sent: Thursday, August 25, 2005 5:33 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: [Mata] passing a function to mata
>
>
> Dear _all
>
> I'm trying to write mata code to evaluate integrals |f(x)g(x) (where
> g(x)is the standard normal pdf) by Gauss-Hermite quadrature
> (I know this
> has been done before in Stata, but I'm trying to learn mata, so I
> thought I might give it a try).
>
> I was able to code for the nodes and weight vectors, but I
> can't figure
> out how to tell mata which fuction f(x) I wish to evaluate.
>
> I want to be able to specify the f(x) function, say x^2, when calling
> the mata program
>
> Here is a sketch of the code :
>
>
> ___________________________________________
> void gaussher(scalar node,   funct){
>
> [compute nodes (x) and weight (w) vectors]
>
> x=x:*sqrt(2)
> s=w:/sqrt(pi()):*(funct)
>
> [return stuff]
> }
> end
> ____________________________________________
>
>
>
> If I type -gaussher(12 , x:^2)-
>
>
> but if I type -gaussher(12 , "x:^2")-
>
> mata complains that "nonnumeric found where numeric required"
>
>
> Could anyone point me to the correct way of passing such
> arguments to a
> mata program ?
>
> Best,
> Antoine
>

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```