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

📄 m1_first.hlp

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


{pstd}
We create complex vectors and matrices just as we create real ones, 
the only difference being that their elements are complex:

	: {cmd:Z = (1+1i, 2+3i \ 3-2i , -1-1i)}

	: {cmd:Z}
        {res}       {txt}      1         2
            {c TLC}{hline 21}{c TRC}
          1 {c |}  {res} 1 + 1i    2 + 3i{txt}  {c |}
          2 {c |}  {res} 3 - 2i   -1 - 1i{txt}  {c |}
            {c BLC}{hline 21}{c BRC}{txt}

{pstd}
Similarly, we can create string vectors and matrices, which are vectors and
matrices with string elements:

        : {cmd:S = ("1st element", "2nd element" \ "another row", "last element")}

	: {cmd:S}
        {res}       {txt}           1              2
            {c TLC}{hline 31}{c TRC}
          1 {c |}  {res} 1st element    2nd element{txt}  {c |}
          2 {c |}  {res} another row   last element{txt}  {c |}
            {c BLC}{hline 31}{c BRC}{txt}

{pstd}
In the case of strings, the individual elements can be up to 2,147,437,647
characters long.


{title:Working with functions}

{pstd}
Mata's expressions also include functions:

	: {cmd:sqrt(4)}
	  2

	: {cmd:sqrt(-4)}
	  .

{pstd}
When we ask for the square root of -4, Mata replies "{cmd:.}" Further, note
that {cmd:.} can be stored just like any other number:

	: {cmd:findout = sqrt(-4)}
	
	: {cmd:findout}
	  .

{pstd}
"{cmd:.}" means missing, that there is no answer to our calculation.  Taking
the square root of a negative number is not an error; 
it merely produces missing.  To Mata, missing is a number
like any other number, and the rules for all the operators have been
generalized to understand missing.  For instance, the addition rule is
generalized such that missing plus anything is missing:

	: {cmd:2 + .}
	  .

{pstd}
Still, it should surprise you that Mata produced missing for the
{cmd:sqrt(-4)}.  We said that Mata understands complex numbers, so should
not the answer be 2i?  The answer is that is should be if you are working on
the complex plane, but otherwise, missing is probably a better answer.
Mata attempts to intuit the kind of answer you want by context, and
in particular, uses inheritance rules.  If you ask for the square root of a
real number, you get a real number back.  If you ask for the square root of a
complex number, you get a complex number back:

	: {cmd:sqrt(-4+0i)}
	  2i

{pstd}
Here complex means multipart:  {cmd:-4+0i} is a complex number, it merely
happens to have 0 imaginary part.  Thus:

	: {cmd:areal = -4}

	: {cmd:acomplex = -4 + 0i}

	: {cmd:sqrt(areal)}
	 .

	: {cmd:sqrt(acomplex)}
	  2i

{pstd}
If you ever have a real scalar, vector, or matrix, and want to make it 
complex, use the {cmd:C()} function, which means "convert to complex":

	: {cmd:sqrt(C(areal))}
	  2i

{pstd}
{cmd:C()} is documented in {bf:{help mf_c:[M-5] C()}}.
{cmd:C()} allows one or two arguments.  With one argument, it casts to complex.
With two arguments, it makes a complex out of the two real arguments.  
Thus you could type 

	: {cmd:sqrt(-4+2i)}
	  .485868272 + 2.05817103i

{pstd}
or you could type 

	: {cmd:sqrt(C(-4,2))}
	  .485868272 + 2.05817103i

{pstd}
By the way, used with one argument, {cmd:C()} also allows complex, and then
it does nothing:

	: {cmd:sqrt(C(acomplex))}
	  2i


{title:Distinguishing real and complex values}

{pstd}
It is virtually impossible to tell the difference between a real value 
and a complex value with zero imaginary part:

	: {cmd:areal = -4}

	: {cmd:acomplex = -4 + 0i}

	: {cmd:areal}
	  -4

	: {cmd:acomplex}
	  -4

