Announcement

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

  • pointer and built-in functions

    Dear all,
    I am trying to create a pointer that points to different functions (e.g., max, min, sum, mean). However, when it is pointing to function sum(), I got an error.
    Code:
    mata:
       func = &max()  // works fine
       func = &min()  // works fine
       func = &mean() // works fine
       func = &sum()  // <-- ERROR
    end
    The error says
    Code:
    sum() built-in, may not evaluate address
    (0 lines skipped)
    I found a way around it by creating a wrapper function like the one below, but I still would like to know why built-in functions do not allow to be ‘pointed’ and if there is a more elegant way to solve this issue.
    Code:
          function _msum(numeric matrix Z) {
            real matrix T
            T = sum(Z)
            return(T)
         }
    Thanks,
    Best,
    Pablo Bonilla

  • #2
    If you type
    Code:
    help m2_ftof
    and scroll down to the header Passing built-in functions, you'll get your answer to your second question.

    My guess regarding the first is that those Mata functions that are written in compiled C/C++ cannot be pointed to from the outside, i.e., Mata.

    Comment


    • #3
      That's awesome, Joseph. Thank you so much for your quick and clear response. The solution is way more elegant than mine...

      Thanks!
      Best,
      Pablo Bonilla

      Comment

      Working...
      X