Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is Mata a pre-compiled code? In the ado file I see only call to the Mata function, but not the function's code

    Good afternoon,

    In studying a user written command, and in the .ado file I see a call to a Mata function, something like

    Code:
     mata: functionname(arguments)
    but there is no actual code of the function. This is different from the Mata examples that I read online say here
    https://blog.stata.com/2015/12/29/pr...nd-using-mata/

    In the examples on the Statablog the Mata code is always seen.

    So how does Stata know what the Mata functionname() does? What is the process to precompile this code?
    Also, how does the programmer and Stata check that there is no name conflict and there is no other Mata function with the name functionname()?

  • #2
    [M-1] Ado -- Using Mata with ado-files explains the different ways of how Mata can be used in ado-files.

    I feel that the discussions of advantages and disadvantages is somewhat incomplete, though. For example, the entry states that including Mata code in the ado-file will slow down execution because the code must first be compiled. In my experience, this is still (much) faster than Stata searching through its Mata libraries and mo-files until the respective function is found. That is especially true if (i) you have many functions and/or (ii) if the functions were pre-compiled using an older version of Stata/Mata. This also points to the second drawback of pre-compiling code, which is not well covered in the manual entry. If you pre-compile your Mata code using Stata 17, it cannot be used in Stata 16 or earlier. If you pre-compile the code with, say, Stata 11, the code will be slower than it could be if it were recompiled in Stata 17. Also, when you include the Mata code in the ado-file, the functions are private to the ado-file and there is no naming conflict.

    Comment


    • #3
      Is it possible to share code with precomputed libraries for each supported version, or would that not really work?

      Comment


      • #4
        Originally posted by Leonardo Guizzetti View Post
        Is it possible to share code with precomputed libraries for each supported version[...]?
        In principle, this is possible but it is a bit cumbersome. Obviously, you cannot have two Mata libraries with the same name. You can have same-named functions in different libraries; which function is then called depends, I believe, on the order in which the libraries are loaded. So the safe thing would be to also have the same functions with different names. Typically, you would have some sort of switcher either in the ado or in the Mata code that would call the respective functions. Alternatively, you could set up different packages, one for every supported version.

        In general, unless you are setting up generic Mata functions, e.g., moremata (SSC), I would stick with including (optionally with the include command) the Mata code in the ado-files.

        btw., there is one aspect: precompiled code cannot be read (unless reverse-engineered); that can be an "advantage" if, for whatever reasons, you want to hide your source code.
        Last edited by daniel klein; 10 May 2021, 00:40.

        Comment


        • #5
          Thanks, Daniel. I thought this might be the case.

          Comment

          Working...
          X