Formulas and syntax
KeOps lets us define any reduction operation of the form
or
where
We now describe the symbolic syntax that can be used through all KeOps bindings.
Variables: category, index and dimension
At a low level, every variable
In practice, the category
keyword |
meaning |
---|---|
|
variable indexed by |
|
variable indexed by |
|
parameter |
followed by a Vi(2,4)
specifies a variable indexed by
N.B.: Using the same index k
for two variables with different dimensions or categories is not allowed and will be rejected by the compiler.
Reserved words
keyword |
meaning |
---|---|
|
indexes sequences |
followed by a sequence Ind(2,4,2,5,12)
can be used as parameters for some operations.
Math operators
To define formulas with KeOps, you can use simple arithmetics:
|
scalar-vector multiplication (if |
|
addition of two vectors |
|
difference between two vectors or minus sign |
|
element-wise division (N.B. |
|
scalar product between vectors |
|
element-wise equal function ( |
|
element-wise equal function ( |
|
element-wise less-than function ( |
|
element-wise greater-than function ( |
|
element-wise less-than-or-equal-to function ( |
|
element-wise greater-than-or-equal-to function ( |
Elementary functions:
|
element-wise opposite of |
|
element-wise inverse |
|
element-wise exponential function |
|
element-wise natural logarithm |
|
computes |
|
element-wise sine function |
|
function |
|
element-wise arc-sine function |
|
element-wise cosine function |
|
element-wise arc-cosine function |
|
element-wise arc-tangent function |
|
element-wise 2-argument arc-tangent function |
|
|
|
power operation, alias for |
|
element-wise square, faster than |
|
element-wise square root, faster than |
|
element-wise inverse square root, faster than |
|
element-wise absolute value |
|
element-wise sign function ( |
|
element-wise step function ( |
|
element-wise ReLU function ( |
|
element-wise Clamp function ( |
|
element-wise Clamp function, with a and b fixed integers |
|
element-wise IfElse function ( |
|
element-wise Modulo function, with offset (computes |
|
element-wise Round function, with d decimal rounding |
Simple vector operations:
|
squared L2 norm, same as |
|
L2 norm, same as |
|
normalize vector, same as |
|
squared L2 distance, same as |
Generic squared Euclidean norms, with support for scalar, diagonal and full (symmetric)
matrices. If f
is a vector of size N, depending on the size of
s
, WeightedSqNorm(s,f)
may refer to:
a weighted L2 norm
ifs
is a vector of size 1.a separable norm
ifs
is a vector of size N.a full anisotropic norm
ifs
is a vector of size N * N such thats[i*N+j]=s[j*N+i]
(i.e. stores a symmetric matrix).
|
generic squared euclidean norm |
|
generic squared distance, same as |
Operations involving complex numbers:
|
Real part of complex (vectorized) |
|
Imaginary part of complex (vectorized) |
|
convert real vector to complex vector with zero imaginary part (F+0*i) |
|
convert real vector to complex vector with zero real part (0+i*F) |
|
Complex conjugate (vectorized) |
|
Absolute value or modulus of complex (vectorized) |
|
Square of modulus of complex (vectorized) |
|
Angle of complex (vectorized) |
|
Sum of complex vector |
|
Adjoint operation of ComplexSum - replicates f (complex scalar) dim times |
|
Complex multiplication of f and g (vectorized) |
|
Multiplication of f (complex scalar) with g (complex vector) |
|
Multiplication of f (real scalar) with g (complex vector) |
|
Complex division of f and g (vectorized) |
Constants and padding/concatenation operations:
|
integer constant N |
|
alias for |
|
vector of zeros of size N |
|
sum of elements of vector |
|
max of elements of vector |
|
min of elements of vector |
|
argmax of elements of vector |
|
argmin of elements of vector |
|
extract M-th element of vector |
|
insert scalar value |
|
extract sub-vector from vector |
|
insert vector |
|
concatenation of vectors |
|
encodes a (rounded) scalar value as a one-hot vector of dimension D |
Elementary tensor algebra:
|
matrix-vector product |
|
vector-matrix product |
|
tensor cross product |
|
kronecker product (similar to numpy's kron in the spirit) |
|
tensordot product |
Symbolic gradients and linear operators:
|
gradient of |
|
differential of |
|
matrix of gradient (i.e. transpose of the jacobian matrix) |
|
divergence of |
|
laplacian of |
|
trace of |
|
adjoint of |
Other:
|
Soft-DTW distance (based on the sqaured euclidean distance) between |
Reductions
The operations that can be used to reduce an array are described in the following table.
code name |
arguments |
mathematical expression (reduction over j) |
remarks |
---|---|---|---|
|
|
||
|
|
|
|
|
|
only in Python bindings |
|
|
|
|
|
|
|
only in Python bindings |
|
|
|
only in Python bindings |
|
|
|
no gradient |
|
|
|
gradient xreturns zeros |
|
|
|
no gradient |
|
|
|
no gradient |
|
|
|
gradient returns zeros |
|
|
|
no gradient |
|
|
|
no gradient |
|
|
|
gradient returns zeros |
|
|
|
no gradient |
N.B.: All these reductions, except Max_SumShiftExp
and LogSumExp
, are vectorized : whenever the input f
or g
is vector-valued, the output will be vector-valued, with the corresponding reduction applied element-wise to each component.
N.B.: All reductions accept an additional optional argument that specifies wether the reduction is performed over the j or the i index. (see C++ API for KeOps and Generic reductions)
An example
Assume we want to compute the sum
where:
is a parameter, is an x-variable indexed by , is an y-variable indexed by , is an y-variable indexed by .
Using the variable placeholders presented above and the
mathematical operations listed in Math operators,
we can define F
as a symbolic string
F = "Sum_Reduction( Square( Pm(0,1) - Vj(3,1) ) * Exp( Vi(1,3) + Vj(2,3) ), 1 )"
in which +
and -
denote the usual addition of vectors, Exp
is the (element-wise) exponential function and *
denotes scalar-vector multiplication.
The second argument 1
of the Sum_Reduction
operator
indicates that the summation is performed with respect to the 0
would have been associated with an
Note that in all bindings, variables can be defined through aliases.
In this example, we may write p=Pm(0,1)
, x=Vi(1,3)
, y=Vj(2,3)
, a=Vj(3,1)
and thus give F
through a much friendlier expression:
F = "Sum_Reduction( Square(p - a) * Exp(x + y), 1 )"