Mon, 06 Dec 2021 11:52:10 +0200
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
11 | 1 | |
13 | 2 | # AlgTools |
11 | 3 | |
4 | Author: Tuomo Valkonen <tuomov@iki.fi> | |
5 | ||
6 | This repository contains some general utility routines and tools for doing | |
7 | iterative algorithms in Julia: | |
8 | ||
9 | * Tools to facilitate working with `Channel`s | |
10 | * Linked lists and writing them to log files. | |
11 | * Structs: iteration, field replacement | |
12 | * Calculation of norms, dot products, projections | |
13 | * Conditional thread execution macros | |
12 | 14 | * Template for conveniently writing iterative algorithms using `do`-block anonymous functions as algorithm steps, with visualisation/verbosity implemented generally in iterator. |
20 | 15 | * Abstract linear operators with arbitrary domains and codomains; `LinOp{X,Y}`. |
11 | 16 | |
14 | 17 | The code is used by [ImageTools][] and both, for example, by <http://dx.doi.org/10.5281/zenodo.3659180>. |
11 | 18 | |
12 | 19 | ## Installation |
20 | ||
21 | To install this package, first clone the repository with [Mercurial](https://www.mercurial-scm.org/): | |
22 | ||
23 | ```console | |
24 | $ hg clone https://tuomov.iki.fi/repos/AlgTools/ | |
25 | ``` | |
26 | ||
27 | (Canonical public repository URL indicated here.) | |
28 | Then add the repository to Julia with `Pkg.develop`: | |
29 | ||
30 | ```console | |
31 | julia>] develop LOCATION_OF/AlgTools/ | |
32 | ``` | |
33 | ||
34 | Here replace `LOCATION_OF/AlgTools` with path where you cloned AlgTools. (If you did not change directory after cloning, simply use `AlgTools`.) | |
35 | Afterwards, you may use the package with: | |
36 | ||
37 | ```console | |
38 | julia> using AlgTools | |
39 | ``` | |
40 | ||
41 | This package is not and is not planned to be available via `Pkg.add` as it is based on the worst and must unusable version control system ever invented: Git. | |
42 | ||
17 | 43 | ## Iterative algorithms in a functional fashion |
44 | ||
45 | The package includes `simple_iterate` which helps separating the computational step of iterative algorithms from visualisation and other reporting routines. | |
46 | It is merely intended to serve as a template, as different applications require different visualisation routines to be implemented in a replacement of `simple_iterate`; in particular [ImageTools][] implements its own version. | |
11 | 47 | |
17 | 48 | The approach is heavily indebted to functional programming. |
49 | The computational step is to be implemented as a `do`-block anonymous function. | |
50 | That function gets passed another function `verbose` as a parameter. | |
51 | To (potentially) report the current status, the computational step only needs to call `verbose` with yet another function as a parameter. Whether the status is actually reported—and whether it needs to be calculated—is decided by `verbose`, and its parameter called as needed. | |
52 | ||
53 | ### Simple example | |
11 | 54 | |
55 | ```julia | |
56 | using AlgTools.Iterate | |
57 | ||
58 | params = (verbose_iter = 10, maxiter = 100,) | |
59 | ||
60 | begin | |
61 | local x = 1 | |
62 | simple_iterate(params) do verbose | |
63 | # This is our computational step, as an anonymous | |
64 | # `do`-block function. It has one parameter: `verbose`, | |
65 | # itself a function. | |
66 | x = x + √x | |
67 | verbose() do | |
68 | # This is another anonymous function that will | |
69 | # only get called when decided by `verbose`. | |
70 | # If we do get called, return current value | |
71 | return x, nothing | |
72 | end | |
73 | end | |
74 | end | |
75 | ``` | |
76 | This will output | |
77 | ``` | |
78 | 10/100 J=31.051164 | |
79 | 20/100 J=108.493699 | |
80 | 30/100 J=234.690039 | |
81 | 40/100 J=410.056327 | |
82 | 50/100 J=634.799262 | |
83 | 60/100 J=909.042928 | |
84 | 70/100 J=1232.870172 | |
85 | 80/100 J=1606.340254 | |
86 | 90/100 J=2029.497673 | |
87 | 100/100 J=2502.377071 | |
88 | ``` | |
89 | ||
14 | 90 | [ImageTools]: https://tuomov.iki.fi/software/ImageTools/ |