LibCapy - capymath

Mathematical functions.

Macros:

Macro to define a local CapyVec of dimension 3 and avoid memory allocation

Macro to define a local CapyMat of dimension 3x3 and avoid memory allocation

Enumerations:

None.

Typedefs:

None.

Struct CapyPeasantMulDivRes :

Struct CapyPeasantMulDivRes's properties:

Struct CapyPeasantMulDivRes's methods:

None.

Struct CapyVec :

Struct CapyVec's properties:

Struct CapyVec's methods:

None.

Struct CapyMat :

Struct CapyMat's properties:

Struct CapyMat's methods:

None.

Struct CapyFiboLattice :

Struct CapyFiboLattice's properties:

Number of points in the lattice

Array of points. For grid lattice, the points are in {[0,1],[0,1]}. For polar lattice, the points are in {[0,1],[0,2pi]} For lattice on sphere and demi-sphere the points are in {[0,pi],[0,2pi]}

Struct CapyFiboLattice's methods:

None.

Struct CapyPiecewiseGaussian :

Struct CapyPiecewiseGaussian's properties:

Struct CapyPiecewiseGaussian's methods:

None.

Functions:

Get the GCD of two positive integers

Input argument(s):

a, b: the two integers

Output and side effect(s):

Return the greatest common divisor using the Stein's algorithm

Peasant multiplication of two integers

Input argument(s):

a, b: the two integers to multiply

Output and side effect(s):

Return the multiplication of two integers using the Peasant method.

Generic smooth step function.

Input argument(s):

x: input, in [0.0, 1.0]

a: smoothing coefficient in ]0.0, +inf]

Output and side effect(s):

Return the smoothed value of x. If a equals 1.0, it's x itself. As a gets lower than 1.0, the smoothed value varies following the pattern fast-slow-fast. As a gets greater than 1.0, the smoothed value varies following the pattern slow-fast-slow. Continuous but not necessary derivable at x=0.0 or x=1.0.

Smoother step function Inputs: x: the input value in [0,1]

Output and side effect(s):

Return the smoothed value, in [0, 1]

Power function for an integer value and integer exponent

Input argument(s):

x: the value

n: the power

Output and side effect(s):

Return x^n

Power function for a real value and integer exponent

Input argument(s):

x: the value

n: the power

Output and side effect(s):

Return x^n

LERP function, map a double value linearly from a range to another

Input argument(s):

x: the input

from: range of the input

to: range of the output

Output and side effect(s):

Return the mapped intput

LERP function, map a double value linearly from [0, 1] to an array

Input argument(s):

x: the input

to: array of two values

Output and side effect(s):

Return the mapped intput

Allocate an array of CapyVec

Input argument(s):

dim: the dimension of the vectors

nb: the size of the array

Output and side effect(s):

Return a newly allocated array of CapyVec (values initialised to 0.0) Exceptions: May raise CapyExc_MallocFailed

Free an array of CapyVec

Input argument(s):

that: the array of CapyVec

nb: the size of the array

Create a CapyVec

Input argument(s):

dim: the dimension of the vector

Output and side effect(s):

Return a CapyVec (values initialised to 0.0) Exceptions: May raise CapyExc_MallocFailed

Free a CapyVec

Input argument(s):

that: the CapyVec to free

Get a 3D vector orthogonal to another 3D vector

Input argument(s):

u: the input vector

v: the output vector orthonormal to u

Output and side effect(s):

v is updated

Create a CapyMat

Input argument(s):

nbCol: the number of columns of the matrix

nbRow: the number of rows of the matrix

Output and side effect(s):

Return a CapyVec (values initialised to 0.0) Exceptions: May raise CapyExc_MallocFailed

Create the 3x3 CapyMat for the rotation matrix around the i-th axis and given angle (same handness for the rotation as for the coordinates system)

Input argument(s):

iAxis: the axis index (0: x, 1: y, 2: z)

theta: the angle in radians

Output and side effect(s):

Return a CapyVec Exceptions: May raise CapyExc_MallocFailed, CapyExc_UndefinedExecution

Free a CapyMat

Input argument(s):

that: the CapyMat to free

Add two vectors

Input argument(s):

a: first vector

b: second vector

c: result vector, c=a+b (c can be a or b)

Substract two vectors

Input argument(s):

a: first vector

b: second vector

c: result vector, c=a-b

Dot product of two vectors

Input argument(s):

a: first vector

b: second vector

c: result vector, c=a.b

Cross product of two vectors of dimension 3

Input argument(s):

a: first vector

b: second vector

c: result vector, c=a*b

Product vector scalar

Input argument(s):

a: vector

b: scalar

c: result vector, c=a*b

Normalise the vector

Input argument(s):

a: the vector

Output and side effect(s):

The vector is normalised.

Normalise the vector using the fast inverse square root

Input argument(s):

a: the vector

Output and side effect(s):

The vector is normalised.

Get the norm of the vector

Input argument(s):

a: the vector

Output and side effect(s):

