This vignette is adapted from the official Armadillo documentation.
Constants
The reference for these constants are National Institute of Standards and Technology (1994) and Wolfram Research (2009).
Expression | Description |
---|---|
datum::tau |
The ratio of any circle’s circumference to its radius () |
datum::pi |
The ratio of any circle’s circumference to its diameter () |
datum::inf |
Infinity () |
datum::nan |
Not a number |
datum::eps |
Machine dependent epsilon () |
datum::e |
Base of the natural logarithm () |
datum::sqrt2 |
Square root of two () |
datum::log_min |
Type and machine dependent log of minimum non-zero value |
datum::log_max |
Type and machine dependent log of maximum value |
datum::euler |
Euler-Mascheroni constant () |
datum::gratio |
Golden ratio ($ $) | | `datum::m_u` | Atomic mass constant in kilograms ($m_u ^{-27} $) | | `datum::N_A` | Avogadro constant ($N_A ^{23} ^{-1}$) | | `datum::k` | Boltzmann constant in joules per kelvin ($k ^{-23} $) | | `datum::k_evk` | Boltzmann constant in eV/K ($k ^{-5} $) | | `datum::a_0` | Bohr radius in meters ($a_0 ^{-11} $) | | `datum::mu_B` | Bohr magneton ($_B ^{-24} $) | | `datum::Z_0` | Characteristic impedance of vacuum in ohms ($Z_0 $) | | `datum::G_0` | Conductance quantum in siemens ($G_0 ^{-5} $) | | `datum::k_e` | Coulomb's constant in meters per farad ($k_e ^9 $) | | `datum::eps_0` | Electric constant in farads per meter ($_0 ^{-12} $) | | `datum::m_e` | Electron mass in kilograms ($m_e ^{-31} $) |
datum::eV |
Electron volt in joules () |
datum::ec |
Elementary charge in coulombs () |
datum::F |
Faraday constant in coulombs () |
datum::alpha |
Fine-structure constant () |
datum::alpha_inv |
Inverse fine-structure constant () |
datum::K_J |
Josephson constant () |
datum::mu_0 |
Magnetic constant in henries per meter () |
datum::phi_0 |
Magnetic flux quantum in webers () |
datum::R |
Molar gas constant in joules per mole kelvin () |
datum::G |
Newtonian constant of gravitation in newton square meters per kilogram squared () |
datum::h |
Planck constant in joule seconds () |
datum::h_bar |
Reduced Planck constant in joule seconds () |
datum::m_p |
Proton mass in kilograms () |
datum::R_inf |
Rydberg constant in reciprocal meters () |
datum::c_0 |
Speed of light in vacuum in meters per second () |
datum::sigma |
Stefan-Boltzmann constant () |
datum::R_k |
von Klitzing constant in ohms () |
datum::b |
Wien wavelength displacement law constant () |
The constants are stored in the Datum<type>
class, where type
is either float
or double
. For convenience, Datum<double>
is typedefed as datum
, and Datum<float>
is typedefed as fdatum
.
Caveats:
-
datum::nan
is not equal to anything, even itself. - To check whether a scalar
x
is finite, usestd::isfinite(x)
.
These caveats mean that
double x = datum::pi;
double y = datum::nan;
bool is_nan_y = (y == datum::nan); // false
// this block will lead to an arithmetic error
if (is_nan_y == false) {
return x + y; // pi + nan
}
Wall clock
The wall_clock
class is a simple timer class for measuring the number of elapsed seconds. An instance of the class has two member functions:
-
.tic()
: start the timer -
.toc()
: return the number of seconds since the last call to.tic()
Random number generator
The random number generator (RNG) is based on the Mersenne Twister algorithm. The RNG is thread-safe, and each thread has its own RNG state.
Usage:
set_seed(integer); set_seed_random();
The set_seed()
function sets the RNG seed to the specified value.
The set_seed_random()
function sets the RNG seed to a value drawn from std::random_device
(if the reported entropy is not zero), or /dev/urandom
for Linux and macOS, or based on the current time (on systems without /dev/urandom
).
Caveat:
- When using a multi-threading framework (such as OpenMP) and the underlying system supports the
thread_local
storage specifier, the above functions change the seed only within the thread they are running on.
To change the seeds on all OpenMP threads to the same value, adapt the following code:
#pragma omp parallel
{123);
set_seed(// some computation
}
To change the seeds on all OpenMP threads to unique values, adapt the following code:
std::atomic<std::size_t> counter(0);
#pragma omp parallel
{123 + counter++);
set_seed(// some computation
}
Unsigned and signed integers
The uword
class is a typedef for an unsigned integer type. It is used for matrix indices as well as all internal counters and loops.
The sword
class is a typedef for a signed integer type.
The minimum width of both uword
and sword
is either 32 or 64 bits:
- The default width is 32 bits on 32-bit platforms.
- The default width is 64 bits on 64-bit platforms.
- The default width is 32 bits when using Armadillo in the R environment on either 32-bit or 64-bit platforms.
The width can also be forcefully set to 64 bits by enabling ARMA_64BIT_WORD
by editing armadillo/config.hpp
.
Short forms for complex data types
The cx_double
class is a convenience short form (typedef) for the complex element type std::complex<double>
.
The cx_float
class is a convenience short form (typedef) for the complex element type std::complex<float>
.
Example:
cpp11::register]] list cx_double_example_() {
[[3.4, 5.6);
cx_double z(return as_complex(z);
}
National Institute of Standards and Technology. 1994. “Fundamental Physical Constants from NIST.” https://physics.nist.gov/cuu/Constants/.
Wolfram Research. 2009. “WolframAlpha.” https://www.wolframalpha.com.