Skip to content

Math functions

qm.qua.lib.Math

MSB staticmethod

MSB(x: Scalar[NumberT]) -> QuaLibFunctionOutput[int]

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: Scalar[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[int]

a QUA int

abs staticmethod

abs(x: ScalarOfAnyType) -> QuaLibFunctionOutput[float]

Computes the absolute value of x

PARAMETER DESCRIPTION
x

a QUA variable

TYPE: ScalarOfAnyType

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

aelu staticmethod

aelu(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

argmax staticmethod

argmax(x: Vector[NumberT]) -> QuaLibFunctionOutput[int]

Return the index of the maximum of an array

PARAMETER DESCRIPTION
x

a QUA array

TYPE: Vector[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[int]

the index of maximum value of array, a QUA Integer

argmin staticmethod

argmin(x: Vector[NumberT]) -> QuaLibFunctionOutput[int]

Return the index of the minimum of an array

PARAMETER DESCRIPTION
x

a QUA array

TYPE: Vector[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[int]

the index of minimum value of array, a QUA Integer

atan staticmethod

atan(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{atan}(x)\)

-- Available from QOP 2.4 --

PARAMETER DESCRIPTION
x

the tangent ratio (opposite/adjacent)

TYPE: a QUA fixed which is -1 <= x <= 1

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

atan2 staticmethod

atan2(
    y: Scalar[float], x: Scalar[float]
) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{atan2}(y,x)\)

-- Available from QOP 2.4 --

PARAMETER DESCRIPTION
y

the coordinate y (opposite)

TYPE: a QUA fixed

x

the coordinate x (adjacent)

TYPE: a QUA fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

atan2_2pi staticmethod

atan2_2pi(
    y: Scalar[float], x: Scalar[float]
) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{1/2 \pi * atan2}(y,x)\)

-- Available from QOP 2.4 --

PARAMETER DESCRIPTION
y

The coordinate y (opposite)

TYPE: a QUA fixed

x

The coordinate x (adjacent)

TYPE: a QUA fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

atan_2pi staticmethod

atan_2pi(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{1/2 \pi * atan}(x)\)

-- Available from QOP 2.4 --

PARAMETER DESCRIPTION
x

The tangent ratio (opposite/adjacent)

TYPE: a QUA fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

cos staticmethod

cos(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{cos}(x)\)

PARAMETER DESCRIPTION
x

the angle in radians

TYPE: QUA variable of type fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

cos2pi staticmethod

cos2pi(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: QUA variable of type fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

div staticmethod

div(x: Scalar[int], y: Scalar[int]) -> Any
div(
    x: Scalar[float], y: Scalar[float]
) -> QuaLibFunctionOutput[float]
div(
    x: Scalar[NumberT], y: Scalar[NumberT]
) -> QuaLibFunctionOutput[float]

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: Scalar[NumberT]

y

a QUA int or fixed

TYPE: Scalar[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA int or fixed

dot staticmethod

dot(
    x: Union[Vector[int], Vector[bool]],
    y: Union[Vector[int], Vector[bool]],
) -> QuaLibFunctionOutput[int]
dot(
    x: Vector[float], y: VectorOfAnyType
) -> QuaLibFunctionOutput[float]
dot(
    x: VectorOfAnyType, y: Vector[float]
) -> QuaLibFunctionOutput[float]
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: VectorOfAnyType

y

a QUA array

TYPE: VectorOfAnyType

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 Fixed, then the result is Fixed. Otherwise, it is an Int

Example
assign(c, dot(a, b))

elu staticmethod

elu(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

exp staticmethod

exp(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(e^{x}\)

PARAMETER DESCRIPTION
x

a QUA fixed smaller than ln(8)=2.0794415416

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

inv staticmethod

inv(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes the inverse of x

PARAMETER DESCRIPTION
x

a QUA fixed which is x<=-1/8 or 1/8<x

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

inv_sqrt staticmethod

inv_sqrt(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes the inverse square root of x

PARAMETER DESCRIPTION
x

a QUA fixed larger than 1/64

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

ln staticmethod

ln(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{ln}(x)\)

PARAMETER DESCRIPTION
x

a QUA fixed larger than exp(-8)=0.0003354627

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

log staticmethod

log(
    x: Scalar[float], base: Scalar[float]
) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{log}_{base}(x)\)

PARAMETER DESCRIPTION
x

a QUA fixed larger than pow2(-8)=0.00390625

TYPE: Scalar[float]

base

a QUA fixed larger than pow2(1/8)=1.09051

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

log10 staticmethod

log10(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{log}_{10}(x)\)

PARAMETER DESCRIPTION
x

a QUA fixed larger than pow10(-8)=0.00000001

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

log2 staticmethod

log2(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{log}_{2}(x)\)

PARAMETER DESCRIPTION
x

a QUA fixed larger than pow2(-8)=0.00390625

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

lrelu staticmethod

lrelu(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

max staticmethod

max(x: Vector[NumberT]) -> QuaLibFunctionOutput[NumberT]

Computes the max of an array x

PARAMETER DESCRIPTION
x

a QUA array

TYPE: Vector[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[NumberT]

the max value of the array, has same type as x

min staticmethod

min(x: Vector[NumberT]) -> QuaLibFunctionOutput[NumberT]

Computes the min of an array x

PARAMETER DESCRIPTION
x

a QUA array

TYPE: Vector[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[NumberT]

the min value of the array, has same type as x

plrelu staticmethod

plrelu(
    x: Scalar[float], a: Scalar[float]
) -> QuaLibFunctionOutput[float]

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: Scalar[float]

a

a QUA fixed

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

pow staticmethod

pow(
    base: Scalar[float], x: Scalar[float]
) -> QuaLibFunctionOutput[float]

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: Scalar[float]

x

a QUA fixed

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

pow2 staticmethod

pow2(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(2^{x}\)

PARAMETER DESCRIPTION
x

a QUA fixed smaller than 3 (to avoid overflow)

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

relu staticmethod

relu(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes the Rectified Linear Unit activation function of x \(\mathrm{ReLU}(x) = \mathrm{max}(0, x)\)

PARAMETER DESCRIPTION
x

a QUA fixed

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

selu staticmethod

selu(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

sin staticmethod

sin(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes \(\mathrm{sin}(x)\)

PARAMETER DESCRIPTION
x

the angle in radians

TYPE: QUA variable of type fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

sin2pi staticmethod

sin2pi(x: Scalar[float]) -> QuaLibFunctionOutput[float]

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: QUA variable of type fixed

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

sqrt staticmethod

sqrt(x: Scalar[float]) -> QuaLibFunctionOutput[float]

Computes the square root of x

PARAMETER DESCRIPTION
x

a non-negative QUA fixed

TYPE: Scalar[float]

RETURNS DESCRIPTION
QuaLibFunctionOutput[float]

a QUA fixed

sum staticmethod

sum(x: Vector[NumberT]) -> QuaLibFunctionOutput[NumberT]

Computes the sum of an array x

PARAMETER DESCRIPTION
x

a QUA array

TYPE: Vector[NumberT]

RETURNS DESCRIPTION
QuaLibFunctionOutput[NumberT]

the sum of the array, has same type as x