{pstd}
Yet, as we have seen, the difference is important:  {cmd:sqrt(areal)} is 
missing, {cmd:sqrt(acomplex)} is -2i.  
One solution is the {cmd:eltype()} function:

	: {cmd:eltype(areal)}
	  real

	: {cmd:eltype(acomplex)}
	  complex

{pstd}
{cmd:eltype()} can also be used with strings, 

	: astring = "hello"

	: {cmd:eltype(astring)}
	  string

{pstd}
but this is mostly useful in programming contexts.


{title:Working with matrix and scalar functions}

{pstd}
Some functions are matrix functions:  they require a matrix and 
return a matrix.  Mata's {cmd:invsym(}{it:X}{cmd:)} is an example of 
such a function.  It returns the matrix that is the inverse of 
symmetric, real matrix {it:X}:

	: {cmd:X = (76, 53, 48 \ 53, 88, 46 \ 48, 46, 63)}

	: {cmd:Xi = invsym(X)}

	: {cmd:Xi}
        {res}{txt}[symmetric]
                          1              2              3
            {c TLC}{hline 46}{c TRC}
          1 {c |}  {res} .0298458083                              {txt}  {c |}
          2 {c |}  {res}-.0098470272    .0216268926               {txt}  {c |}
          3 {c |}  {res}-.0155497706   -.0082885675    .0337724301{txt}  {c |}
            {c BLC}{hline 46}{c BRC}{txt}

	: {cmd:Xi*X}
        {res}       {txt}           1              2              3
            {c TLC}{hline 46}{c TRC}
          1 {c |}  {res}           1   -8.67362e-17   -8.50015e-17{txt}  {c |}
          2 {c |}  {res}-1.38778e-16              1   -1.02349e-16{txt}  {c |}
          3 {c |}  {res}           0    1.11022e-16              1{txt}  {c |}
            {c BLC}{hline 46}{c BRC}{txt}

{pstd}
The last matrix, {cmd:Xi*X}, differs just a little from the identity matrix 
due to unavoidable computational roundoff error.

{pstd}
Other functions are, mathematically speaking, scalar functions.  {cmd:sqrt()}
is an example in that it makes no sense to speak of {cmd:sqrt(}{it:X}{cmd:)}.
(That is, it makes no sense to speak of {cmd:sqrt(}{it:X}{cmd:)} unless we
were speaking of the Cholesky square-root decomposition. Mata has such a
matrix 
function; see help {bf:{help mf_cholesky:[M-5] cholesky()}}.)

