Skip to content

Commit

Permalink
fixing...
Browse files Browse the repository at this point in the history
  • Loading branch information
jzstark committed Mar 25, 2024
1 parent 7270606 commit 5ce6d27
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 111 deletions.
4 changes: 2 additions & 2 deletions chapters/algodiff.md
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ let f y =

For this functions $f: \mathbf{R}^4 \rightarrow \mathbf{R}^4$, we can then find its Jacobian matrix. Suppose the given point of interest of where all four input variables equals one. Then we can use the `Algodiff.D.jacobian` function in this way.

```text
```ocaml
let y = Mat.ones 1 4
let result = jacobian f y
Expand All @@ -1208,7 +1208,7 @@ R3 0.53033 0.176777 0 0

Next, we find the eigenvalues of this jacobian matrix with the Linear Algebra module in Owl that we have introduced in previous chapter.

```text
```ocaml
let eig = Owl_linalg.D.eigvals j
val eig : Owl_dense_matrix_z.mat =
Expand Down
83 changes: 30 additions & 53 deletions chapters/convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For unary operators such as `Arr.sin x`, the situation is rather straightforward

For `Arr.add_ x y`, the question is where to store the final result when both inputs are ndarray. Let's look at the type of `Arr.add_` function.

```text
```ocaml
val Arr.add_ : ?out:Arr.arr -> Arr.arr -> Arr.arr -> unit
```

Expand Down Expand Up @@ -275,55 +275,32 @@ As you can see, the operators above do not allow interoperation on different num

Some people just like Pythonic way of working, `Owl.Ext` module is specifically designed for this purpose, to make prototyping faster and easier. Once you open the module, `Ext` immediately provides a set of operators to allow you to interoperate on different number types, as below. It automatically casts types for you if necessary.

------------- ------------- --------------------------
Operator Example Operation
------------- ------------- --------------------------
`+` `x + y` add

`-` `x - y` sub

`*` `x * y` mul

`/` `x / y` div

`=` `x = y` comparison, return bool

`!=` `x != y` comparison, return bool

`<>` `x <> y` same as `!=`

`>` `x > y` comparison, return bool

`<` `x < y` comparison, return bool

`>=` `x >= y` comparison, return bool

`<=` `x <= y` comparison, return bool

`=.` `x =. y` element_wise comparison

`!=.` `x !=. y` element_wise comparison
Operator | Example | Operation
------------- | ------------- | --------------------------
`+` | `x + y` | add
`-` | `x - y` | sub
`*` | `x * y` | mul
`/` | `x / y` | div
`=` | `x = y` | comparison, return bool
`!=` | `x != y` | comparison, return bool
`<>` | `x <> y` | same as `!=`
`>` | `x > y` | comparison, return bool
`<` | `x < y` | comparison, return bool
`>=` | `x >= y` | comparison, return bool
`<=` | `x <= y` | comparison, return bool
`=.` | `x =. y` | element_wise comparison
`!=.` | `x !=. y` | element_wise comparison
`<>.` | `x <>. y` | same as `!=.`
`>.` | `x >. y` | element_wise comparison
`<.` | `x <. y` | element_wise comparison
`>=.` | `x >=. y` | element_wise comparison
`<=.` | `x <=. y` | element_wise comparison
`%` | `x % y` | element_wise mod divide
`**` | `x ** y` | power function
`*@` | `x *@ y` | matrix multiply
`min2` | `min2 x y` | element-wise min
`max2` | `max2 x y` | element-wise max

`<>.` `x <>. y` same as `!=.`

`>.` `x >. y` element_wise comparison

`<.` `x <. y` element_wise comparison

`>=.` `x >=. y` element_wise comparison

`<=.` `x <=. y` element_wise comparison

`%` `x % y` element_wise mod divide

`**` `x ** y` power function

`*@` `x *@ y` matrix multiply

`min2` `min2 x y` element-wise min

`max2` `max2 x y` element-wise max
------------- ------------- --------------------------
: Operator extensions {#tbl:convention:ext}

You may have noticed, the operators ended with `$` (e.g., `+$`, `-$` ...) disappeared from the table, which is simply because we can add/sub/mul/div a scalar with a matrix directly and we do not need these operators any more. Similar for comparison operators, because we can use the same `>` operator to compare a matrix to another matrix, or compare a matrix to a scalar, we do not need `>$` any longer. Allowing interoperation makes the operator table much shorter.
Expand All @@ -346,7 +323,7 @@ Note that `Ext` contains its own `Ext.Dense` module which further contains the f

These modules are simply the wrappers of the original modules in `Owl.Dense` module so they provide most of the APIs already implemented. The extra thing these wrapper modules does is to pack and unpack the raw number types for you automatically. However, you can certainly use the raw data types then use the constructors defined in `Owl_ext_types` to wrap them up by yourself. The constructors are defined as below.

```text
```ocaml
type ext_typ =
F of float
Expand Down Expand Up @@ -411,7 +388,7 @@ Before we finish this chapter, I want to point out the caveat. `Ext` tries to mi

In Owl, `Dense` module contains the modules of dense data structures. For example, `Dense.Matrix` supports the operations of dense matrices. Similarly, `Sparse` module contains the modules of sparse data structures.

```text
```ocaml
Dense.Ndarray;; (* dense ndarray *)
Dense.Matrix;; (* dense matrix *)
Expand Down Expand Up @@ -536,7 +513,7 @@ Mat.zeros 5 5;; (* same as Dense.Matrix.D.zeros 5 5 *)

More examples besides creation functions are as follows.

```text
```ocaml
Mat.load "data.mat";; (* same as Dense.Matrix.D.load "data.mat" *)
Mat.of_array 5 5 x;; (* same as Dense.Matrix.D.of_array 5 5 x *)
Mat.linspace 0. 9. 10;; (* same as Dense.Matrix.D.linspace 0. 9. 10 *)
Expand All @@ -562,7 +539,7 @@ As I mentioned before, there are four basic number types. You can therefore cast

In fact, all these function rely on the following `cast` function.

```text
```ocaml
val cast : ('a, 'b) kind -> ('c, 'd) t -> ('a, 'b) t
Expand Down
10 changes: 5 additions & 5 deletions chapters/dataframe.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ However, if you just want to append one or two rows, the previous method seems a
There are also functions allow you to retrieve the properties, for example:


```text
```ocaml
val copy : t -> t (* return the copy of a dataframe. *)
Expand Down Expand Up @@ -204,7 +204,7 @@ R1 Carol 30

How can we miss the classic iteration functions in the functional programming? Dataframe includes the following methods to traverse the rows in a dataframe. We did not include any method to traverse columns because they can be simply extracted out as series then processed separately.

```text
```ocaml
val iteri_row : (int -> elt array -> unit) -> t -> unit
Expand All @@ -229,7 +229,7 @@ Applying these functions to a dataframe is rather straightforward. All the eleme
One interesting thing worth mentioning here is that there are several functions are associated with extended indexing operators. This allows us to write quite concise code in our application.


```text
```ocaml
val ( .%( ) ) : t -> int * string -> elt
(* associated with `get_by_name` *)
Expand Down Expand Up @@ -352,7 +352,7 @@ R2 Carol 3000.
CSV (Comma-Separated Values) is a common format to store tabular data. The module provides simple support to process CSV files. The two core functions are as follows.


```text
```ocaml
val of_csv : ?sep:char -> ?head:string array -> ?types:string array -> string -> t
Expand Down Expand Up @@ -383,7 +383,7 @@ Owl_pretty.pp_dataframe Format.std_formatter df

The result should look like this. We have truncated out some rows to save space here.

```text
```ocaml
funding data in csv file
+-----------------+-----------------+-------+---------+-------------+-----+----------+----------+--------------+------------
Expand Down
2 changes: 1 addition & 1 deletion chapters/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ The Second example demonstrates how to plot figures in notebook. Because Owl's P

To load the image into browser, we need to call the `Jupyter_notebook.display_file` function. Then we can see the plot [@fig:introduction:example00] is correctly rendered in the notebook running in your browser. Plotting capability greatly enriches the content of an interactive presentation.

```text
```ocaml
Jupyter_notebook.display_file ~base64:true "image/png" "plot_00.png"
```

