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

📄 m1_first.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 3 页
字号:
               1   2   3
            {c TLC}{hline 13}{c TRC}
          1 {c |}  {res}1        {txt}  {c |}
          2 {c |}  {res}0   1    {txt}  {c |}
          3 {c |}  {res}0   0   1{txt}  {c |}
            {c BLC}{hline 13}{c BRC}{txt}

{pstd}
The function {cmd:J()} in the program line {cmd:res = J(n, n, 0)} is a Mata
built-in function 
that returns an {cmd:n} {it:x} {cmd:n} matrix
containing 0s ({cmd:J(}{it:r}{cmd:,} {it:c}{cmd:,} {it:val}{cmd:)} returns an
{it:r} {it:x} {it:c} matrix, the elements of which are all equal to {it:val});
see {bf:{help mf_j:[M-5] J()}}.

{pstd}
{cmd:for (i=1; i<=n; i++)} says that starting with i=1 and so long as i<=n do
what is inside the braces (set {cmd:res[i,i]} equal to 1) and then (we are
back to the {cmd:for} part again), increment i.  

{pstd}
The final line -- {cmd:return(res)} -- says to return the matrix we have just
created.

{pstd}
Actually, just as with {cmd:add()}, we do not need {cmd:id()} because 
Mata has a built-in function {cmd:I(}{it:n}{cmd:)} that makes identity 
matrices, but it is interesting to see how the problem could be programmed.


{title:More functions}

{pstd}
Mata has lots of functions already and much of this manual concerns
documenting what those functions do; see {bf:{help m4_intro:[M-4] intro}}.
But right now, what is important is that many of the functions 
are themselves written in Mata!

{pstd}
One of those functions is {cmd:pi()};it takes no arguments and returns 
the value of {it:pi}.  The code for it reads

	{cmd:real scalar pi() return(3.141592653589793238462643)}

{pstd}
There is no reason to type the above function because it is already 
included as part of Mata:

	: {cmd:pi()}
	  3.141592654

{pstd}
When Mata lists a result, it does not show as many digits, but we 
could ask to see more:

	: {cmd:printf("%17.0g", pi())}
	  3.14159265358979

{pstd}
Other Mata functions include the hyperbolic functions 
{cmd:sinh(}{it:u}{cmd:)}, {cmd:cosh(}{it:u}{cmd:)}, etc.
The code for 
{cmd:sinh(}{it:u}{cmd:)}, 
{cmd:cosh(}{it:u}{cmd:)}, and 
{cmd:tanh(}{it:u}{cmd:)}
reads

        {cmd:numeric matrix sinh(numeric matrix u) return((exp(u)-exp(-u)):/2)}

        {cmd:numeric matrix cosh(numeric matrix u) return((exp(u)+exp(-u)):/2)}

        {cmd}numeric matrix tanh(numeric matrix u)
        {c -(}
                numeric matrix  eu, emu

                eu = exp(u)
                emu = exp(-u)
                return( (eu-emu):/(eu+emu) )
        {c )-}{txt}

{pstd}
See for yourself:  at the Stata dot prompt (not the Mata colon prompt), type

	. {cmd:viewsource sinh.mata}

	. {cmd:viewsource cosh.mata}

	. {cmd:viewsource tanh.mata}

{pstd}
When the code for a function was written in Mata (as opposed to having been
written in C), {cmd:viewsource} can show you the code; see 
{bf:{help m1_source:[M-1] source}}.

{pstd}
Returning to the functions themselves, 

        {cmd:numeric matrix sinh(numeric matrix u) return((exp(u)-exp(-u)):/2)}

        {cmd:numeric matrix cosh(numeric matrix u) return((exp(u)+exp(-u)):/2)}

        {cmd}numeric matrix tanh(numeric matrix u)
        {c -(}
                numeric matrix  eu, emu

                eu = exp(u)
                emu = exp(-u)
                return( (eu-emu):/(eu+emu) )
        {c )-}{txt}

