1<p align="center">
2  <img src="https://static.hoa-project.net/Image/Hoa.svg" alt="Hoa" width="250px" />
3</p>
4
5---
6
7<p align="center">
8  <a href="https://travis-ci.org/hoaproject/math"><img src="https://img.shields.io/travis/hoaproject/math/master.svg" alt="Build status" /></a>
9  <a href="https://coveralls.io/github/hoaproject/math?branch=master"><img src="https://img.shields.io/coveralls/hoaproject/math/master.svg" alt="Code coverage" /></a>
10  <a href="https://packagist.org/packages/hoa/math"><img src="https://img.shields.io/packagist/dt/hoa/math.svg" alt="Packagist" /></a>
11  <a href="https://hoa-project.net/LICENSE"><img src="https://img.shields.io/packagist/l/hoa/math.svg" alt="License" /></a>
12</p>
13<p align="center">
14  Hoa is a <strong>modular</strong>, <strong>extensible</strong> and
15  <strong>structured</strong> set of PHP libraries.<br />
16  Moreover, Hoa aims at being a bridge between industrial and research worlds.
17</p>
18
19# Hoa\Math
20
21[![Help on IRC](https://img.shields.io/badge/help-%23hoaproject-ff0066.svg)](https://webchat.freenode.net/?channels=#hoaproject)
22[![Help on Gitter](https://img.shields.io/badge/help-gitter-ff0066.svg)](https://gitter.im/hoaproject/central)
23[![Documentation](https://img.shields.io/badge/documentation-hack_book-ff0066.svg)](https://central.hoa-project.net/Documentation/Library/Math)
24[![Board](https://img.shields.io/badge/organisation-board-ff0066.svg)](https://waffle.io/hoaproject/math)
25
26This library provides tools around mathematical operations.
27
28[Learn more](https://central.hoa-project.net/Documentation/Library/Math).
29
30## Installation
31
32With [Composer](https://getcomposer.org/), to include this library into
33your dependencies, you need to
34require [`hoa/math`](https://packagist.org/packages/hoa/math):
35
36```sh
37$ composer require hoa/math '~1.0'
38```
39
40For more installation procedures, please read [the Source
41page](https://hoa-project.net/Source.html).
42
43## Testing
44
45Before running the test suites, the development dependencies must be installed:
46
47```sh
48$ composer install
49```
50
51Then, to run all the test suites:
52
53```sh
54$ vendor/bin/hoa test:run
55```
56
57For more information, please read the [contributor
58guide](https://hoa-project.net/Literature/Contributor/Guide.html).
59
60## Quick usage
61
62We propose a quick overview of one feature: evaluation of arithmetical
63expressions.
64
65### Evaluation of arithmetical expressions
66
67The `hoa://Library/Math/Arithmetic.pp` describes the form of an arithmetical
68expression. Therefore, we will use the classical workflow when manipulating a
69grammar, that involves the [`Hoa\Compiler`
70library](https://central.hoa-project.net/Resource/Library/Compiler) and the
71`Hoa\Math\Visitor\Arithmetic` class.
72
73```php
74// 1. Load the compiler.
75$compiler = Hoa\Compiler\Llk::load(
76    new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
77);
78
79// 2. Load the visitor, aka the “evaluator”.
80$visitor    = new Hoa\Math\Visitor\Arithmetic();
81
82// 3. Declare the expression.
83$expression = '1 / 2 / 3 + 4 * (5 * 2 - 6) * PI / avg(7, 8, 9)';
84
85// 4. Parse the expression.
86$ast        = $compiler->parse($expression);
87
88// 5. Evaluate.
89var_dump(
90    $visitor->visit($ast)
91);
92
93/**
94 * Will output:
95 *     float(6.4498519738463)
96 */
97
98// Bonus. Print the AST of the expression.
99$dump = new Hoa\Compiler\Visitor\Dump();
100echo $dump->visit($ast);
101
102/**
103 * Will output:
104 *     >  #addition
105 *     >  >  #division
106 *     >  >  >  token(number, 1)
107 *     >  >  >  #division
108 *     >  >  >  >  token(number, 2)
109 *     >  >  >  >  token(number, 3)
110 *     >  >  #multiplication
111 *     >  >  >  token(number, 4)
112 *     >  >  >  #multiplication
113 *     >  >  >  >  #group
114 *     >  >  >  >  >  #substraction
115 *     >  >  >  >  >  >  #multiplication
116 *     >  >  >  >  >  >  >  token(number, 5)
117 *     >  >  >  >  >  >  >  token(number, 2)
118 *     >  >  >  >  >  >  token(number, 6)
119 *     >  >  >  >  #division
120 *     >  >  >  >  >  token(constant, PI)
121 *     >  >  >  >  >  #function
122 *     >  >  >  >  >  >  token(id, avg)
123 *     >  >  >  >  >  >  token(number, 7)
124 *     >  >  >  >  >  >  token(number, 8)
125 *     >  >  >  >  >  >  token(number, 9)
126 */
127```
128
129We can add functions and constants on the visitor, thanks to the `addFunction`
130and `addConstant` methods. Thus, we will add the `rand` function (with 2
131parameters) and the `ANSWER` constant, set to 42:
132
133```php
134$visitor->addFunction('rand', function ($min, $max) {
135    return mt_rand($min, $max);
136});
137$visitor->addConstant('ANSWER', 42);
138
139$expression = 'rand(ANSWER / 2, ANSWER * 2)'
140var_dump(
141    $visitor->visit($compiler->parse($expression))
142);
143
144/**
145 * Could output:
146 *     int(53)
147 */
148```
149
150## Documentation
151
152The
153[hack book of `Hoa\Math`](https://central.hoa-project.net/Documentation/Library/Math) contains
154detailed information about how to use this library and how it works.
155
156To generate the documentation locally, execute the following commands:
157
158```sh
159$ composer require --dev hoa/devtools
160$ vendor/bin/hoa devtools:documentation --open
161```
162
163More documentation can be found on the project's website:
164[hoa-project.net](https://hoa-project.net/).
165
166## Getting help
167
168There are mainly two ways to get help:
169
170  * On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)
171    IRC channel,
172  * On the forum at [users.hoa-project.net](https://users.hoa-project.net).
173
174## Contribution
175
176Do you want to contribute? Thanks! A detailed [contributor
177guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains
178everything you need to know.
179
180## License
181
182Hoa is under the New BSD License (BSD-3-Clause). Please, see
183[`LICENSE`](https://hoa-project.net/LICENSE) for details.
184
185## Related projects
186
187The following projects are using this library:
188
189  * [PSIH & PMSIpilot](http://www.psih.fr/en/),
190    PSIH is the leading French integrator of business intelligence solutions for
191    the healthcare sector,
192  * [PHP Telegram Bot](https://github.com/akalongman/php-telegram-bot/),
193    Telegram bot based on the official Telegram Bot API.
194