Download the PHP package apollonin/numphp without Composer
On this page you can find all versions of the php package apollonin/numphp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package numphp
Numphp
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.
Contributions are highly appreciated.
Installation
With composer
Available features
Arrays
- 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.
Matrix
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.
You can perform all the same operations and comparisons as with arrays. Refer to Matrix section below in usage examples.
Dimensional Manipulation
You are able to change dimensions for existed array or matrix. Use flatten
or reshape
methods.
Random Module
Numphp also provides convenient ways to generate new np_arrays and populate them with random values. Available methods are
- rand
- randint
If size
parameter is given, returns np_array with appropriate elements. Otherwise, it returns single random value.
Generators
For quick stub array creation you may use these convenient predefined methods
- 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
Get items by their indexes
To get item as single value - pass index as single value as well
Get items by condition
You may also access index by string representations of comparison.
Important note about conditional indexing: conditional operator returns masking array:
You also can pass another array as an argument. In this case the comparison will be applied for each element respectively.
Get items by conditions
b_and - "bitwise" and
Array-like behaviour
You may also iterate your np_array object as usual
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.
You can even skip stop
and step
values, which means: get all items from start
to the end of array.
You may even skip start
value; it will be considered as 0 in this case
Negative start
or stop
means indexes count from the end of array
Set item values
Set items by indexes
Set items by conditions
Set items by slice
Adding new items
Of course, you may add new items as usual
Math operations
You are able to apply certain math operations to the whole array. It will apply to each element.
You may also perform math operation under two np_arrays
Or event np_array and normal array!
Random module
Create array with random floats
Array with random integers
Generators module
create array full of zeros, ones or fill_value
Create array within a range and given interval
Generate N Fibonacci numbers
Generate numbers according to formula
Provide callable as a first argument. It must return value, that will be used in sequence.
Generate matrix with given diagonal
Matrix operations
Generally the syntax and features are the same as for arrays
Creation
Indexing
Indexing is done in respect to X-axis (rows)
Slicing
Comparisons
Keep in mind 'masking' feature
Changing values
Math operations
Get shape of matrix
And if you just need count of dimensions
Diagonal
or you can set offset for diagonal
Changing dimensions
Flatten matrix
You can get 1-D array from matrix.
Reshaping
You also can change current shape of matrix to any desired.
Concatenation
concatenate arrays
You can concatenate two or more arrays into one. Logic is similar to array_merge native php method
concatenate matrixes
The same logic can be applied to matrixes