2017 © Pedro Peláez
 

library numphp

Library for numbers manipulation.

image

apollonin/numphp

Library for numbers manipulation.

  • Monday, June 18, 2018
  • by apollonin
  • Repository
  • 5 Watchers
  • 78 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 1 Open issues
  • 18 Versions
  • 20 % Grown

The README.md

Numphp

Build Status Latest Stable Version Total Downloads License codecov Maintainability Test Coverage, (*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
    • shape
    • dimension
  • 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)

  • rand
  • randint

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]]

The Versions

18/06 2018

dev-master

9999999-dev

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

filter array numbers

30/01 2018

dev-feature_operators_for_matrix

dev-feature_operators_for_matrix

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

29/01 2018

dev-improve_matrix_printer

dev-improve_matrix_printer

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

29/01 2018

1.8.0

1.8.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

29/01 2018

dev-feature-matrix-diagonal

dev-feature-matrix-diagonal

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

29/01 2018

dev-feature-shape-for-generators

dev-feature-shape-for-generators

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

28/01 2018

dev-feature-matrix-stats

dev-feature-matrix-stats

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

28/01 2018

dev-feature-matrix-max

dev-feature-matrix-max

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

28/01 2018

1.7.1

1.7.1.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

28/01 2018

1.7.0

1.7.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

28/01 2018

dev-feature-ndarray-support

dev-feature-ndarray-support

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

26/01 2018

1.6.2

1.6.2.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

25/01 2018

1.6.0

1.6.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

25/01 2018

1.5.0

1.5.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

24/01 2018

1.4.0

1.4.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

18/01 2018

1.2.0

1.2.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

18/01 2018

1.1.0

1.1.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers

16/01 2018

1.0.0

1.0.0.0

Library for numbers manipulation.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

filter array numbers