epinowcast
Loading...
Searching...
No Matches
utils.stan File Reference

Go to the source code of this file.

Functions

real log_dot_product (vector x, vector y)
 
int num_nonzero (matrix x)
 

Function Documentation

◆ log_dot_product()

real log_dot_product ( vector  x,
vector  y 
)

Compute dot product on the log scale

Calculates the dot product of two vectors on the log scale efficiently. This function is designed to handle operations where the vectors are represented in log space, avoiding potential underflow issues that can arise with standard dot product calculations on the natural scale, especially with small values.

Parameters
xFirst vector in log space.
ySecond vector in log space.
Returns
The log of the dot product of the two vectors.
Note
The function computes the dot product as follows:
  1. Element-wise sum of x and y (since they are in log space).
  2. Application of log_sum_exp to the result, which efficiently calculates the logarithm of the sum of exponentials, corresponding to the log of the dot product.

This approach is particularly useful in statistical contexts where log-space operations are common, such as in the computation of log-likelihoods.

Definition at line 26 of file utils.stan.

◆ num_nonzero()

int num_nonzero ( matrix  x)

Count the number of non-zero elements in a matrix

This function iterates through all elements of a given matrix and counts the number of elements that are not equal to zero.

Parameters
xThe input matrix to be analyzed.
Returns
An integer representing the count of non-zero elements in the matrix.
Note
This function may be useful in contexts where the sparsity of a matrix needs to be quantified, such as in sparse matrix operations or in assessing the efficiency of certain matrix algorithms.

Definition at line 44 of file utils.stan.