📄 mf_trace.hlp
字号:
{smcl}
{* 25mar2005}{...}
{cmd:help mata trace()}
{hline}
{* index trace of matrix}{...}
{* index trace()}{...}
{title:Title}
{p 4 4 2}
{bf:[M-5] trace() -- Trace of square matrix}
{title:Syntax}
{p 8 12 2}
{it:numeric scalar}
{cmd:trace(}{it:numeric matrix A}{cmd:)}
{p 8 12 2}
{it:numeric scalar}
{cmd:trace(}{it:numeric matrix A}{cmd:,}
{it:numeric matrix B}{cmd:)}
{p 8 12 2}
{it:numeric scalar}
{cmd:trace(}{it:numeric matrix A}{cmd:,}
{it:numeric matrix B}{cmd:,}
{it:real scalar t}{cmd:)}
{title:Description}
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:)}
returns the sum of the diagonal elements of {it:A}.
Returned result is real if {it:A} is real, complex if {it:A} is complex.
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:)}
returns trace({it:A}{it:B}), the calculation being made without calculating
or storing the off-diagonal elements of {it:A}{it:B}.
Returned result is real if {it:A} and {it:B} are real, and complex otherwise.
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:,} {it:t}{cmd:)}
returns trace({it:A}{it:B}) if {it:t}==0, and returns
trace({it:A}{bf:'}{it:B}{cmd:)} otherwise, where, if either {it:A} or {it:B}
is complex, transpose is understood to mean conjugate transpose.
Returned result is real if {it:A} and {it:B} are real, and complex otherwise.
{title:Remarks}
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:)}
returns the same result as
{cmd:trace(}{it:A}{cmd:*}{it:B}{cmd:)}, but is more efficient if you do
not otherwise need to calculate {it:A}{cmd:*}{it:B}.
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:, 1)}
returns the same result as
{cmd:trace(}{it:A}{cmd:'}{it:B}{cmd:)}, but is more efficient.
{p 4 4 2}
For real matrices {it:A} and {it:B},
trace({it:A}{bf:'}) = trace({it:A})
trace({it:A}{it:B}) = trace({it:B}{it:A})
{p 4 4 2}
and for complex matrices,
trace({it:A}{bf:'}) = conj(trace({it:A}))
trace({it:A}{it:B}) = trace({it:B}{it:A})
{p 4 4 2}
where, in the case of complex matrices, transpose is understood to mean
conjugate transpose.
{p 4 4 2}
Thus for real matrices,
to calculate code
{hline 35}
trace({it:AB}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:)}
trace({it:A}{bf:'}{it:B}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:, 1)}
trace({it:AB}{bf:'}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:, 1)}
trace({it:A}{bf:'}{it:B}{bf:'}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:)}
{hline 35}
{p 4 4 2}
and, for complex matrices,
to calculate code
{hline 41}
trace({it:AB}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:)}
trace({it:A}{bf:'}{it:B}) {cmd:trace(}{it:A}{cmd:,} {it:B}{cmd:, 1)}
trace({it:AB}{bf:'}) {cmd:conj(trace(}{it:A}{cmd:,} {it:B}{cmd:, 1))}
trace({it:A}{bf:'}{it:B}{bf:'}) {cmd:conj(trace(}{it:A}{cmd:,} {it:B}{cmd:))}
{hline 41}
Transpose in the first column means conjugate
transpose
{title:Conformability}
{cmd:trace(}{it:A}{cmd:)}:
{it:A}: {it:n x n}
{it:result}: 1 {it:x} 1
{cmd:trace(}{it:A}, {it:B}{cmd:)}:
{it:A}: {it:n x m}
{it:B}: {it:m x n}
{it:result}: 1 {it:x} 1
{cmd:trace(}{it:A}, {it:B}, {it:t}{cmd:)}
{it:A}: {it:n x m} if {it:t}==0
{it:m x n} otherwise
{it:B}: {it:m x n}
{it:t}: 1 {it:x} 1
{it:result}: 1 {it:x} 1
{title:Diagnostics}
{p 4 4 2}
{cmd:trace(}{it:A}{cmd:)}
aborts with error if {it:A} is not square.
{p 4 4 2}
{cmd:trace(}{it:A}, {it:B}{cmd:)} {cmd:trace(}{it:A}, {it:B},
{it:t}{cmd:)} abort with error if the matrices are not conformable or their
product is not square.
{p 4 4 2}
The trace of a 0 {it:x} 0 matrix is 0.
{title:Source code}
{p 4 4 2}
{view trace.mata, adopath asis:trace.mata}
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-5] trace()}
{p 4 13 2}
Online: help for
{bf:{help m4_matrix:[M-4] matrix}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -