Announcement

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

  • A simple example of hash table and asarray

    While the Mata Manual talks about hash1() function and asarray() function, but does not give an example of how to use these two functions together. Can someone give a simple example of using these two functions together. Thanks in advance.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    My reading of asarray() in the Mata manual suggests to me that hash() is used "behind the scenes" by Mata to implement the asarray() suite of functions. My thinking is that StataCorp chose to expose hash() to Mata users for whatever other use they might be able to make of this powerful technique, but direct use of hash() is not required for effective use of the asarray() suite, and consequently it is not documented that way. Now the question is, are you trying to accomplish something specific that asarray() does not seem to support but you hope hash() would make possible?

    I look too forward to a definitive answer from someone who has made use of asarray(). My knowledge of Mata is small, but familiarity with associative arrays from other settings interests me in this topic. Thank you for bringing it to my attention with your question.

    Comment


    • #3
      Sergio Correia has used hash1() function in his FTOOLS package. I was interested in using hash1() and other associated functions for matrix / vector comparisons for the purpose of speeding up Mata functions.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        The code for asarray is open, so if we look behind the scenes we can see that indeed it uses hash1() (view asarray.mata, adopath).

        Depending on what you want to do, it might be easier to use asarray() or rely on hash1() directly. The first solution is simpler and easier to code, but the last one gives more flexiblity. (in my case, I was using asarrays that I knew would not be increased in size, so using my own implementation of an associative array was the way to go)

        Comment


        • #5
          Thank you Sergio for your reply. Can you set up a simple example of using asarray() or hash1() to show thier use.
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            I found some code on my hard drive that I wrote a while ago, where I implemented a counter in Mata. counter1() piggybacks on asarray(), while counter2() creates an alternative associative array with hash1().

            The main difference is how they treat cases where two values have the same hash i.e. hash1(123)==hash1(456). counter1() keeps a list of the values for a given hash (because that's how asarray() works), while counter2() uses something called linear probing.

            Anyways, this is the do file:

            https://gist.github.com/sergiocorrei...de8f854e8cdc6a

            Comment

            Working...
            X