Announcement

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

  • colvector changes orgtype from vector to scalar

    Hi
    I'd just found the following peculiarity in Mata when passing string colvector or vector arguments to functions (This is done in Stata 14):

    Code:
    : function test() {
    > string colvector v
    > v = J(0,1,"")  // Empty start value
    > eltype(v), orgtype(v) // Validate before
    > rows(v), cols(v) // Validate before
    > v = v \ "add row value"
    > eltype(v), orgtype(v) // Validate after
    > rows(v), cols(v) // Validate after
    > }
    
    : test()
                   1           2
        +-------------------------+
      1 |     string   colvector  |
        +-------------------------+
           1   2
        +---------+
      1 |  0   1  |
        +---------+
                1        2
        +-------------------+
      1 |  string   scalar  |
        +-------------------+
           1   2
        +---------+
      1 |  1   1  |
        +---------+
    Inside function test I define an empty string colvector v and validates that this is true.
    Then I add a row, and now is the orgtype a scalar instead, however it got 1 row and 1 column.

    How can that be? What have I done wrong?
    Kind regards

    nhb

  • #2
    Works both ways, too.

    .ÿversionÿ14.1

    .ÿ
    .ÿlocalÿlinesizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ80

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
    :ÿmataÿclear

    :ÿmataÿsetÿmatastrictÿon

    :ÿmataÿsetÿmatalnumÿon

    :ÿ
    :ÿvoidÿfunctionÿtest()ÿ{
    >ÿ
    >ÿÿÿÿÿÿÿÿÿstringÿcolvectorÿV
    >ÿÿÿÿÿÿÿÿÿpragmaÿunsetÿV
    >ÿÿÿÿÿÿÿÿÿprintf("Vÿisÿaÿ%-10s\n",ÿorgtype(V))
    >ÿ
    >ÿÿÿÿÿÿÿÿÿVÿ=ÿJ(10,ÿ10,ÿ"StringÿColumnÿVector")ÿ//ÿConvertÿme
    >ÿÿÿÿÿÿÿÿÿprintf("Vÿisÿaÿ%-10s\n",ÿorgtype(V))
    >ÿ
    >ÿÿÿÿÿÿÿÿÿVÿ=ÿJ(10,ÿ1,ÿ"StringÿColumnÿVector")ÿ//ÿConvertÿme,ÿagain
    >ÿÿÿÿÿÿÿÿÿprintf("Vÿisÿaÿ%-10s\n",ÿorgtype(V))
    >ÿÿÿÿÿÿÿÿÿ
    >ÿÿÿÿÿÿÿÿÿVÿ=ÿJ(1,ÿ1,ÿ"StringÿColumnÿVector")ÿ//ÿAndÿagain
    >ÿÿÿÿÿÿÿÿÿprintf("Vÿisÿaÿ%-10s\n",ÿorgtype(V))
    >ÿ
    >ÿÿÿÿÿÿÿÿÿstringÿscalarÿv
    >ÿÿÿÿÿÿÿÿÿvÿ=ÿ"StringÿScalar"
    >ÿÿÿÿÿÿÿÿÿprintf("vÿisÿaÿ%-10s\n",ÿorgtype(v))
    >ÿ
    >ÿÿÿÿÿÿÿÿÿvÿ=ÿvÿ\ÿ"StringÿColumnÿVector"ÿ//ÿConvertÿme,ÿtoo
    >ÿÿÿÿÿÿÿÿÿprintf("vÿisÿaÿ%-10s\n",ÿorgtype(v))
    >ÿ}

    :ÿ
    :ÿtest()
    Vÿisÿaÿcolvectorÿ
    Vÿisÿaÿmatrixÿÿÿÿ
    Vÿisÿaÿcolvectorÿ
    Vÿisÿaÿscalarÿÿÿÿ
    vÿisÿaÿscalarÿÿÿÿ
    vÿisÿaÿcolvectorÿ

    :ÿ
    :ÿend
    --------------------------------------------------------------------------------

    .ÿ
    .ÿsetÿlinesizeÿ`linesize'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    But not here.

    .ÿversionÿ14.1

    .ÿ
    .ÿlocalÿlinesizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ80

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ------
    :ÿmataÿclear

    :ÿmataÿsetÿmatastrictÿon

    :ÿmataÿsetÿmatalnumÿon

    :ÿ
    :ÿclassÿAÿ{
    >ÿ}

    :ÿ
    :ÿstructÿBÿ{
    >ÿ
    >ÿ}

    :ÿ
    :ÿstructÿBÿscalarÿfunctionÿreturnB(classÿAÿscalarÿa)ÿreturn(a)

    :ÿ
    :ÿaÿ=ÿA(1)

    :ÿeltype(a)
    ÿÿclass

    :ÿeltype(returnB(a))
    ÿÿclass

    :ÿ
    :ÿclassÿAÿscalarÿfunctionÿreturnA(structÿBÿscalarÿb)ÿreturn(b)

    :ÿ
    :ÿbÿ=ÿB(1)

    :ÿeltype(b)
    ÿÿstruct

    :ÿeltype(returnA(b))
    ÿÿstruct

    :ÿ
    :ÿend
    --------------------------------------------------------------------------------

    .ÿ
    .ÿsetÿlinesizeÿ`linesize'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    I would prefer that Mata enforce type more strictly.

    Comment

    Working...
    X