# AD Model Builder/Cheat sheet

Jump to: navigation, search

ADMB Handy operations and functions (aka ADMB cheat sheet)

Adapted from a Word document written by Carolina Minte-Vera.

Let V, X, Y be vectors and vi, xi, yi be the elements of the vectors. Let M, N, A be matrices and mij, nij, aij be the elements of the matrices. Let α be a scalar.

## Matrix and vector operations

Operation Math notation ADMB slow code ADMB fast code
Sum of two vectors vi = xi + yi  V = X + Y   X+=Y
Sum of scalar to a vector vi = xi + α  V = X + nu   X+=nu
Sum of two matrices aij = mij + nij  A = M + N  
Sum of a scalar to a matrix aij = mij + α  A = M +alpha   M+=alpha
Subtract two vectors vi = xiyi  V = X - Y   X-=Y
Subtract a scalar from a vector vi = xi − α  V=X-alpha   X-=alpha
Subtract a scalar from a matrix vi = xi − α  A=M-alpha   M-=alpha
Subtract two matrices aij = mijnij  A=M-N   M-=N
Vector dot product
 α = ∑ xiyi i
 alpha=X*Y  
Outer product of two vectors mij = xiyj  M=outer_prod(X,Y)  
Multiply a scalar by a vector xi = αyi  X=alpha*Y   Y*=alpha
Multiply a vector by a scalar xi = yiα  X=Y*alpha  
Multiply a scalar by a matrix mij = αaij  A=alpha*M   M*=alpha
Multiply a vector by a matrix
 xj = ∑ yimij i
 X=Y*M  
Multiply a matrix by a vector
 xi = ∑ mijyi j
 X=M*Y  
Multiply two matrices
 aij = ∑ miknkj k
 A=M*N  
Division of vector by scalar yi = xi / α  Y=X/alpha   X/=alpha
Division of scalar by a vector yi = α / xi  Y=alpha/X  
Division of a matrix by a scalar mij = aij / α  M=N/alpha  

## Element–wise (e-w) operations in a matrix or vector object

Operation [need objects of the same dimension] Math notation ADMB code
Vectors e-w multiplication vi = xiyi  V=elem_prod(X,Y)
Vectors e-w division vi = xi / yi  V=elem_div(X,Y)
Matrices e-w multiplication mij = aijnij  M=elem_prod(A,N)
Matrices e-w division mij = aij / nij  M=elem_div(A,N)
Vectors e-w multiplication vi = xiyi  V=elem_prod (X,Y)

## Other matrix or vector operations

Operation Math notation ADMB code
Norm of a vector $\alpha=\sqrt{\sum_{i} v_i^2}$  alpha=norm(V)
Norm square of a vector $\alpha=\sum_{i} v_i^2$  alpha=norm2(V)
Norm of a matrix $\alpha=\sqrt{\sum_{i} \sum_{j} m_{ij}^2}$  alpha=norm(M)
Norm square of a matrix $\alpha=\sum_{i} \sum_{j} m_{ij}^2$  alpha=norm2(M)
Sum over elements of a vector
 α = ∑ vi i
 alpha=sum(V)
Row sums of a matrix object
 xi = ∑ mij j
 X=rowsum(M)
Column sum of a matrix object
 yj = ∑ mij i
 Y=colsum(M)
Concatenation X = (x1,x2,x3),Y = (y1,y2),V = (x1,x2,x3,y1,y2)  V=X&Y

## Still more matrix or vector operations

Operation ADMB code
Determinant (must be a square matrix cols=rows)  alpha=det(M)
Logarithm of the determinant? (must be a square matrix cols=rows), sgn is an integer. Not explained in the manual, but used in the codes.  alpha=ln_det(M,sgn)
Inverse (must be a square matrix cols=rows)  N=inv(M)
Minimum value of a vector object  alpha=min(V)
Maximum value of a vector object  alpha=max(V)
Eigenvalues of a symmetric matrix  V=Eigenvalues(M)
Eigenvectors of a symmetric matrix  N=eigenvectors(M)
Identity matrix function  M=identity_matrix(int min, int max)

## Useful functions

Function Action
 current_phase()  return an integer that is the value of the current phase
 last_phase()  return a binary: "true" if the current phase is the last phase and "false" (=0) otherwise
 Active(Par)  return a binary: "true" if the parameter Par is active in the current phase and "false" (=0) otherwise
 mceval_phase()  return a binary: "true" if the current phase is the mceval phase and "false" (=0) otherwise
 bolinha.initialize()  Initializes (sets all elements equal to zero) the object bolinha