{pstd}
this is the first time we have seen the word {cmd:numeric}:  it means real or 
complex.  Built-in (previously written) function {cmd:exp()} works like
{cmd:sqrt()} in that it allows a real or complex argument and correspondingly
returns a real or complex result.  Said in Mata jargon, {cmd:exp()} allows a
{cmd:numeric} argument and correspondingly returns a {cmd:numeric} result.
{cmd:sinh()}, {cmd:cosh()}, and {cmd:tanh()} will also 
work like {cmd:sqrt()} and {cmd:exp()}.

{pstd}
Another characteristic {cmd:sinh()}, {cmd:cosh()} and {cmd:tanh()} share
with {cmd:sqrt()} and {cmd:exp()} is element-by-element operation.
{cmd:sinh()}, {cmd:cosh()}, and {cmd:tanh()} are element-by-element 
 because 
{cmd:exp()} is element-by-element and because we were careful 
to use the {cmd::/} (element-by-element) divide operator.

{pstd}
In any case, 
there is no need to type the above functions because they are already
part of Mata.  You could learn more about them by seeing their manual 
entry, {bf:{help mf_sin:[M-5] sin()}}.

{pstd}
At the other extreme, Mata functions can become quite long.  Here is 
Mata's function to solve {it:AX}={it:B} for {it:X} when {it:A} is 
lower triangular, placing the result {it:X} back into {it:A}:

	{cmd}real scalar _solvelower(
			numeric matrix A, numeric matrix b, 
			|real scalar usertol, numeric scalar userd)
	{
		real scalar             tol, rank, a_t, b_t, d_t
		real scalar             n, m, i, im1, complex_case
		numeric rowvector       sum
		numeric scalar          zero, d

		d  = userd

		if ((n=rows(A))!=cols(A)) _error(3205)
		if (n != rows(b))         _error(3200)
		if (isview(b))            _error(3104)
		m = cols(b)
		rank = n

		a_t = iscomplex(A)
		b_t = iscomplex(b)
		d_t = d<. ? iscomplex(d) : 0

		complex_case = a_t | b_t | d_t

		if (complex_case) {
			if (!a_t) A = C(A)
			if (!b_t) b = C(b)
			if (d<. & !d_t) d = C(d)
			zero = 0i
		}
		else zero = 0 

		if (n==0 | m==0) return(0)

		tol = solve_tol(A, usertol)

		if (abs(d) >=. ) {
			if (abs(d=A[1,1])<=tol) {
				b[1,.] = J(1, m, zero)
				--rank
			}
			else {
				b[1,.] = b[1,.] :/ d
				if (missing(d)) rank = .
			}
	
			for (i=2; i<=n; i++) {
				im1 = i - 1 
				sum = A[|i,1\i,im1|] * b[|1,1\im1,m|]
				if (abs(d=A[i,i])<=tol) {
					b[i,.] = J(1, m, zero)
					--rank
				}
				else {
					b[i,.] = (b[i,.]-sum) :/ d
					if (missing(d)) rank = .
				}
			}
		}
		else {
			if (abs(d)<=tol) {
				rank = 0
				b = J(rows(b), cols(b), zero)
			}
			else {
				b[1,.] = b[1,.] :/ d

				for (i=2; i<=n; i++) {
					im1 = i - 1 
					sum = A[|i,1\i,im1|] * b[|1,1\im1,m|]
					b[i,.] = (b[i,.]-sum) :/ d
				}
			}
	
		}
		return(rank)
	}{txt}
	

{pstd}
If the function were not already part of Mata and you wanted to use it, 
you could type 
it into a do-file or onto the end of an ado-file (especially good if
you just want to use {cmd:_solvelower()} as a subroutine).  In those cases, 
do not forget to enter and exit Mata:

        {hline 40} {it:top of file} {hline 5}
	{cmd:program} {it:mycommand} 
		...
		{it:ado-file code appears here}
		...
	{cmd:end}

	{cmd:mata:}
		{it:_solvelower() code appears here}
	{cmd:end}
        {hline 40} {it:bottom of file} {hline 5}

{pstd}
Sharp-eyed readers will notice that we put a colon on the end of the Mata
command.  That's a detail and why we did that is explained in 
{bf:{help m3_mata:[M-3] mata}}.

{pstd}
In addition to loading functions by putting their code in do- and ado-files, 
you can also save the compiled versions of functions in {cmd:.mo} files 
(see {bf:{help mata_mosave:[M-3] mata mosave}}) or into {cmd:.mlib} Mata
libraries (see {bf:{help mata_mlib:[M-3] mata mlib}}).

