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

From |
"David E Moore" <davem@hartman-group.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: [Mata] passing a function to mata (new question) |

Date |
Sun, 28 Aug 2005 13:35:06 -0700 |

An alternative approach to the one offerred by Bill G. is to work with an array of pointers. 1) declare an array of function pointers, say, pointer (function) colvector myfunc 2) load the array with the addresses of all relevant functions 3) in the calling function, calculate a value that you can use to index into the array Step (3) is the tricky part. In Jann's example it's relatively easy to implement, since the desired functions are differentiated by a number suffix. Thus, if myfunc[1] held the address of he1(), myfunc[2] pointed to he2(), etc., then the index is easily obtained from parsing the name passed in he. In other words, if he=="he1", then the index could be calculated by removing the prefix "he" and converting the substring to an integer. The me2() call in Jann's example could be: me2(x, myfunc[realofstr(substr(he,3,.))]) Note, realofstr() is a function that Jann himself wrote to transform a matrix of strings into a corresponding matrix of reals. If Jann's actual code lacked this simple structure (i.e., the function names were not all numbered variants of a simple fixed base name), then the calling procedure would be complicated with the addition of a lookup table. That is, you would create another array of strings that matched the function names. By looping over this array you could search for the appropriate function name. When found, the index of the matched string would be the same as the index of the desired function. Suppose we had, for example, an array of strings called fnames, with fnames[1] equal to the name of the function pointed to by myfunc[1], fnames[2] the name of myfunc[2], etc. Matching on the name in fnames would tell us which function to call from myfunc. I hope that makes sense. Dave -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Jann Ben Sent: Thursday, August 25, 2005 1:18 PM To: statalist@hsphsun2.harvard.edu Subject: st: [Mata] passing a function to mata (new question) There is another issue with passing functions. Suppose, the following code function me1(string scalar he) { ... if (he=="he1") y = me2(x, &he1()) else if (he=="he2") y = me2(x, &he2()) else if (he=="he3") y = me2(x, &he3()) ... return(...) } where function me2(x, he) { ... y = (*he)(x) ... return(...) } Is it possible to change -me1()- such that the different -he-'s do not have to be typed out explicitly? If Mata had macro expansion, I would type function me1(string scalar he) { ... y = me2(x, &`he'()) ... return(...) } However, such things don't work in Mata. Does anyone know a solution to this problem? 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/

**References**:**st: [Mata] passing a function to mata (new question)***From:*"Jann Ben" <ben.jann@soz.gess.ethz.ch>

- Prev by Date:
**st: panel data and estimated variables** - Next by Date:
**st: Stopping Stata** - Previous by thread:
**st: [Mata] passing a function to mata (new question)** - Next by thread:
**Re: st: [Mata] passing a function to mata (new question)** - Index(es):

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