📄 mf_assert.hlp
字号:
{smcl}
{* 15mar2005}{...}
{cmd:help mata assert()}
{hline}
{* index assert()}{...}
{* index asserteq()}{...}
{title:Title}
{p 4 4 2}
{bf:[M-5] assert() -- Abort execution if false}
{title:Syntax}
{p 8 8 2}
{it:void}
{cmd:assert(}{it:real scalar r}{cmd:)}
{p 8 8 2}
{it:void}
{cmd:asserteq(}{it:transmorphic matrix A}{cmd:,}
{it:transmorphic matrix B}{cmd:)}
{title:Description}
{p 4 4 2}
{cmd:assert(}{it:r}{cmd:)} produces the error message
"{err:assertion is false}" and aborts with error if {it:r}==0.
{p 4 4 2}
{cmd:asserteq(}{it:A}{cmd:,} {it:B}{cmd:)} is logically equivalent to
{cmd:assert(}{it:A}{cmd:==}{it:B}{cmd:)}. If the assertion is false,
however, information is presented on the number of mismatches.
{title:Remarks}
{p 4 4 2}
In the midst of complicated code, you know that a certain calculation
must produce a result greater than 0, but you worry that perhaps you
have an error in your code:
...
{cmd:assert(n>0)}
...
{p 4 4 2}
In another spot, you have produced matrix {cmd:A} and know every element
of {cmd:A} should be positive or zero:
...
{cmd:assert(A:>=0)}
...
{p 4 4 2}
Once you are convinced that your function works, these verifications should be
removed. In a third part of your code, however, the problem is different
if the number of rows {it:r} exceed the number of columns {it:c}. In all the
cases you need to use it, however, {it:r} will be less than {it:c}, so you
are not much interested in programming the alternative solution:
...
{cmd:assert(rows(PROBLEM) < cols(PROBLEM))}
...
{p 4 4 2}
Leave that one in.
{title:Conformability}
{cmd:assert(}{it:r}{cmd:)}:
{it:r}: 1 {it:x} 1
{it:result}: {it:void}
{cmd:asserteq(}{it:A}{cmd:,} {it:B}{cmd:)}:
{it:A}: {it:r1 x c1}
{it:B}: {it:r2 x c2}
{it:result}: {it:void}
{title:Diagnostics}
{p 4 4 2}
{cmd:assert(}{it:r}{cmd:)} aborts with error if {it:r}==0.
{p 4 4 2}
{cmd:asserteq(}{it:A}{cmd:,} {it:B}{cmd:)} aborts with error if
{it:A}!={it:B}.
{title:Source code}
{p 4 4 2}
{view assert.mata, adopath asis:assert.mata},
{view asserteq.mata, adopath asis:asserteq.mata}
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-5] assert()}
{p 4 13 2}
Online: help for
{bf:{help m4_programming:[M-4] programming}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -