% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/math_functions.R
\name{math_functions}
\alias{math_functions}
\title{Mathematical and Matrix Utility Functions for RTMB Models}
\description{
This page summarizes the utility functions provided to assist in writing
efficient and numerically stable model code within `rtmb_code`. These functions
are specifically designed to be compatible with RTMB's Automatic Differentiation (AD).
}
\details{
\strong{1. Link and Inverse Functions:}
\itemize{
  \item \code{logit(x)}: Computes the logit transformation \code{log(x/(1-x))}.
  \item \code{inv_logit(x)}: Computes the inverse logit (logistic) transformation \code{1/(1+exp(-x))}.
}

\strong{2. Numerical Stability Utilities:}
These functions use specialized algorithms to prevent overflow/underflow in AD calculations.
\itemize{
  \item \code{log_sum_exp(x)}: Safely computes \code{log(sum(exp(x)))} using the log-sum-exp trick.
  \item \code{log1p_exp(x)}: Computes \code{log(1 + exp(x))} stably.
  \item \code{log1m_exp(x)}: Computes \code{log(1 - exp(x))} stably for \code{x < 0}.
  \item \code{log_softmax(x)}: Computes the log of the softmax function.
  \item \code{fabs(x)}: A smooth version of the absolute value function \code{abs(x)} to ensure differentiability at zero.
}

\strong{3. Matrix and Vector Transformations:}
Used to convert unconstrained vectors into structured matrices (e.g., for factor analysis or identification constraints).
\itemize{
  \item \code{sum_to_zero(x)}: Transforms a vector of length K-1 into a vector of length K that sums to zero.
  \item \code{to_lower_tri(x, M, D)}: Fills a matrix of size M x D with elements from vector \code{x} in a lower-triangular fashion.
  \item \code{to_centered_matrix(x, R, C)}: Creates an R x C matrix where each column sums to zero.
  \item \code{to_centered_tri(x, R, C)}: Creates an R x C matrix with column-wise sum-to-zero constraints on the lower elements (useful for identification in factor analysis).
}

\strong{4. Linear Algebra for AD:}
\itemize{
  \item \code{log_det_chol(L)}: Calculates the log-determinant of a covariance matrix from its Cholesky factor \code{L}.
  \item \code{quad_form_chol(x, L)}: Computes the quadratic form x^T Sigma^(-1) x using the Cholesky factor \code{L}.
  \item \code{distance(x, y)}: Computes the Euclidean distance between two vectors with a small epsilon for stability.
}
}
\concept{utilities}