{pstd}
When a function is, mathematically speaking, a scalar function, 
the corresponding Mata function will usually allow vector and matrix 
arguments and, in that case, the Mata function makes the calculation 
on each element individually:

	: {cmd:M = (1,2 \ 3,4 \ 5,6)}

	: {cmd:M}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}1   2{txt}  {c |}
          2 {c |}  {res}3   4{txt}  {c |}
          3 {c |}  {res}5   6{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

	: {cmd:S = sqrt(M)}

	: {cmd:S}
        {res}       {txt}          1             2
            {c TLC}{hline 29}{c TRC}
          1 {c |}  {res}          1   1.414213562{txt}  {c |}
          2 {c |}  {res}1.732050808             2{txt}  {c |}
          3 {c |}  {res}2.236067977   2.449489743{txt}  {c |}
            {c BLC}{hline 29}{c BRC}{txt}

	: {cmd:S[1,2]*S[1,2]}
	  2

	: {cmd:S[2,1]*S[2,1]}
	  3

{pstd}
When a function returns a result calculated in this way, it is said to 
return an element-by-element result.


{title:Performing element-by-element calculations:  colon operators}

{pstd}
Mata's operators, such as {cmd:+} (addition) and {cmd:*} (multiplication), 
work as you would expect.  In particular, {cmd:*} performs matrix 
multiplication:


	: {cmd:A = (1, 2 \ 3, 4)}

	: {cmd:B = (5, 6 \ 7, 8)}

	: {cmd:A*B}
        {res}       {txt} 1    2
            {c TLC}{hline 11}{c TRC}
          1 {c |}  {res}19   22{txt}  {c |}
          2 {c |}  {res}43   50{txt}  {c |}
            {c BLC}{hline 11}{c BRC}{txt}

{pstd}
The first element of the result was calculated as 1*5+2*7=19.

{pstd}
Sometimes, you really want the element-by-element result.  When you do, 
place a colon in front of the operator:  Mata's {cmd::*} operator performs 
element-by-element multiplication:


	: {cmd:A:*B}
        {res}       {txt} 1    2
            {c TLC}{hline 11}{c TRC}
          1 {c |}  {res} 5   12{txt}  {c |}
          2 {c |}  {res}21   32{txt}  {c |}
            {c BLC}{hline 11}{c BRC}{txt}

{pstd}
See {bf:{help m2_op_colon:[M-2] op_colon}} for more information.


{title:Writing programs}

{pstd}
Mata is a complete programming language; it will allow you to create 
your own functions:

	: {cmd:function add(a,b) return(a+b)}

{pstd}
That single statement creates a new function, although perhaps you would
prefer if we typed it as

	: {cmd:function add(a, b)}
	> {cmd:{c -(}}
	>         {cmd:return(a+b)}
	> {cmd:{c )-}}

{pstd} 
because that makes it obvious that a program can contain many lines.
In either case, once defined, we can use the function:

	: {cmd:add(1,2)}
	  3

	: {cmd:add(1+2i,4-1i)}
	  5+1i

	: {cmd:add( (1,2), (3,4) )}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}4   6{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

	: {cmd:add(x,y)}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}4   6{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}
	
	: {cmd:add(A,A)}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}2   4{txt}  {c |}
          2 {c |}  {res}6   8{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

	: {cmd:Z1 = (1+1i, 1+1i \ 2, 2i)}
	: {cmd:Z2 = (1+2i, -3+3i \ 6i, -2+2i)}
	: {cmd:add(Z1, Z2)}
        {res}       {txt}      1         2
            {c TLC}{hline 21}{c TRC}
          1 {c |}  {res} 2 + 3i   -2 + 4i{txt}  {c |}
          2 {c |}  {res} 2 + 6i   -2 + 4i{txt}  {c |}
            {c BLC}{hline 21}{c BRC}{txt}


	: {cmd:add("Alpha","Beta")}
	  AlphaBeta

	: {cmd:S1 = ("one", "two" \ "three", "four")}
	: {cmd:S2 = ("abc", "def" \ "ghi", "jkl")}
	: {cmd:add(S1, S2)}
        {res}       {txt}       1          2
            {c TLC}{hline 23}{c TRC}
          1 {c |}  {res}  oneabc     twodef{txt}  {c |}
          2 {c |}  {res}threeghi    fourjkl{txt}  {c |}
            {c BLC}{hline 23}{c BRC}{txt}

{pstd}
Of course, our little function {cmd:add()} does not do anything that the 
{cmd:+} operator does not already do, but we could write a program that 
did do something different.  The following program will allow us to make 
{it:n} {it:x} {it:n} identity matrices:

	: {cmd:real matrix id(real scalar n)}
	> {cmd:{c -(}}
	>         {cmd:real scalar i}
	>         {cmd:real matrix res}
	>
	>         {cmd:res = J(n, n, 0)}
	>         {cmd:for (i=1; i<=n; i++) {c -(}}
        >                 {cmd:res[i,i] = 1}
	>         {cmd:{c )-}}
        >         {cmd:return(res)}
	> {cmd:{c )-}}

	: {cmd:I3 = id(3)}

	: {cmd:I3}
        {res}{txt}[symmetric]

⌨️ 快捷键说明

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