⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m2_syntax.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 2 页
字号:
{title:Operators}

{p 4 4 2}
Mata provides the usual assortment of operators; see 
{bf:{help m2_exp:[M-2] exp}}.

{p 4 4 2}
The monadic prefix operators are

	{cmd:-    !    ++    --    &    *}

{p 4 4 2}
Prefix operators {cmd:&} and {cmd:*} have to do with pointers; 
see {bf:{help m2_pointers:[M-2] pointers}}.

{p 4 4 2}
The monadic postfix operators are

	{cmd:'    ++   --}

{p 4 4 2}
Note the inclusion of postfix operator {cmd:'} for transposition.  
Also note that, in the case of {it:Z} complex, {it:Z}{cmd:'} returns 
the conjugate transpose.  If you want the transposition without 
conjugation, see 
{bf:{help mf_transposeonly:[M-5] transposeonly()}}.

{p 4 4 2}
The dyadic operators are

	{cmd:= ?  \  ::  ,  ..  |  &  ==  >=  <=  <  >  !=  +  -  *  #  ^}

{p 4 8 2}
In addition, {cmd:&&} and {cmd:||} are included as synonyms for
{cmd:&} and {cmd:|}.

{p 4 4 2}
The operators {cmd:==} and {cmd:!=} do not require conformability nor do they
require that the matrices be of the same type.  In such cases, the matrices
are unequal ({cmd:==} is false and {cmd:!=} is true.)  For complex arguments,
{cmd:<}, {cmd:<=}, {cmd:>}, and {cmd:>=} refer to length of the complex vector.
{cmd:==} and {cmd:!=}, however, refer not to length but to actual components.
See {bf:{help m2_op_logical:[M-2] op_logical}}.

{p 4 4 2}
The operators {cmd:,} and {cmd:\} are the row-join and column-join operators.
{cmd:(1,2,3)} constructs the row vector (1,2,3).
{cmd:(1\2\3)} constructs the column vector (1,2,3){cmd:'}.
{cmd:(1,2\3,4)} constructs the matrix with first row (1,2) and second row (3,4).
{it:a}{cmd:,}{it:b} joins two scalars, vectors, or matrices rowwise.
{it:a}{cmd:\}{it:b} joins two scalars, vectors, or matrices columnwise.
See {bf:{help m2_op_join:[M-2] op_join}}.

{p 4 4 2}
{cmd:..} and {cmd:::} refer to the row-to and column-to operators.
    {cmd:1..5} is (1,2,3,4,5).
    {cmd:1::5} is (1\2\3\4\5).
    {cmd:5..1} is (5,4,3,2,1).
    {cmd:5::1} is (5\4\3\2\1).
    See {bf:{help m2_op_range:[M-2] op_range}}.

{p 4 4 2}
For 
{cmd:|}, 
{cmd:&}, 
{cmd:==}, 
{cmd:>=}, 
{cmd:<=}, 
{cmd:<}, 
{cmd:>}, 
{cmd:!=}, 
{cmd:+}, 
{cmd:-}, 
{cmd:*}, 
{cmd:/}, and
{cmd:^}, 
there is {cmd:}{it::op} at precedence just below {it:op}. 
These operators perform the elementwise operation.  For instance, 
{it:A}{cmd:*}{it:B} refers to matrix multiplication; 
{it:A}{cmd::*}{it:B} refers to elementwise multiplication.
Moreover, "elementwise" is generalized to cases where {it:A} and {it:B} 
do not have the same number of rows and the same number of columns.  For
instance, if {it:A} is a 1 x {it:c} row vector and {it:B} is a 
{it:r} {it:x} {it:c} matrix, then
{it:C}[{it:i},{it:j}] = A[{it:j}]*B[{it:i},{it:j}] is returned.  
See {bf:{help m2_op_colon:[M-2] op_colon}}.


{title:Subscripts}

{p 4 4 2}
{it:A}{cmd:[}{it:i}{cmd:,}{it:j}{cmd:]}
returns the {it:i},{it:j} element of {it:A}.

{p 4 4 2}
{it:A}{cmd:[}{it:k}{cmd:]} returns 
{it:A}{cmd:[1,}{it:k}{cmd:]} if {it:A} is 1 {it:x} {it:c} and 
{it:A}{cmd:[}{it:k}{cmd:,1]} if {it:A} is {it:r} {it:x} 1.
That is, in addition to declared vectors, any 
1 {it:x} {it:c} matrix or
{it:r} {it:x} 1 matrix may be subscripted by a single index.
Similarly, any vector can be subscripted by two indices.

{p 4 4 2}
{it:i}, {it:j}, and {it:k} may be vectors as well as scalars.
For instance, 
{it:A}{cmd:[(3\5), 4]} returns a 3 {it:x} 1 column vector containing rows 3 to
5 of the 4th column.  

{p 4 4 2}
{it:i}, {it:j}, and {it:k} may be missing value.
{it:A}{cmd:[., 4]} returns a column vector of the 4th column of {it:A}.

{p 4 4 2}
The above subscripts are called list-style subscripts.  Mata provides 
a second format called range-style subscripts that is especially useful
for selecting submatrices.  
{it:A}{cmd:[|3,3\5,5|]} returns the 2 {it:x} 2 submatrix of {it:A} starting at
{it:A}{cmd:[3,3]}.

{p 4 4 2}
See {bf:{help m2_subscripts:[M-2] subscripts}}.


{title:Implied input tokens}

{p 4 4 2}
Before interpreting and compiling a line, Mata makes the following substitutions
to what it sees:

	Input sequence		Interpretation
	{hline 39}
	{cmd:'}{it:name}                   {cmd:'*}{it:name}
	{cmd:[,}                       {cmd:[.,}
	{cmd:,]}                       {cmd:,.]}
	{hline 39}

{p 4 4 2}
Hence, coding {cmd:X'Z} is equivalent to coding {cmd:X'*Z}, and 
coding {cmd:x = z[1,]} is equivalent to coding {cmd:x = z[1,.]}.



{title:Function argument-passing convention}

{p 4 4 2}
Arguments are passed to functions by address, also known as by name or by 
reference.  They are not passed by value.  What this means is that
when you code 

		... {cmd:f(A)} ...

{p 4 4 2}
it is the address of {cmd:A} that is passed to {cmd:f()}, not a copy of the
values in {cmd:A}.  {cmd:f()} can modify {cmd:A}.

{p 4 4 2}
Most functions do not modify their arguments, but some do.
{cmd:lud(}{it:A}{cmd:,} {it:L}{cmd:,} {it:U}{cmd:,} {it:p}{cmd:)},
for instance, calculates
the LU decomposition of {it:A}.  The function replaces the contents of {it:L},
{it:U}, and {it:p}
with matrices such that {it:L}{cmd:[}{it:p}{cmd:,]*}{it:U}={it:A}.

{p 4 4 2}
Oldtimers will have heard of the FORTRAN programmer who called 
a subroutine and passed to it a second argument of 1.  Unbeknownst 
to him, the subroutine changed its second argument, with the result that 
the constant 1 was changed throughout the rest of his code.
That cannot happen in Mata.  When an expression is passed as an argument 
(and constants are expressions), a temporary variable containing the 
evaluation is passed to the function.  Modifications to the temporary variable
are irrelevant because the temporary variable is discarded once the function
returns.  Thus if {cmd:f()} modifies its second argument and you call 
it by coding {cmd:f(A,2)}, because 2 is copied to a temporary variable, 
the value of the literal 2 will remain unchanged on the next call or of any
other 2 appearing in your function.

{p 4 4 2}
If you call a function with an expression that includes the assignment 
operator, it is the left-hand side of the expression that is passed.
That is, coding 

	{cmd:f(a, b=c)}

{p 4 4 2}
has the same result as coding 

	{cmd:b = c}
	{cmd:f(a, b)}

{p 4 4 2}
If function {cmd:f()} changes its second argument, it will be {cmd:b} and 
not {cmd:c} that is modified.

{p 4 4 2}
In addition, Mata attempts not to create unnecessary copies of matrices.  
For instance, consider 

	{cmd:function changearg(x) x[1,1] = 1}

{p 4 4 2}
{cmd:changearg(mymat)} changes the 1,1 element of {cmd:mymat} to 1.
Now let us define

	{cmd:function cp(x) return(x)}

{p 4 4 2}
Coding {cmd:changearg(cp(mymat))} would still change {cmd:mymat} 
because {cmd:cp()} returned {cmd:x} itself.  On the other hand, 
if we defined {cmd:cp()} as 

	{cmd}function cp(x)
	{c -(}
		matrix	t

		t = x
		return(t)
	{c )-}{txt}

{p 4 4 2}
then coding 
{cmd:changearg(cp(mymat))} would not change {cmd:mymat}.  It would change 
a temporary matrix which would be discarded once {cmd:changearg()} returned.


{title:Passing functions to functions}

{p 4 4 2}
One function may receive another function as an argument using pointers.
One codes

	{cmd}function myfunc(pointer(function) f, a, b)
	{c -(}
		{txt:...} (*f)(a) {txt:...} (*f)(b) {txt:...}
	{c )-}{txt}
	
{p 4 4 2}
although the {cmd:pointer(function)} declaration, like all declarations, is
optional.  To call {cmd:myfunc()} and tell it to use function {cmd:prima()}
for {cmd:f()}, and 2 and 3 for {cmd:a} and {cmd:b}, one codes

	{cmd:myfunc(&prima(), 2, 3)}

{p 4 4 2}
See {bf:{help m2_ftof:[M-2] ftof}}
and
{bf:{help m2_pointers:[M-2] pointers}}.


{title:Optional arguments}

{p 4 4 2}
Functions may be coded to allow receiving a variable number of arguments.
This is done by placing a vertical or bar ({cmd:|}) in front of the
first argument that is optional.  For instance, 

	{cmd:function mynorm(matrix A, |scalar power)}
	{cmd:{c -(}}
		...
	{cmd:{c )-}}

{p 4 4 2}
The above function may be called with a single matrix or with a matrix 
followed by a scalar.

{p 4 4 2}
The function {bf:{help mf_args:[M-5] args()}} can be used to determine 
the number of arguments received and set defaults:


	{cmd:function mynorm(matrix A, |scalar power)}
	{cmd:{c -(}}
		...
		{cmd:if (args()==1) power = 2}
		...
	{cmd:{c )-}}

{p 4 4 2}
See {bf:{help m2_optargs:[M-2] optargs}}.


{title:Also see}

{p 4 13 2}
Manual:  {hi:[M-2] syntax}

{p 4 13 2}
Online:  help for 
{bf:{help m2_intro:[M-2] intro}}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -