Math functions¶
qm.qua.lib.Math ¶
MSB
staticmethod
¶
Finds the index of the most significant bit in the parameter x. Notes:
- Result is independent of sign, for example, +3 and -3 will return the same msb
- The returned value will be the closet log2, rounded down.
This is given by \(\mathrm{floor}(\mathrm{log}_2(|x|))\).
For example:
- msb(0.1) will return -4.
- msb(5) will return 2.
- For an integer, msb(0) will return 0.
- For a fixed point number, msb(0) will return -28.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed or a QUA int
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[int]
|
a QUA int |
abs
staticmethod
¶
Computes the absolute value of x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA variable
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
aelu
staticmethod
¶
Computes faster an approximated Exponential Linear Unit activation function of x \(\mathrm{aELU}(x) \sim \mathrm{ELU}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
argmax
staticmethod
¶
Return the index of the maximum of an array
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[int]
|
the index of maximum value of array, a QUA Integer |
argmin
staticmethod
¶
Return the index of the minimum of an array
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[int]
|
the index of minimum value of array, a QUA Integer |
atan
staticmethod
¶
Computes \(\mathrm{atan}(x)\)
-- Available from QOP 2.4 --
| PARAMETER | DESCRIPTION |
|---|---|
x
|
the tangent ratio (opposite/adjacent)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
atan2
staticmethod
¶
Computes \(\mathrm{atan2}(y,x)\)
-- Available from QOP 2.4 --
| PARAMETER | DESCRIPTION |
|---|---|
y
|
the coordinate y (opposite)
TYPE:
|
x
|
the coordinate x (adjacent)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
atan2_2pi
staticmethod
¶
Computes \(\mathrm{1/2 \pi * atan2}(y,x)\)
-- Available from QOP 2.4 --
| PARAMETER | DESCRIPTION |
|---|---|
y
|
The coordinate y (opposite)
TYPE:
|
x
|
The coordinate x (adjacent)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
atan_2pi
staticmethod
¶
Computes \(\mathrm{1/2 \pi * atan}(x)\)
-- Available from QOP 2.4 --
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The tangent ratio (opposite/adjacent)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
cos
staticmethod
¶
Computes \(\mathrm{cos}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
the angle in radians
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
cos2pi
staticmethod
¶
Computes \(\mathrm{cos}(2 \pi x)\).
This is more efficient than Math.cos(\(2 \pi x\)).
In addition, this function is immune to overflows: An overflow means that the argument gets a :math:\pm 16, which does not change the result due to the periodicity of the cosine function.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
the angle in radians
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
div
staticmethod
¶
Computes the division between two same-type variables \(x/y\). Takes integer and fixed variables as long as both are of the same type. When both inputs are integer, the actual result type (int or fixed) is determined by how the result is used - e.g. the type of the variable it is assigned to. When assigned to an int variable, the result is \(floor(x/y)\).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA int or fixed
TYPE:
|
y
|
a QUA int or fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA int or fixed |
dot
staticmethod
¶
dot(
x: VectorOfAnyType, y: VectorOfAnyType
) -> Union[
QuaLibFunctionOutput[float], QuaLibFunctionOutput[int]
]
Calculates a dot product of two QUA arrays of identical size.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
y
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[QuaLibFunctionOutput[float], QuaLibFunctionOutput[int]]
|
The dot product of x and y. |
Union[QuaLibFunctionOutput[float], QuaLibFunctionOutput[int]]
|
If either x or y is an array of type |
elu
staticmethod
¶
Computes the Exponential Linear Unit activation function of x \(\mathrm{ELU(x)} = \mathrm{max}(0, x) + \mathrm{min}(0, \mathrm{exp}(x)-1)\).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
exp
staticmethod
¶
Computes \(e^{x}\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed smaller than ln(8)=2.0794415416
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
inv
staticmethod
¶
Computes the inverse of x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed which is x<=-1/8 or 1/8<x
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
inv_sqrt
staticmethod
¶
Computes the inverse square root of x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed larger than 1/64
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
ln
staticmethod
¶
Computes \(\mathrm{ln}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed larger than exp(-8)=0.0003354627
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
log
staticmethod
¶
Computes \(\mathrm{log}_{base}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed larger than pow2(-8)=0.00390625
TYPE:
|
base
|
a QUA fixed larger than pow2(1/8)=1.09051
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
log10
staticmethod
¶
Computes \(\mathrm{log}_{10}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed larger than pow10(-8)=0.00000001
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
log2
staticmethod
¶
Computes \(\mathrm{log}_{2}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed larger than pow2(-8)=0.00390625
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
lrelu
staticmethod
¶
Computes the Leaky Rectified Linear Unit activation function of x \(\mathrm{LReLU}(x)=\mathrm{max}(0, x)+0.01*\mathrm{min}(0, x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
max
staticmethod
¶
Computes the max of an array x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[NumberT]
|
the max value of the array, has same type as x |
min
staticmethod
¶
Computes the min of an array x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[NumberT]
|
the min value of the array, has same type as x |
plrelu
staticmethod
¶
Computes the Parametric Leaky Rectified Linear Unit activation function of x \(\mathrm{PLReLU}(x, a) = \mathrm{max}(0, x)+a*\mathrm{min}(0, x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
a
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
pow
staticmethod
¶
Computes \({base}^{x}\). Does not support base=1, nor the case where both base=0 & x=0.
| PARAMETER | DESCRIPTION |
|---|---|
base
|
a non-negative QUA fixed
TYPE:
|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
pow2
staticmethod
¶
Computes \(2^{x}\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed smaller than 3 (to avoid overflow)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
relu
staticmethod
¶
Computes the Rectified Linear Unit activation function of x \(\mathrm{ReLU}(x) = \mathrm{max}(0, x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
selu
staticmethod
¶
Computes the Scaled Exponential Linear Unit activation function of x \(\mathrm{SELU}(x) = s*(\mathrm{max}(0, x)+a*\mathrm{min}(0, \mathrm{exp}(x)-1))\), \(a=1.67326324\), \(s=1.05070098\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
sin
staticmethod
¶
Computes \(\mathrm{sin}(x)\)
| PARAMETER | DESCRIPTION |
|---|---|
x
|
the angle in radians
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
sin2pi
staticmethod
¶
Computes \(\mathrm{sin}(2 \pi x)\).
This is more efficient than Math.sin(2*np.pi*x).
In addition, this function is immune to overflows: An overflow means that the argument gets a \(\pm 16\), which does not change the result due to the periodicity of the sine function.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
the angle in radians
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
sqrt
staticmethod
¶
Computes the square root of x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a non-negative QUA fixed
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[float]
|
a QUA fixed |
sum
staticmethod
¶
Computes the sum of an array x
| PARAMETER | DESCRIPTION |
|---|---|
x
|
a QUA array
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QuaLibFunctionOutput[NumberT]
|
the sum of the array, has same type as x |