Retun the norm of the vector

Get the cosine-similarity of two vectors

Input argument(s):

a: the first vector

b: the second vector

Output and side effect(s):

Retun the dot product of normalised vectors

Get the approximated norm of a 2D vector

Input argument(s):

u: the 2D vector

Output and side effect(s):

Retun the approx norm of the vector (equals to 0.96x+0.4y where x>=y>=0) accurate within 4%

Get the angle of a 2D vector

Input argument(s):

u: the 2D vector

Output and side effect(s):

Return the angle of the vector (relative to x, ccw) in [-M_PI, M_PI]

Set the angle of a 2D vector

Input argument(s):

a: vector

b: angle in radians

c: result vector, c=a*b

Output and side effect(s):

Set the angle to 'b' (relative to x, ccw) while conserving the distance of 'a' and store the result in 'c' (which can be 'a')

Get the squared norm of the vector

Input argument(s):

a: the vector

Output and side effect(s):

Retun the squared norm of the vector

Get the moment of a vector's values

Input argument(s):

u: the vector

c: the center of the moment

n: the order of the moment

Output and side effect(s):

Return the moment. CapyVecGetMoment(u, 0, 0) is the sum of u's values, aka total mass. CapyVecGetMoment(u, 0, 1) is the mean of u's values, aka first raw moment. CapyVecGetMoment(u, mean(u), 2) is the variance of u's values, aka second centered moment, or square of the standard deviation sigma. CapyVecGetMoment(u, mean(u), 3) is the skewness, aka third centered moment. CapyVecGetMoment(u, mean(u), 4) is the kurtosis, aka fourth centered moment. Standardized moment of order n (aka normalized n-th central moment) is equal to: CapyVecMoment(u, mean(u), n) / sqrt(CapyVecMoment(u, mean(u), 2))^n

Get the covariance of two vectors' values. The two vectors must have same dimension.

Input argument(s):

u: the first vector

v: the second vector

Output and side effect(s):

