Errata for The Mata Book: A Book for Serious Programmers and Those Who Want to Be

The errata for The Mata Book are provided below. Click here for an explanation of how to read an erratum. Click here to learn how to determine the printing number of a book.

(1) Chapter 3, p. 35, third code block
	toret.c1 = c.c1 / distance
	toret.c2 = c.c2 / distance
	toret.c1 = c.x / distance
	toret.c2 = c.y / distance
(1) Chapter 3, p. 37, bottom portion of code block
	real matrix regress::XX(real matrix X)
        ...
	real colvector regress::Xy(real colvector y, real matrix X)
        ...
	real matrix regression::XX(real matrix X)
        ...
	real colvector regression::Xy(real colvector y, real matrix X)
        ...
(1) Chapter 4, p. 51, first while loop example
	while  ( ... {
                    x = x + f(x)/fprime(x)
        }
	while  ( ... {
                    x = x - f(x)/fprime(x)
        }
(1) Chapter 4, p. 51, second while loop example
	while  ( ... {
                    x = x + (x^2-2)/(2*x)
        }
	while  ( ... {
                    x = x - (x^2-2)/(2*x)
        }
(1) Chapter 8, p. 124, first variant of "success"
	_______                _  _  ___
        success = fcn(A, B, C, D, E, ...)
                            -  -  -
	_______                _  _  ___
        success = fcn(A, B, C, D, E, ...)
                      -  -  -
(1) Chapter 8, p. 125, second code block
	real matrix myfcn(real matrix A, real scalar caller_i)
        ...
                 i = caller_i + 1
        ...
	real matrix myfcn(real matrix A, real scalar i)
        ...
                 caller_i = i + 1
        ...
(1) Chapter 8, p. 126, code at the top and bottom of page
	real matrix matrixmanip( ... |, ...
	real matrix matrixmanip( ... ,| ...
(1) Chapter 8, p. 126, first paragraph, first sentence
The vertical bar in front of the comma separates ... The vertical bar after the comma separates ...
(1) Chapter 8, p. 128, code
	... foo( ... |, ...
        {
                  if (eltype(x)... {
                               ...
                  } 
                  if (eltype(x)... {
                               ...
                  } 
        ...
	... foo( ... ,| ...
        {
                  if (eltype(one)...{
                               ...
                  } 
                  if (eltype(one)...{
                               ...
                  } 
        ...
(1) Chapter 9, p. 132, second sentence of second full paragraph
Here you just calculated \(n!(n-k)!/k!\) by using Here you just calculated \(n!/((n-k)! k!)\) by using
(1) Chapter 9, p. 137, second code block
	for (n=1; n<=20; n++) {
        ...
                  // printf("Ch(%f, %f) = %f\n", n, k,
                          n_choose_k(n,k))
        ...
	for (n=1; n<=5; n++) {
        ...
                  /* printf("Ch(%f, %f) = %f\n", n, k,
                          n_choose_k(n,k)) */
        ...
(1) Chapter 10, p. 164, ninth line of first code block
	r                    = obtainb(ln_in)
	r                    = obtainb(i)
(1) Chapter 10, p. 166, first line of last code block, and p. 167, first line of fifth code block
	void run_regression(..., real matrix y,
	void run_regression(..., real matrix X,
(1) Chapter 10, p. 173, first line of last code block
	void setup(struct simple scalar s) ...
	void setup(struct Simple scalar s) ...
(1) Chapter 10, p. 174, first line of first code block
	void setup2(pointer(struct simple scalar) scalar p) ...
	void setup2(pointer(struct Simple scalar) scalar p) ...
(1) Chapter 10, p. 175, first code block
	struct LrInputs
        {
                pointer ... *X
                pointer ... *y
        ...
	struct LrInputs
        {
                pointer ... X
                pointer ... y
        ...
(1) Chapter 11, p. 179, second line of first code block
        options.vce
options.predicted_values
        options.V
options.yhat
(1) Chapter 11, p. 210, second line of second, fifth, and sixth code block
       St=quadcrossdev(X,0,mean(X), X,0,mean(y))\0
       St=quadcrossdev(X,0,mean(X), y,0,mean(y))\0
(1) Chapter 11, p. 216, last code block
	b'   trub
	b' \ trub
(1) Chapter 12, p. 235, last code block
        transmorphic Foo::X(|real matrix userX)
        transmorphic Foo::X(|real matrix X)
(1) Chapter 12, p. 240, second code block
        real scalar Anova::LinReg() return(lr.Rsquared())
        real scalar Anova::Rsquared() return(lr.Rsquared())
(1) Chapter 14, p. 264, first code block
 ________
 distance = e.distance(`Position' pos1 `Position' pos2)
                                  ----            ----
 ________
 distance = e.distance(`Position' pos1, `Position' pos2)
                                  ----             ----
(1) Chapter 14, p. 267, second code block
...
real scalar myprogram(real matrix X, `VarV' v)
{
        `ObsS' scalar    i
        `VarS' scalar    j
...
...
real scalar myprogram(real matrix X, `VarV' v)
{
        `ObsS'     i
        `VarS'     j
...
(1) Chapter 14, p. 269, first code block
...
for  (i=1;  i<=rows(X) ;  i++) {
                  if  (X[i,i]==0)  return(0)
}
...
...
for  (i=1;  i<=rows(Xinv) ;  i++) {
                  if  (Xinv[i,i]==0)  return(0)
}
...
(1) Chapter 14, p. 269, second code block
...
for  (i=1;  i<=rows(X) ;  i++) {
                  if  (X[i,i]==0)  return(`False')
}
...
...
for  (i=1;  i<=rows(Xinv) ;  i++) {
                  if  (Xinv[i,i]==0)  return(`False')
}
...
(1) Chapter 14, p. 273, second line of first code block
The extra space should be removed in `PositionV'.
(1) Chapter 15, p. 279, first code block
tsquared = secs_per_year'^2
tsquared = `secs_per_year'^2
(1) Chapter 17, p. 299 and 301, first code block
real matrix R_SxS(class SpMat Scalar A, tA,
                  class SpMat scalar B, tB)
real matrix R_SxS(class SpMat scalar A, tA,
                  class SpMat scalar B, tB)
(1) Chapter 17, p. 302, first code block
...
class SpMat scalar S_RxS(A, tA, class SpMat B, tB)
...
class SpMat scalar S_SxS(class SpMat Scalar A, tA,
                         class SpMat scalar B, tB)
...
class SpMat scalar S_RxR(A, tA, B, tB)
...
class SpMat scalar S_RxS(real matrix A, tA, class SpMat scalar B, tB)
...
class SpMat scalar S_SxS(class SpMat scalar A, tA,
                         class SpMat scalar B, tB)
...
class SpMat scalar S_RxR(real matrix A, tA, real matrix B, tB)
(1) Chapter 17, p. 306, first code block
R = J(A.rows(), cols(B), 0)
R = J(rows(A), B.cols(), 0)
(1) Chapter 17, p. 306, second code block
R = J(A.rows(), cols(B), 0)
R = J(A.rows(), B.cols(), 0)
(1) Chapter 17, p. 308, top table
To be written: B.R_RxS(A  tB)   Sketch: S=RS (done)
To be written: B.S_RxS(A  tB)   Sketch: S=RS (done)
(1) Chapter 17, p. 320, bottom code block
void `Sp'::set_row(`RR' row_i, `boolean' transposed, `RS' i)
void `Sp'::set_row(`RR' row_i, `RS' i, `boolean' transposed)
(1) Chapter 18, p. 327, first code block
...
SB = SpMat()
SA.setup(1000, 1000)
SA.Put(., ., B)
...
SB = SpMat()
SB.setup(1000, 1000)
SB.Put(., ., B)
(1) Chapter 18, p. 331, second paragraph
We will start with R_SxR() and measure, ... We will start with R_RxS() and measure, ...
(1) Chapter 18, p. 342, third paragraph last sentence
A.AA.keys() does not count the number of nonzero values in A; ... A.AA.N() does not count the number of nonzero values in A; ...
(1) Chapter 18, p. 344, first code block
subscripts = sort(A.AA.keys(), (1,2))
for (k=1; k<=rows(subscripts); k+++) {
        i = subscript[1]
        j = subscript[2]
        a = A.get(i, j)
        ...
}
subscripts = sort(A.AA.keys(), (1,2))
for (k=1; k<=rows(subscripts); k++) {
        i = subscripts[1]
        j = subscripts[2]
        a = A.get(i, j)
        ...
}
(1) Chapter 18, p. 345, first code block
...
for (k=1; k<=length(jvalues): k++) {
...
...
for (k=1; k<=length(jvalues); k++) {
...
(1) Chapter 18, p. 347, third line of table
*jofh[3]       (1, 3)          1 x 3 vector
*jofh[3]       (1, 3)          1 x 2 vector
(1) Chapter 18, p. 347, first code block
...
for (el=1; el<=length(*jvalues): el++) {
...
...
for (el=1; el<=length(*jvalues); el++) {
...
(1) Chapter 18, p. 348, first code block
...
if (jvalues) for (el=1; el<=length(*jvalues): el++) {
...
...
if (jvalues) for (el=1; el<=length(*jvalues); el++) {
...
(1) Chapter 18, p. 349, first code block
...
class SpMat scalar  R         // result: R = A*B
...
...
real matrix  R               // result: R = A*B
...
(1) Chapter 18, p. 350 and 351, first code block on each page
...
class SpMat scalar  R
...
...
real matrix  R
...
(1) Chapter 18, p. 354, line 7 of cols_of_row() code
key should be keys so that the line is subscripts sort(A.AA.keys(), (1,2)).
(1) Chapter 18, p. 364, first code block
The name of the code block should be rows_of_col() instead of rows_of_col_ptr().
        ...
                 return(cols_of_row_ptr(!tA))
        ... 
        ...
                 return(cols_of_row(!tA))
        ...
(1) Chapter 18, p. 366, first code block
The assert command shown does not produce any results.
(1) Appendix A, p. 391, first sentence second to last paragraph
We are done ... "return scalar result = statistic" in xyzsubr(). We are done ... "return scalar statistic = r(result)" in xyzsubr().
(1) Appendix A, p. 397, second to last item in table
st_scalar()              clear Stata's s()
st_sclear()              clear Stata's s()
  
(1) Appendix B, p. 404–405, output
: z = (86+2i, 13-2i)

: Z = (z \ 12+2i, 22+14i)

: z
             1         2
    +---------------------+
  1 |  86 + 12i  13 - 2i  |
    +---------------------+

: Z
              1          2
    +-----------------------+
  1 |   86 + 12i   13 - 2i  |
  2 |   12 - 3i   22 + 14i  |
    +-----------------------+

: Z'
              1          2
    +-----------------------+
  1 |   86 - 12i    12 + 3i |
  2 |    13 + 2i   22 - 14i |
    +-----------------------+

: z = 1 + 2i

: conj(z)
  1 - 2i

: H = Z'Z

: H
[Hermitian]
                1            2
    +---------------------------+
  1 |        7693               |
  2 |  1316 + 94i          853  |
    +---------------------------+

: H[1,2]
  1316 - 94i

: H[2,1]      // note that Im(H[2,1]) == -Im(H[1,2])
  1316 + 94i
: z = (86+2i, 13-2i)

: Z = (z \ 12+2i, 22+14i)
  
: z 
             1         2
    +---------------------+
  1 |  86 + 2i   13 - 2i  |
    +---------------------+

: Z
              1          2
    +-----------------------+
  1 |   86 + 2i    13 - 2i  |
  2 |   12 + 2i   22 + 14i  |
    +-----------------------+

: Z'
              1          2
    +-----------------------+
  1 |   86 - 2i    12 - 2i  |
  2 |   13 + 2i   22 - 14i  |
    +-----------------------+

: z = 1 + 2i

: conj(z)
  1 - 2i

: H = Z'Z

: H
[Hermitian]
                1            2
    +---------------------------+
  1 |        7548               |
  2 |  1406 + 74i          853  |
    +---------------------------+

: H[1,2]
  1406 - 74i

: H[2,1]      // note that Im(H[2,1]) == -Im(H[1,2])
  1406 + 74i
  
(1) Appendix C, p. 414, last two items in table
p = &(st(c))       new 1 x c class cl "vector"
p = &(st(r, c))    new r x c class cl "matrix"
p = &(cl(c))       new 1 x c class cl "vector"
p = &(cl(r, c))    new r x c class cl "matrix"