Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: mata: passing additional arguments to functions passed to functions


From   "Ben Jann" <ben.jann@soz.gess.ethz.ch>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: mata: passing additional arguments to functions passed to functions
Date   Thu, 8 Jun 2006 11:14:46 +0200

> I've been working on a mata function, called qnewsolve, which finds
roots
> to nonlinear equations f using a quasi-newton's method.

I'd be interested in that. Please make it public once you're done. 

>  I'd like to
> also be able to pass additional arguments to f.How do I do this?
[...]

One solution would be to pass a pointer vector. Note that this requires
that f() knows what to do with the pointer vector. Example:

 numeric matrix qnewsolve(..., pointer vector opt)
 {
 ...
 f0 = (*f)(x, opt) 
 ..
 }

where in f() the options are then used as *opt[1], *opt[2], etc. and you
could call qnewsolve() as

 : s = qnewsolve(&f(), x, (&a1, &a2, ...))

See -help m2_pointers-

A related approach would be to use structures but I don't really see the
advantage of using structures in this situation.

> Also, is there an easy way to return x and rc separately instead of
> tacking rc to the end of x?

Yes. Use structures. See -help m2_struct-.
ben

*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index