Return the covariance, equal to E[(u-E(u)(v-E(v))].

Exception(s):

May raise CapyExc_InvalidParameters.

Get the Pearson correlation of two vector's values. The two vectors must have same dimension.

Input argument(s):

u: the first vector

v: the second vector

Output and side effect(s):

Return the covariance (in [-1,1]), equal to cov(u, v)/(sigma(u)*sigma(v)).

Exception(s):

May raise CapyExc_InvalidParameters.

Get the distance covariance of two vector's values (seen as univariate variables). The two vectors must have same dimension.

Input argument(s):

u: the first vector

v: the second vector

Output and side effect(s):

Return the distance covariance.

Exception(s):

May raise CapyExc_InvalidParameters.

Get the distance correlation of two vector's values (seen as univariate variables). The two vectors must have same dimension.

Input argument(s):

u: the first vector

v: the second vector

Output and side effect(s):

Return the covariance (in [0,1]).

Exception(s):

May raise CapyExc_InvalidParameters.

Apply the softmax function to a vector

Input argument(s):

u: the vector

t: 'temperature'

Output and side effect(s):

The vector is updated. The temperature must be >0.0. A temperature of 1.0 gives the standard softmax function. The higher the temperature the more uniformly distributed the result vector is. A temperature value infinitely small produces a vector with value 1.0 for the max value of the input, and 0.0 for all other values.

Product matrix vector

Input argument(s):

a: matrix

b: vector

c: result vector, c=a*b

Product matrix matrix

Input argument(s):

a: matrix

b: matrix

c: result matrix, c=a*b

Product scalar matrix

Input argument(s):

a: matrix

b: scalar

c: result matrix, c=a*b

Add matrix matrix

Input argument(s):

a: matrix

b: matrix

c: result matrix, c=a+b

Transpose matrix

Input argument(s):

a: matrix

b: result transpose matrix

Get the determinant of a matrix a: matrix b: result determinant

Pseudo inverse matrix (Moore-Penrose inverse)

Input argument(s):

a: matrix

b: result pseudo inverse matrix

Exceptions: May raise CapyExc_MatrixInversionFailed, CapyExc_MallocFailed

Inverse matrix (if the matrix is not square the result is the pseudo inverse)

Input argument(s):

a: matrix

b: result inverse matrix

Exceptions: May raise CapyExc_MatrixInversionFailed, CapyExc_MallocFailed

Get the QR decomposition of a matrix cf http://www.seas.ucla.edu/~vandenbe/133A/lectures/qr.pdf

Input argument(s):

m: the matrix to decompose (nbRow>=nbCol)

q: the result Q matrix (same dimensions as m)

r: the result R matrix (dimensions: m.nbCol, m.nbCol)

Exception(s):

May raise CapyExc_QRDecompositionFailed, CapyExc_MallocFailed.

Get the Eigen values and vectors of a matrix cf http://madrury.github.io/ jekyll/update/statistics/2017/10/04/qr-algorithm.html

Input argument(s):

m: the matrix (nbRow==nbCol)

eigenVal: the Eigen values (from largest to smallest in absolute value, dim==m.nbCol)

eigenVec: the Eigen vectors (same order as Eigen values, same dimension as m, one per column)

Output and side effect(s):

eigenVec and eigenVal are updated with the result.

Exception(s):

May raise CapyExc_QRDecompositionFailed, CapyExc_MallocFailed.

Set a matrix to the identity.

Input argument(s):

m: the matrix (may be rectangular)

Copy a matrix to another.

Input argument(s):

src: the matrix to be copied

dest: the matrix updated

Get the moment of a column of a matrix

Input argument(s):

u: the vector

iCol: the column index

c: the center of the moment

n: the order of the moment

Output and side effect(s):

Return the moment (cf CapyVecGetMoment)

Calculate (a^b)%c using the square and multiply algorithm

Input argument(s):

a,b,c: the value of a, b and c in (a^b)%c

Ouput: Return (a^b)%c

Return the Fibonacci sequence up to the 'n'-th element.

Input argument(s):

n: the number of elements

Output and side effect(s):

Return the Fibonnaci sequence in newly allocated array.

Return the index in the Fibonacci sequence of the smallest value greater or equal than a given value.

Input argument(s):

val: the value

Output and side effect(s):

Return the index in the Fibonnaci sequence. (i.e: CapyFibonacciIdx(10)=6)

Free a CapyFiboLattice

Input argument(s):

that: the CapyFiboLattice to free

Return the Fibonacci grid lattice for the 'n'-th Fibonacci number

Input argument(s):

n: the Fibonacci number

Output and side effect(s):

Return the lattice.

Return the Fibonacci polar lattice for the 'n'-th Fibonacci number

Input argument(s):

n: the Fibonacci number

Output and side effect(s):

Return the lattice.

Get the polar coordinates of n points uniformly distributed on a demi-sphere using the Fibonacci sequence

Input argument(s):

n: the number of points

Output and side effect(s):

Return the lattice. points[2i] in [0,pi] and points[2i+1] in [0,2pi]

Get the polar coordinates of n points uniformly distributed on a sphere using the Fibonacci sequence

Input argument(s):

n: the number of points

Output and side effect(s):

Return the lattice. points[2i] in [0,pi] and points[2i+1] in [0,2pi]

Solve the quadratic equation a+bx+cx^2=0

Input argument(s):

coeffs: the coefficients of the equation (in order a,b,...)

roots: array of size 2 to memorise the roots

Output and side effect(s):

Return true and update 'roots' (sorted by increasing values) if there is a solution, else return false and leave 'roots' unchanged. If there are less roots than the maximum possible number, the smallest root is repeated to fill in 'roots'.

Solve the cubic equation a+bx+cx^2+d^3=0

Input argument(s):

coeffs: the coefficients of the equation (in order a,b,...)

roots: array of size 3 to memorise the roots

Output and side effect(s):

Return true and update 'roots' (sorted by increasing values) if there is a solution, else return false and leave 'roots' unchanged. If there are less roots than the maximum possible number, the smallest root is repeated to fill in 'roots'.

Solve the quartic equation a+bx+cx^2+dx^3+ex^4=0

Input argument(s):

coeffs: the coefficients of the equation (in order a,b,...)

roots: array of size 4 to memorise the roots

Output and side effect(s):

Return true and update 'roots' (sorted by increasing values) if there is a solution, else return false and leave 'roots' unchanged. If there are less roots than the maximum possible number, the smallest root is repeated to fill in 'roots'.

Get the approximated inverse square root of a number using the Quake algorithm (cf https://en.wikipedia.org/wiki/Fast_inverse_square_root)

Input argument(s):

x: the number

Output and side effect(s):

Return 1/sqrt(x).

Convert from degree to radians

Input argument(s):

theta: the angle in degree

Output and side effect(s):

Return the ange in radians.

Convert from radians to degree

Input argument(s):

theta: the angle in radians

Output and side effect(s):

Return the ange in degree.

Piecewise Gaussian evaluation

Input argument(s):

x: argument of the Gaussian

gauss: the Gaussian

Output and side effect(s):

Return the value of the piecewise Gaussian at the requested argument

Ackley's function

Input argument(s):

in: 2D input

out: 1D output

Output and side effect(s):

'out' is updated. Cf https://en.wikipedia.org/wiki/Ackley_function

Himmelblau's function

Input argument(s):

in: 2D input

out: 1D output

Output and side effect(s):

'out' is updated. Cf https://en.wikipedia.org/wiki/Himmelblau%27s_function

Check if a position is inside an ellipse (aligned with cooridnate system)

Input argument(s):

pos: the position to check

center: the center of the ellipse

dims: the dimensions of the ellise

Output and side effect(s):

Return true if pos is inside the ellipse, false else

Calculate the value of a cubic Bezier curve Inputs: t: argument of the function, in [0, 1] params: the 4 control values of the Bezier

Output and side effect(s):

Return the value of the Bezier