# IBMPopSim C++ essentials

The arguments intensity_code, interaction_code and kernel_code of the events functions must contain some C++ code given by the user. You don't need to be a C++ guru to write the few instructions needed. These functions should use very little C++ syntax. They are essentially arithmetic or logical operations, tests and calls to predefined functions, which we give an overview below.

For code efficiency, you should not allocate memory in these functions (no new) or use type containers (std::vector, std::list, ...). If you think you need to allocate memory, consider as parameter an R vector that will be mapped via arma::vector (see below), or declare the variable as static. Also, it should not be necessary to make a loop. Keep in mind that these functions should be fast.

There are no C++ language restrictions so you can use all the functions of the standard C++11 library. However we detail in this section some functions that should be sufficient. For more details on C++ and Rcpp we recommend:

## C++ syntax

• Each statement must be ended by a semicolon.
• To create a variable, you must specify the type and assign it a value (type variable = value;). Here some examples:
int myNum = 5;               // Integer (whole number without decimals)
double myFloatNum = 5.99;    // Floating point number (with decimals)
char myLetter = 'D';         // Character
bool myBoolean = true;       // Boolean (true or false)

• bool data type can take the values true (1) or false (0).
• C++ supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
• The logical operators are: !, &&, ||
• The arithmetic operators are: +, -, *, /, %
• Compound assignment: +=, -=, *=, /=, %=
• Increment and decrement: ++, --
• Conditional ternary operator: ? : The conditional operator evaluates an expression, returning one value if that expression evaluates to true, and a different one if the expression evaluates as false. Its syntax is:
condition ? result1; : result2;

• Use the if, else if, else statements to specify a block of C++ code to be executed if one or more conditions are or not true.
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

• The syntax of the switch statement is a bit peculiar. Its purpose is to check for a value among a number of possible constant expressions. It is something similar to concatenating if-else statements, but limited to constant expressions. Its most typical syntax is:

switch (expression)
{
case constant1:
group-of-statements-1;
break;
case constant2:
group-of-statements-2;
break;
.
.
.
default:
default-group-of-statements
}

It works in the following way: switch evaluates expression and checks if it is equivalent to constant1; if it is, it executes group-of-statements-1 until it finds the break statement. When it finds this break statement, the program jumps to the end of the entire switch statement (the closing brace).

• When you know exactly how many times you want to loop through a block of code, use the for loop.

for (statement 1; statement 2; statement 3) {
// code block to be executed
}

• The while loop loops through a block of code as long as a specified condition is true.
while (condition) {
// code block to be executed
}

For more details we recommend a few pages of www.cplusplus.com about:

## Usual numeric functions

The most popular functions of the cmath library, which is included in the package, are the following:

• Exponential and logarithm functions: exp(x), log(x) (natural logarithm)
• Trigonometric functions: cos(x), sin(x), tan(x)
• Power functions: pow(x, a) meaning $x^a$ and sqrt(x) meaning $\sqrt{x}$
• Absolute value: abs(x)
• Truncation functions: ceil(x) meaning $\lceil x \rceil$ and floor(x) meaning $\lfloor x \rfloor$
• Bivariate functions: max(x, y) and min(x,y)

Note that these functions are not vectorial, the arguments x and y must be scalar. If the user wants to call some other functions of cmath not listed in the table, this is possible by adding the prefix std:: to the name of the function (scope resolution operator :: to access to functions declared in namespace standard std).

To facilitate the model creation, the individuals' characteristics and a list model parameters can be declared in the R environment and used in the C++ code.

The data shared between the R environment and the C++ code are:

• The characteristics of the individuals which must be atomic (Boolean, scalar or character).
• The model parameters: a list of variables of type:
• Atomic, vector or matrix.
• Predefined real functions of one variable, or list of such functions.
• Piecewise real function of two variables, of list of such.

### Atomic types (characteristics and parameters)

Here is the conversion table used between the atomic types of R and C++.

|C++ type|R type| |:---|:---| |bool|logical| |int|integer| |double|double| |char|character|

Individuals characteristics

The characteristics of an individual are defined by a named character vector containing the name of the characteristic and the associated C++ type. The function ?get_characteristics provides a way to extract the characteristics from a population data frame.