-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.php
More file actions
343 lines (327 loc) Β· 7.67 KB
/
Math.php
File metadata and controls
343 lines (327 loc) Β· 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
namespace Vector\Lib;
use Vector\Core\Curry;
use Vector\Core\Module;
class Math
{
use Module;
/**
* Arithmetic Addition
*
* Add two numbers together.
*
* @param number $a First number to add
* @param number $b Second number to add
* @return number
*
* @example
* Math::add(-1, 2); // 1
*
* @example
* Math::add(2, 2); // 4
*
* @type Number a => a -> a -> a
*
*/
#[Curry]
protected static function add($a, $b)
{
return $a + $b;
}
/**
* Array Sum
*
* Add all the numbers of a list together and return their sum. If the given
* list is empty, returns 0.
*
* @param array $a List of numbers to add
* @return number Sum of all the elements of the list
*
* @example
* Math::sum([1, 2, 3]); // 6
*
* @example
* Math::sum([]); // 0
*
* @type Number a => [a] -> a
*
*/
#[Curry]
protected static function sum($a)
{
return array_reduce($a, function ($carry, $item) use ($a) {
$carry += $item;
return $carry;
}, 0);
}
/**
* Negate a number.
*
* Returns a given number * -1.
*
* @param number $a Number to make negative
* @return number The negated number
* @example
* Math::negate(4); // -4
*
* @example
* Math::negate(0); // 0
*
* @type Number a => a -> a
*
*/
#[Curry]
protected static function negate($a)
{
return -$a;
}
/**
* Arithmetic Subtraction.
*
* Subtracts two numbers, with the first argument being subtracted from the second.
*
* @param number $a Number to subtract
* @param number $b Number to subtract from
* @return number Subtraction of $b - $a
* @example
* Math::subtract(-1, 3); // 4
*
* @type Number a => a -> a -> a
*
* @example
* Math::subtract(4, 9); // 5
*
*/
#[Curry]
protected static function subtract($a, $b)
{
return $b - $a;
}
/**
* Arithmetic Multiplication
*
* Multiply two numbers together.
*
* @param number $a First number to multiply
* @param number $b Second number to multiply
* @return number Multiplication of $a * $b
* @example
* Math::multiply(0, 4); // 0
*
* @type Number a => a -> a -> a
*
* @example
* Math::multiply(2, 4); // 8
*
*/
#[Curry]
protected static function multiply($a, $b)
{
return $a * $b;
}
/**
* Array Product
*
* Returns the product of a list of numbers, i.e. the result of multiplying
* every element of a list together. Returns 1 for an empty list.
*
* @param array $a List of values to multiply
* @return mixed Product of every value in the list
* @example
* Math::product([2, 2, 3]); // 12
*
* @example
* Math::product([]); // 1
*
* @type Number a => [a] -> a
*
*/
#[Curry]
protected static function product($a)
{
return empty($a)
? 0
: array_reduce($a, function ($carry, $item) use ($a) {
$carry *= $item;
return $carry;
}, 1);
}
/**
* Arithmetic Division
*
* Divide two numbers, with the first argument being the divisor.
*
* @param number $a Denominator
* @param number $b Numerator
* @return float Result of $b divided by $a
* @example
* Math::divide(4, 12); // 3
*
* @type Number a => a -> a -> a
*
* @example
* Math::divide(2, 8); // 4
*
*/
#[Curry]
protected static function divide($a, $b)
{
return $b / $a;
}
/**
* Modulus Operator
*
* Take the modulus of two integers, with the first argument being the divisor.
* Returns the remainder of $b / $a.
*
* @param int $a Divisor
* @param int $b Numerator
* @return int Remainder of $b / $a
* @example
* Math::mod(2, 5); // 1
*
* @example
* Math::mod(5, 12); // 2
*
* @example
* Math::mod(3, 3); // 0
*
* @type Int -> Int -> Int
*
*/
#[Curry]
protected static function mod($a, $b)
{
return $b % $a;
}
/**
* Number Range
*
* Given two values m and n, return all values between m and n in an array, inclusive, with a
* step size of $step. The list of numbers will start at the first value and approach the second value.
*
* @param number $step The step sizes to take when building the range
* @param number $first First value in the list
* @param number $last Last value in the list
* @return array All the numbers between the first and last argument
* @example
* Math::range(1, 1, 5); // [1, 2, 3, 4, 5]
*
* @example
* Math::range(2, 0, -3); // [0, -2]
*
* @example
* Math::range(0, 0, 0); // [0]
*
* @example
* Math::range(0.1, 0, 0.5); // [0, 0.1, 0.2, 0.3, 0.4, 0.5]
*
* @type Number a => a -> a -> a
*
*/
#[Curry]
protected static function range($step, $first, $last)
{
return ($step + $first >= $last)
? [$first]
: range($first, $last, $step);
}
/**
* Minimum Value
*
* Returns the minimum of two arguments a and b.
* If a and be are equal, returns the first value. But since they're equal, that doesn't
* really matter now does it?
*
* @param number $a First number to compare
* @param number $b Second number to compare
* @return number The lesser of the two numbers
* @example
* Math::min(1, 2); // 1
*
* @example
* Math::min(-1, -6); // -6
*
* @example
* Math::min(5, 5); // 5
*
* @type Number a => a -> a -> a
*
*/
#[Curry]
protected static function min($a, $b)
{
return min([$a, $b]);
}
/**
* Maximum Value
*
* Returns the maximum of two arguments a and b. If a and b are equal, just returns the value.
*
* @param number $a First number to compare
* @param number $b Second number to compare
* @return number The greater of the two numbers
* @example
* Math::max(1, 2); // 2
*
* @example
* Math::max(-1, -6); // -1
*
* @example
* Math::max(5, 5); // 5
*
* @type Number a => a -> a -> a
*
*/
#[Curry]
protected static function max($a, $b)
{
return max([$a, $b]);
}
/**
* Power function
*
* Arithmetic exponentiation. Raises the second argument to the power
* of the first.
*
* @param number $a The power exponent
* @param number $b The power base
* @return number The base raised to the exponent's power
* @example
* Math::pow(3, 2); // 2 ^ 3 = 8
*
* @type Number a => a -> a -> a
*
* @example
* Math::pow(2, 3); // 3 ^ 2 = 9
*
*/
#[Curry]
protected static function pow($a, $b)
{
return pow($b, $a);
}
/**
* Arithmetic mean
*
* Returns the average of a list, or zero for an empty list.
*
* @param array $arr List of numbers
* @return number Mean of input list
* @example
* Math::mean([1, 2, 3]); // (1 + 2 + 3) / 3 = 2
*
* @example
* Math::mean([]); // 0
*
* @type Number a => [a] -> a
*
*/
#[Curry]
protected static function mean($arr)
{
return count($arr)
? array_sum($arr) / count($arr)
: 0;
}
}