{pstd}
In the case of {cmd:_solvelower()}, it has already been saved into a 
library, namely Mata's official library, so you need not do any of 
this.


{title:Mata environment commands}

{pstd}
When you are using Mata, there is a set of commands that will tell you about
and manipulate Mata's environment.

{pstd}
The most useful such command is {cmd:mata} {cmd:describe}:

	: {cmd:mata describe}
	
              {txt}# bytes   type                       name and extent
	{hline 70}
	{res}           34   {txt}transmorphic matrix        {res}add{txt}()
	{res}           98   {txt}real matrix                {res}id{txt}()
	{res}           32   {txt}real matrix                {res}A{txt}[2,2]
	{res}           32   {txt}real matrix                {res}B{txt}[2,2]
	{res}           72   {txt}real matrix                {res}I3{txt}[3,3]
	{res}           48   {txt}real matrix                {res}M{txt}[3,2]
	{res}           48   {txt}real matrix                {res}S{txt}[3,2]
	{res}           47   {txt}string matrix              {res}S1{txt}[2,2]
	{res}           44   {txt}string matrix              {res}S2{txt}[2,2]
	{res}           72   {txt}real matrix                {res}X{txt}[3,3]
	{res}           72   {txt}real matrix                {res}Xi{txt}[3,3]
	{res}           64   {txt}complex matrix             {res}Z{txt}[2,2]
	{res}           64   {txt}complex matrix             {res}Z1{txt}[2,2]
	{res}           64   {txt}complex matrix             {res}Z2{txt}[2,2]
	{res}           16   {txt}real colvector             {res}a{txt}[2]
	{res}           16   {txt}complex scalar             {res}acomplex
                    8   {txt}real scalar                {res}areal
                   16   {txt}real colvector             {res}b{txt}[2]
        {res}           32   {txt}real colvector             {res}c{txt}[4]
        {res}            8   {txt}real scalar                {res}findout
                   16   {txt}real rowvector             {res}x{txt}[2]
        {res}           16   {txt}real rowvector             {res}y{txt}[2]
        {res}           32   {txt}real rowvector             {res}z{txt}[4]
        {hline 70}{txt}

	: {cmd:_}

{pstd}
Another useful command is {cmd:mata} {cmd:clear}, which will clear Mata 
without disturbing Stata:

	: {cmd:mata clear}

	: {cmd:mata describe}

              {txt}# bytes   type                       name and extent
        {hline 70}
        {hline 70}

{pstd}
There are other useful {cmd:mata} commands; see 
{bf:{help m3_intro:[M-3] intro}}.
Do not confuse this command {cmd:mata} which you type at Mata's 
colon prompt with Stata's command {cmd:mata} which you type at Stata's 
dot prompt and which invokes Mata.


{title:Exiting Mata}

{pstd}
When you are done using Mata, type {cmd:end} to Mata's colon prompt:

	: {cmd:end}
	{hline 70}

	. {cmd:_}

{pstd}
Exiting Mata does not clear it:

	. {cmd:mata}
	{hline 35} mata (type {cmd:end} to exit} {hline 10}
	: {cmd:x = 2}

	: {cmd:y = (3+2i)}

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

	: {cmd:end}
	{hline 70}

	. ...

	. {cmd:mata}
	{hline 35} mata (type {cmd:end} to exit} {hline 10}
	: {cmd:mata describe}
	
              {txt}# bytes   type                       name and extent
        {hline 70}
        {res}           34   {txt}transmorphic matrix        {res}add{txt}()
        {res}            8   {txt}real scalar                {res}x
                   16   {txt}complex scalar             {res}y
        {txt}{hline 70}

	: {cmd:end}

{pstd}
Exiting Stata clears Mata, as does Stata's {bf:{help clear}} command;
see {bf:{help clear:[D] drop}}.


{title:Also see}

{psee}
Manual:  {bf:[M-1] first}

{psee}
Online:  
{bf:{help mata:[M-0] intro}},
{bf:{help m1_intro:[M-1] intro}}
{p_end}

⌨️ 快捷键说明

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