Repository navigation
Tutorial: Operators and Ext Module
This tutorial will go through the operators and Ext module. The operators in Owl are implemented using functors. However, you do not need to work with functors directly in order to use the operators.
The operators have been included in each Ndarray and Matrix module. The following table summarises the operators currently implemented in Owl.
| Operator | Example | Operation | Dense/Sparse | Ndarray/Matrix |
|---|---|---|---|---|
+ |
x + y |
element-wise add | both | both |
- |
x - y |
element-wise sub | both | both |
* |
x * y |
element-wise mul | both | both |
/ |
x / y |
element-wise div | both | both |
+$ |
x +$ a |
add scalar | both | both |
-$ |
x -$ a |
sub scalar | both | both |
*$ |
x *$ a |
mul scalar | both | both |
/$ |
x /$ a |
div scalar | both | both |
$+ |
a $+ x |
scalar add | both | both |
$- |
a $- x |
scalar sub | both | both |
$* |
a $* x |
scalar mul | both | both |
$/ |
a $/ x |
scalar div | both | both |
= |
x = y |
comparison | both | both |
!= |
x != y |
comparison | both | both |
<> |
x <> y |
same as !=
|
both | both |
> |
x > y |
comparison | both | both |
< |
x < y |
comparison | both | both |
>= |
x >= y |
comparison | both | both |
<= |
x <= y |
comparison | both | both |
=. |
x =. y |
comparison | Dense | both |
!=. |
x !=. y |
comparison | Dense | both |
<>. |
x <>. y |
same as !=.
|
Dense | both |
>. |
x >. y |
comparison | Dense | both |
<. |
x <. y |
comparison | Dense | both |
>=. |
x >=. y |
comparison | Dense | both |
<=. |
x <=. y |
comparison | Dense | both |
** |
x ** y |
power function | Dense | both |
*@ |
x *@ y |
matrix multiply | both | Matrix |
There are a list of things worth your attention as below.
-
*is for element-wise multiplication;*@is for matrix multiplication. You can easily understand the reason if you read the source code ofAlgodiffmodule. Using*for element-wise multiplication (for matrices) leads to the consistent implementation of algorithmic differentiation. -
+$has its corresponding operator$+if we flip the order of parameters. However, be very careful about the operator precedence since OCaml determines the precedence based on the first character of an infix.+$preserves the precedence whereas$+does not. Therefore, I do not recommend using$+. If you do use it, please use parentheses to explicitly specify the precedence. The same argument also applies to-$,*$, and/$. -
For comparison operators, e.g. both
=and=.compare all the elements in two variablesxandy. The difference is that=returns a boolean value whereas=.returns a matrix or ndarray of the same shape and same type asxandy. In the returned result, the value in a given position is1if the values of the corresponding position inxandysatisfy the predicate, otherwise it is0.
Operators are easy to use, here are some examples.
let x = Mat.uniform 5 5;;
let y = Mat.uniform 5 5;;
Mat.(x + y);;
Mat.(x * y);;
Mat.(x ** y);;
Mat.(x *@ y);;
...
(* please compare the returns of the following two examples *)
Mat.(x > y);;
Mat.(x >. y);;