Polynomials and random generators#

Polynomial roots#

XLiFE++ provides tools to find roots of polynomials of degree 2, 3, 4:

\[[a_4x^4]+[a_3x^3]+a_2x^2+a_1x+a_0 = 0\]

You can use

  • the quadratic() function for degree 2 polynomials,

  • the cardan() function for degree 3 polynomials,

  • the ferrari() function for degree 4 polynomials.

These routines deal with real or complex polynomials and return always a vector of complexes (Complexes object):

Real a0, a1, a2 , a3, a4;
Complex c0, c1, c2, c3, c4;
Complexes roots;
roots = quadratic(a2, a1, a0);
roots = cardan(a3, a2, a1, a0);
roots = ferrari(a4, a3, a2, a1, a0);
roots = quadratic(c2, c1, c0);
roots = cardan(c3, c2, c1, c0);
roots = ferrari(c4, c3, c2, c1, c0);

Random generators#

XLiFE++ offers an uniform distribution generator and a normal distribution generator. As C++11 provides a lot of random generators, contrary to C98 that proposes only a uniform distribution generator, the front end user functions will use prior the C++11 generators if C++11 is available (default behaviour from gcc 6.1). These front end functions are the following:

Real uniformDistribution(Real a=0., Real b=1.);    // uniform distribution on [a,b[
Real normalDistribution(Real mu=0., Real sigma=1.);// normal distribution (mu,sigma)

a, b, mu, sigma have the specified default values if they are not given. For instance to build a vector of random values of size 100:

Reals us(100), ns(100);
for(Number i=1; i<=100; i++) us(i) = uniformDistribution();
for(Number i=1; i<=100; i++) ns(i) = normalDistribution(2.,5.);//normal mu=2,sigma=5

Random vectors or random matrices can be constructed in a direct way :

Reals us = uniformDistribution<Real>(100);      // uniform on [0,1[
Reals ns = normalDistribution<Real>(100,2.,5.); // normal distribution mu=2,sigma=5
RealMatrix mn = normalDistribution<Real>(10,10);// normal distribution mu=0,sigma=1

Hint

Works also with complex vectors or complex matrices.

Warning

Because of multiple random functions proposed, do not omit the dot in distribution parameters (Real) when they are passed explicitely.

Tip

There exist also functions addressing directly double or complex pointers to deal with C vectors, for instance:

Reals us(100);
uniformDistribution(&us[0],-1.,1.,100); //fill us using uniform distribution on [−1,1[

To address the C98 random generators when C++11 is on, use the “C” version of previous functions:

Real uniformDistributionC(Real a=0., Real b=1.); //C−style uniform distribution on [a,b[
Real normalDistributionC(Real mu=0., Real sigma=1.);//C−style normal distribution (mu,sigma)

In C-style, two normal distribution generators are available : the Marsiglia generator (default) ant the Box-Muller generator. They can be invoked by using keyword (_MarsagliaGenerator or _BoxMullerGenerator ) after normal distribution parameters:

Reals us(100):
normalDistributionC(&us[0],0.,1.,_BoxMullerGenerator,100);

Tip

Random generators are automatically initialized when starting XLiFE++ applications. Initialization value (the seed) is based on current time. It is possible to re-initialize random engines them with a given seed :

int myseed=1000;
initRandomGenerators(myseed);