Expand Down
2 changes: 1 addition & 1 deletion chapters/linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ let x = Mat.sequential 4 6;;

You should be able to see the following output in your `utop`.

```text
```ocaml
C0 C1 C2 C3 C4 C5
R0 1 2 3 4 5 6
Expand Down
36 changes: 18 additions & 18 deletions chapters/ndarray.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ The `map` function can be very useful in implementing vectorised math functions.

If you need indices in the transformation function, you can use the `mapi` function which accepts the 1-d index of the element being accessed.

```text
```ocaml
val mapi : (int -> 'a -> 'a) -> ('a, 'b) t -> ('a, 'b) t
Expand All @@ -212,7 +212,7 @@ If you need indices in the transformation function, you can use the `mapi` funct

The `fold` function is often referred to as "reduction" in other programming languages. It has a named parameter called `axis`, with which you can specify along what axis you want to fold a given ndarray.

```text
```ocaml
val fold : ?axis:int -> ('a -> 'a -> 'a) -> 'a -> ('a, 'b) t -> ('a, 'b) t
Expand All @@ -231,7 +231,7 @@ The functions `sum`, `sum'`, `prod`, `prod'`, `min`, `min'`, `mean`, and `mean'`

Similarly, if you need indices in folding function, you can use `foldi` which passes in 1-d indices.

```text
```ocaml
val foldi : ?axis:int -> (int -> 'a -> 'a -> 'a) -> 'a -> ('a, 'b) t -> ('a, 'b) t
Expand All @@ -243,7 +243,7 @@ Similarly, if you need indices in folding function, you can use `foldi` which pa
To some extent, the `scan` function is like the combination of `map` and `fold`. It accumulates the value along the specified axis but it does not change the shape of the input. Think about how we generate a cumulative distribution function (CDF) from a probability density/mass function (PDF/PMF).
The type signature of `scan` looks like this in Ndarray.

```text
```ocaml
val scan : ?axis:int -> ('a -> 'a -> 'a) -> ('a, 'b) t -> ('a, 'b) t
Expand All @@ -263,7 +263,7 @@ Again, you can use the `scani` to obtain the indices in the passed in cumulative

The comparison functions themselves can be divided into several groups. The first group compares two ndarrays then returns a boolean value.

```text
```ocaml
val equal : ('a, 'b) t -> ('a, 'b) t -> bool
Expand All @@ -278,7 +278,7 @@ The comparison functions themselves can be divided into several groups. The firs

The second group compares two ndarrays but returns an 0-1 ndarray of the same shape. The elements where the predicate is satisfied have value 1 otherwise 0.

```text
```ocaml
val elt_equal : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t
Expand All @@ -294,7 +294,7 @@ The second group compares two ndarrays but returns an 0-1 ndarray of the same sh

The third group is similar to the first one but compares an ndarray with a scalar value, the return is a Boolean value.

```text
```ocaml
val equal_scalar : ('a, 'b) t -> 'a -> bool
Expand All @@ -310,7 +310,7 @@ The third group is similar to the first one but compares an ndarray with a scala

The fourth group is similar to the second one but compares an ndarray with a scalar value, and the returned value is a 0-1 ndarray.

```text
```ocaml
val elt_equal_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t
Expand Down Expand Up @@ -358,7 +358,7 @@ Conceptually, Owl can implement all these functions using the aforementioned `ma

Like native OCaml array, Owl also provides `iter` and `iteri` functions with which you can iterate over all the elements in an ndarray.

```text
```ocaml
val iteri :(int -> 'a -> unit) -> ('a, 'b) t -> unit
Expand All @@ -369,7 +369,7 @@ Like native OCaml array, Owl also provides `iter` and `iteri` functions with whi
One common use case is iterating all the elements and checking if one (or several) predicate is satisfied.
There is a special set of iteration functions to help you finish this task.

```text
```ocaml
val is_zero : ('a, 'b) t -> bool
Expand All @@ -387,7 +387,7 @@ There is a special set of iteration functions to help you finish this task.

The predicates can be very complicated sometimes. In that case you can use the following three functions to pass in arbitrarily complicated functions to check them.

```text
```ocaml
val exists : ('a -> bool) -> ('a, 'b) t -> bool
Expand All @@ -399,7 +399,7 @@ The predicates can be very complicated sometimes. In that case you can use the f

All aforementioned functions only tell us whether the predicates are met or not. They cannot tell which elements satisfy the predicate. The following `filter` function can return the 1-d indices of those elements satisfying the predicates.

```text
```ocaml
val filteri : (int -> 'a -> bool) -> ('a, 'b) t -> int array
Expand All @@ -409,7 +409,7 @@ All aforementioned functions only tell us whether the predicates are met or not.

We have mentioned that 1-d indices are passed in. The reason is passing in 1-d indices is way faster than passing in n-d indices. However, if you do need n-dimensional indices, you can use the following two functions to convert between 1-d and 2-d indices, both are defined in the `Owl.Utils` module.

```text
```ocaml
val ind : ('a, 'b) t -> int -> int array
(* 1-d to n-d index conversion *)
Expand Down Expand Up @@ -473,7 +473,7 @@ R5 8 9 10 11

You can also expand the dimensionality of an ndarray, or squeeze out those dimensions having only one element, or even padding elements to an existing ndarray.

```text
```ocaml
val expand : ('a, 'b) t -> int -> ('a, 'b) t
Expand All @@ -488,7 +488,7 @@ The `concatenate` allows us to concatenate an array of ndarrays along the specif
For matrices, there are two operators associated with concatenation: `@||` for concatenating horizontally (i.e. along axis 1); `@=` for concatenating vertically (i.e. along axis 0).
The `split` is simply the inverse operation of concatenation.

```text
```ocaml
val concatenate : ?axis:int -> ('a, 'b) t array -> ('a, 'b) t
Expand All @@ -498,15 +498,15 @@ The `split` is simply the inverse operation of concatenation.

You can also sort an ndarray but note that modification will happen in place.

```text
```ocaml
val sort : ('a, 'b) t -> unit
```

Converting between ndarrays and OCaml native arrays can be efficiently done with these conversion functions:

```text
```ocaml
val of_array : ('a, 'b) kind -> 'a array -> int array -> ('a, 'b) t
Expand All @@ -521,7 +521,7 @@ Again, there also exist the `to_arrays` and `of_arrays` two functions for the sp

Serialisation and de-serialisation are simply done with the `save` and `load` functions.

```text
```ocaml
val save : out:string -> ('a, 'b) t -> unit
Expand Down
Loading

0 comments on commit 5ce6d27

Please sign in to comment.