Skip to content

Tutorial: Operators and Ext Module

Liang Wang edited this page May 1, 2017 · 19 revisions

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.

Basic 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 of Algodiff module. 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 variables x and y. The difference is that = returns a boolean value whereas =. returns a matrix or ndarray of the same shape and same type as x and y. In the returned result, the value in a given position is 1 if the values of the corresponding position in x and y satisfy the predicate, otherwise it is 0.

Operators are easy to use, here are some examples.