Numphp
, (*1)
Numphp is a library for number manipulations. If you have an array of numbers, numphp gives you an ability to perform a wide range of useful operations., (*2)
Contributions are highly appreciated., (*3)
Installation
With composer
composer require apollonin/numphp
Available features
Arrays, (*4)
- get item by index
- get items by array of indexes
- get items by condition
- eq, gt, gte, lt, lte, neq - equals, greater than, and so on
- get items by complex conditions
- b_and, b_or - bitwise AND and OR
- set items values according to conditions, indexes or slices
- apply math operations to whole array
- mul, div, add, sub, pow, mod
- get slice of array
- get statistical values from array
- count, max, mean, median, min, sum
- describe - special method that displays all above values
- Get dimensional data
- Concatenate arrays
np_array also has classical array behaviour. So you are able to iterate through it as usual., (*5)
Matrix, (*6)
Matrix is a special case of arrays. At the moment, we only support 2d matrices. Full support for n-dimensional matrices is on the way., (*7)
You can perform all the same operations and comparisons as with arrays. Refer to Matrix section below in usage examples., (*8)
Dimensional Manipulation, (*9)
You are able to change dimensions for existed array or matrix. Use flatten
or reshape
methods., (*10)
Random Module, (*11)
Numphp also provides convenient ways to generate new np_arrays and populate them with random values. Available methods are, (*12)
If size
parameter is given, returns np_array with appropriate elements. Otherwise, it returns single random value., (*13)
Generators, (*14)
For quick stub array creation you may use these convenient predefined methods, (*15)
- ones - creates array full of 1
- zeros - creates array full of 0
- full- creates array full of provided fill_value
- arange - creates evenly spaced values within a given interval.
- fib - creates Fibonacci numbers
- formula - returns sequence of numbers, based on provided formula
Usage Examples
Indexing
create new array, (*16)
$list = new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
Get items by their indexes, (*17)
$result = $list[[2,3]];
// result
[2, 3]
To get item as single value - pass index as single value as well, (*18)
$result = $list[1];
// result
1
Get items by condition, (*19)
$result = $list[$list->gt(5)];
// result
[6, 7, 8, 9]
You may also access index by string representations of comparison., (*20)
// gives the same result as above
$result = $list[$list['> 5']];
Important note about conditional indexing: conditional operator returns masking array:, (*21)
$mask = $list->gt(5);
// mask
[false, false, false, false, false, false, true, true, true, true]
// and then
$result = $list[$mask];
// result
[6, 7, 8, 9]
You also can pass another array as an argument. In this case the comparison will be applied for each element respectively., (*22)
$result = $list[$list->gt([5, 6, 7, 8, 9, 3, 4, 5, 6, 7])];
// result
[6, 7, 8, 9]
Get items by conditions, (*23)
b_and - "bitwise" and, (*24)
$resuilt = $list[Bitwise::b_and($list->gte(5), $list->lt(8))];
// result
[5, 6, 7]
Array-like behaviour, (*25)
You may also iterate your np_array object as usual, (*26)
foreach ($list as $item) {
echo $item . " ";
}
// output
0 1 2 3 4 5 6 7 8 9
Slicing
You may get slices of your np_array in a very convenient way. Just pass string formatted like start:[stop][:step]
as index and you'll get result., (*27)
$result = $list['1:5'];
//result
[1, 2, 3, 4]
$result = $list['1:5:2'];
//result
[1, 3]
You can even skip stop
and step
values, which means: get all items from start
to the end of array., (*28)
$result = $list['1:'];
//result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
You may even skip start
value; it will be considered as 0 in this case, (*29)
$result = $list[':'];
//result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Negative start
or stop
means indexes count from the end of array, (*30)
$result = $list['-7:6'];
//result
[3, 4, 5]
Set item values
Set items by indexes, (*31)
$result = clone($list);
$result[[2,3]] = 999;
// result
[0, 1, 999, 999, 4, 5, 6, 7, 8, 9]
Set items by conditions, (*32)
$result = clone($list);
$result[$result->gte(5)] = 999;
// result
[0, 1, 2, 3, 4, 999, 999, 999, 999, 999]
Set items by slice, (*33)
$result = clone($list);
$result['1:3'] = 999;
// result
[0, 999, 999, 3, 4, 5, 6, 7, 8, 9]
Adding new items, (*34)
Of course, you may add new items as usual, (*35)
$result = clone($list);
$result[] = 999;
// result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 999]
Math operations
You are able to apply certain math operations to the whole array. It will apply to each element., (*36)
$result = $list->add(100);
// result
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
You may also perform math operation under two np_arrays, (*37)
$result = $list->add(new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
//result
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Or event np_array and normal array!, (*38)
$result = $list->add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
//result
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Random module
Create array with random floats, (*39)
use numphp\Random\Random;
$result = Random::rand(5)
// result
[0.64488127438579, 0.21702189986455, 0.96931800524207, 0.78197341448719, 0.89214772632911]
Array with random integers, (*40)
use numphp\Random\Random;
$result = Random::randint(5, 15, 10);
// result
[13, 9, 12, 14, 6, 15, 8, 9, 5, 13]
Generators module
create array full of zeros, ones or fill_value, (*41)
use numphp\Generator\Generator;
$result = Generator::zeros(5);
//result
[0, 0, 0, 0, 0]
$result = Generator::ones(5);
//result
[1, 1, 1, 1, 1]
$result = Generator::full(5, 999);
//result
[999, 999, 999, 999, 999]
Create array within a range and given interval, (*42)
use numphp\Generator\Generator;
$result = Generator::arange(1, 15, 2);
//result
[1, 3, 5, 7, 9, 11, 13]
Generate N Fibonacci numbers, (*43)
use numphp\Generator\Generator;
$result = Generator::fib(6);
//result
[1, 1, 2, 3, 5, 8]
Generate numbers according to formula, (*44)
Provide callable as a first argument. It must return value, that will be used in sequence., (*45)
use numphp\Generator\Generator;
$result = Generator::formula(function($n){return 2*$n+1;}, 1, 5);
//result
[3, 5, 7, 9]
Generate matrix with given diagonal, (*46)
$matrix = Generator::diagonal([5, 3, 1]);
// matrix
[[5, 0, 0],
[0, 3, 0],
[0, 0, 1]]
Matrix operations
Generally the syntax and features are the same as for arrays, (*47)
Creation, (*48)
$matrix = new np_array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]);
// matrix
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]
Indexing, (*49)
Indexing is done in respect to X-axis (rows), (*50)
$result = $matrix[0];
//result
[0, 1, 2, 3]
Slicing, (*51)
$result = $matrix['1:3'];
//result
[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]
Comparisons, (*52)
$result = $matrix[$matrix->gt(5)];
//result
[6, 7, 8, 9, 10, 11]
Keep in mind 'masking' feature, (*53)
$mask = $matrix->gt(5);
//mask
[[false, false, false, false],
[false, false, true, true],
[true, true, true, true]]
Changing values, (*54)
$matrix[$matrix->gte(5)] = 999;
//matrix
[[ 0, 1, 2, 3],
[ 4, 999, 999, 999],
[999, 999, 999, 999]]
Math operations, (*55)
$result = $matrix->mul(5);
//result
[[ 0, 5, 10, 15],
[20, 25, 30, 35],
[40, 45, 50, 55]]
Get shape of matrix, (*56)
$shape = $matrix->shape;
//shape: [rows, cols]
[3, 4]
And if you just need count of dimensions, (*57)
$dimensions = $matrix->dimensions;
//dimensions
2
Diagonal, (*58)
$result = $matrix->diagonal();
//result
[0, 5, 10]
or you can set offset for diagonal, (*59)
$result = $matrix->diagonal(2);
//result
[2, 7]
Changing dimensions
Flatten matrix, (*60)
You can get 1-D array from matrix., (*61)
$result = $matrix->flatten();
//result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Reshaping, (*62)
You also can change current shape of matrix to any desired., (*63)
$result = $matrix->reshape([6, 2]);
//result
[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]]
Concatenation
concatenate arrays, (*64)
You can concatenate two or more arrays into one. Logic is similar to array_merge native php method, (*65)
$l1 = Generator::arange(1, 5);
$l2 = Generator::arange(5, 8);
$l3 = Generator::arange(8, 10);
$result = np::concatenate($l1, $l2, $l3)
//result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
concatenate matrixes, (*66)
The same logic can be applied to matrixes, (*67)
$m2 = Generator::ones([1, 4]);
$result = np::concatenate($matrix, $m2)
//result
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 1, 1, 1, 1]]