{"id":13828606,"url":"https://github.com/apollonin/numphp","last_synced_at":"2025-07-09T06:32:24.854Z","repository":{"id":56949493,"uuid":"116257033","full_name":"apollonin/numphp","owner":"apollonin","description":"Numphp is a library for manipulating numbers","archived":false,"fork":false,"pushed_at":"2019-03-19T20:11:58.000Z","size":120,"stargazers_count":84,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-05T09:18:10.650Z","etag":null,"topics":["numbers","numphp","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apollonin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-04T12:22:05.000Z","updated_at":"2024-08-04T11:54:56.000Z","dependencies_parsed_at":"2022-08-21T07:50:21.881Z","dependency_job_id":null,"html_url":"https://github.com/apollonin/numphp","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollonin%2Fnumphp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollonin%2Fnumphp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollonin%2Fnumphp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apollonin%2Fnumphp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apollonin","download_url":"https://codeload.github.com/apollonin/numphp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["numbers","numphp","php"],"created_at":"2024-08-04T09:02:54.660Z","updated_at":"2024-11-20T08:30:52.755Z","avatar_url":"https://github.com/apollonin.png","language":"PHP","readme":"# Numphp\n\n[![Build Status](https://travis-ci.org/apollonin/numphp.svg?branch=master)](https://travis-ci.org/apollonin/numphp)\n[![Latest Stable Version](https://poser.pugx.org/apollonin/numphp/v/stable)](https://packagist.org/packages/apollonin/numphp)\n[![Total Downloads](https://poser.pugx.org/apollonin/numphp/downloads)](https://packagist.org/packages/apollonin/numphp)\n[![License](https://poser.pugx.org/apollonin/numphp/license)](https://packagist.org/packages/apollonin/numphp)\n[![codecov](https://codecov.io/gh/apollonin/numphp/branch/master/graph/badge.svg)](https://codecov.io/gh/apollonin/numphp)\n[![Maintainability](https://api.codeclimate.com/v1/badges/9cda6d0e7e7967900ff2/maintainability)](https://codeclimate.com/github/apollonin/numphp/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/9cda6d0e7e7967900ff2/test_coverage)](https://codeclimate.com/github/apollonin/numphp/test_coverage)\n\nNumphp 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.\n\nContributions are highly appreciated.\n\n## Installation\n\n### With composer\n\n```\ncomposer require apollonin/numphp\n```\n\n\n## Available features\n\n**Arrays**\n\n* get item by index\n* get items by array of indexes\n* get items by condition\n  * eq, gt, gte, lt, lte, neq - equals, greater than, and so on\n* get items by complex conditions\n  * b_and, b_or - bitwise AND and OR\n* set items values according to conditions, indexes or slices\n* apply math operations to whole array\n  * mul, div, add, sub, pow, mod\n* get slice of array\n* get statistical values from array\n  * count, max, mean, median, min, sum\n  * describe - special method that displays all above values\n* Get dimensional data\n  * shape\n  * dimension\n* Concatenate arrays\n\nnp_array also has classical array behaviour. So you are able to iterate through it as usual.\n\n**Matrix**\n\nMatrix is a special case of arrays. At the moment, we only support 2d matrices. Full support for n-dimensional matrices is on the way. \n\nYou can perform all the same operations and comparisons as with arrays. Refer to Matrix section below in usage examples.\n\n**Dimensional Manipulation**\n\nYou are able to change dimensions for existed array or matrix. Use `flatten` or `reshape` methods.\n\n**Random Module**\n\nNumphp also provides convenient ways to generate new np_arrays and populate them with random values. Available methods are\n\n* rand\n* randint\n\nIf `size` parameter is given, returns np_array with appropriate elements. Otherwise, it returns single random value.\n\n**Generators**\n\nFor quick stub array creation you may use these convenient predefined methods\n\n* ones - creates array full of 1\n* zeros - creates array full of 0 \n* full- creates array full of provided fill_value\n* arange - creates evenly spaced values within a given interval.\n* fib - creates Fibonacci numbers\n* formula - returns sequence of numbers, based on provided formula\n\n\n## Usage Examples\n\n### Indexing\n\n**create new array**\n```php\n$list = new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);\n```\n\n**Get items by their indexes**\n\n```php\n$result = $list[[2,3]];\n\n// result\n[2, 3]\n```\n\nTo get item as single value - pass index as single value as well\n\n```php\n$result = $list[1];\n\n// result\n1\n```\n\n**Get items by condition**\n\n```php\n$result = $list[$list-\u003egt(5)];\n\n// result\n[6, 7, 8, 9]\n```\n\nYou may also access index by string representations of comparison. \n\n```php\n// gives the same result as above\n$result = $list[$list['\u003e 5']];\n```\n\n*Important note about conditional indexing: conditional operator returns masking array:*\n\n\n\n```php\n  $mask = $list-\u003egt(5);\n\n  // mask\n  [false, false, false, false, false, false, true, true, true, true]\n\n  // and then\n  $result = $list[$mask];\n\n  // result \n  [6, 7, 8, 9]\n```\n\nYou also can pass another array as an argument. In this case the comparison will be applied for each element respectively.\n\n```php\n$result = $list[$list-\u003egt([5, 6, 7, 8, 9, 3, 4, 5, 6, 7])];\n\n// result\n[6, 7, 8, 9]\n```\n\n\n**Get items by conditions**\n\n*b_and* - \"bitwise\" and\n\n```php\n$resuilt = $list[Bitwise::b_and($list-\u003egte(5), $list-\u003elt(8))];\n\n// result\n[5, 6, 7]\n```\n\n**Array-like behaviour**\n\nYou may also iterate your np_array object as usual\n\n```php\nforeach ($list as $item) {\n    echo $item . \" \";\n}\n\n// output\n0 1 2 3 4 5 6 7 8 9\n```\n\n\n### Slicing\n\nYou 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.\n\n```php\n$result = $list['1:5'];\n\n//result\n[1, 2, 3, 4]\n\n\n$result = $list['1:5:2'];\n\n//result\n[1, 3]\n```\n\nYou can even skip `stop` and `step` values, which means: get all items from `start` to the end of array.\n\n```php\n$result = $list['1:'];\n\n//result\n[1, 2, 3, 4, 5, 6, 7, 8, 9]\n``` \n\nYou may even skip `start` value; it will be considered as 0 in this case\n\n```php\n$result = $list[':'];\n\n//result\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n```\n\nNegative `start` or `stop` means indexes count from the end of array\n\n```php\n$result = $list['-7:6'];\n\n//result\n[3, 4, 5]\n```\n\n\n### Set item values\n\n**Set items by indexes**\n\n```php\n$result = clone($list);\n$result[[2,3]] = 999;\n\n// result\n[0, 1, 999, 999, 4, 5, 6, 7, 8, 9]\n```\n\n**Set items by conditions**\n\n```php\n$result = clone($list);\n$result[$result-\u003egte(5)] = 999;\n\n// result\n[0, 1, 2, 3, 4, 999, 999, 999, 999, 999]\n```\n\n**Set items by slice**\n\n```php\n$result = clone($list);\n$result['1:3'] = 999;\n\n// result\n[0, 999, 999, 3, 4, 5, 6, 7, 8, 9]\n```\n\n**Adding new items**\n\nOf course, you may add new items as usual\n\n```php\n$result = clone($list);\n$result[] = 999;\n\n// result \n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 999]\n```\n\n### Math operations\n\nYou are able to apply certain math operations to the whole array. It will apply to each element.\n\n```php\n$result = $list-\u003eadd(100);\n\n// result \n[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]\n```\n\nYou may also perform math operation under two np_arrays\n\n```php\n$result = $list-\u003eadd(new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))\n\n//result\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n```\n\nOr event np_array and normal array!\n\n```php\n$result = $list-\u003eadd([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);\n\n//result\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n```\n\n\n\n### Random module\n\n**Create array with random floats**\n\n```php\nuse numphp\\Random\\Random;\n\n$result = Random::rand(5)\n\n// result\n[0.64488127438579, 0.21702189986455, 0.96931800524207, 0.78197341448719, 0.89214772632911]\n\n```\n\n**Array with random integers**\n\n```php\nuse numphp\\Random\\Random;\n\n$result = Random::randint(5, 15, 10);\n\n// result\n[13, 9, 12, 14, 6, 15, 8, 9, 5, 13]\n```\n\n\n### Generators module\n\n**create array full of zeros, ones or fill_value**\n\n```php\nuse numphp\\Generator\\Generator;\n\n$result = Generator::zeros(5);\n\n//result\n[0, 0, 0, 0, 0]\n\n\n$result = Generator::ones(5);\n\n//result\n[1, 1, 1, 1, 1]\n\n$result = Generator::full(5, 999);\n\n//result\n[999, 999, 999, 999, 999]\n```\n\n**Create array within a range and given interval**\n\n```php\nuse numphp\\Generator\\Generator;\n\n$result = Generator::arange(1, 15, 2);\n\n//result\n[1, 3, 5, 7, 9, 11, 13]\n```\n\n**Generate N [Fibonacci](https://en.wikipedia.org/wiki/Fibonacci_number) numbers**\n\n```php\nuse numphp\\Generator\\Generator;\n\n$result = Generator::fib(6);\n\n//result\n[1, 1, 2, 3, 5, 8]\n```\n\n\n**Generate numbers according to formula**\n\nProvide [callable](http://php.net/manual/en/language.types.callable.php) as a first argument. It must return value, that will be used in sequence.\n\n```php\nuse numphp\\Generator\\Generator;\n\n$result = Generator::formula(function($n){return 2*$n+1;}, 1, 5);\n\n//result\n[3, 5, 7, 9]\n```\n\n**Generate matrix with given diagonal**\n\n```\n$matrix = Generator::diagonal([5, 3, 1]);\n\n// matrix\n[[5, 0, 0],\n[0, 3, 0],\n[0, 0, 1]]\n```\n\n\n### Matrix operations\n\nGenerally the syntax and features are the same as for arrays\n\n**Creation**\n\n```\n$matrix = new np_array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]);\n\n// matrix\n[[ 0,  1,  2,  3],\n [ 4,  5,  6,  7],\n [ 8,  9, 10, 11]]\n```\n\n**Indexing**\n\nIndexing is done in respect to X-axis (rows)\n\n```\n$result = $matrix[0];\n\n//result\n[0, 1, 2, 3]\n```\n\n**Slicing**\n\n```\n$result = $matrix['1:3'];\n\n//result\n[[ 4,  5,  6,  7],\n [ 8,  9, 10, 11]]\n```\n\n**Comparisons**\n\n```\n$result = $matrix[$matrix-\u003egt(5)];\n\n//result\n[6, 7, 8, 9, 10, 11]\n```\n\nKeep in mind 'masking' feature\n\n```\n$mask = $matrix-\u003egt(5);\n\n//mask \n[[false, false, false, false],\n[false, false, true, true],\n[true, true, true, true]]\n```\n\n**Changing values**\n\n```\n$matrix[$matrix-\u003egte(5)] = 999;\n\n//matrix\n[[  0,   1,   2,   3],\n [  4, 999, 999, 999],\n [999, 999, 999, 999]]\n```\n\n**Math operations**\n\n```\n$result = $matrix-\u003emul(5);\n\n//result\n[[ 0,  5, 10, 15],\n [20, 25, 30, 35],\n [40, 45, 50, 55]]\n```\n\n**Get shape of matrix**\n\n```\n$shape = $matrix-\u003eshape;\n\n//shape: [rows, cols]\n[3, 4]\n```\n\nAnd if you just need count of dimensions\n\n```\n$dimensions = $matrix-\u003edimensions;\n\n//dimensions\n2\n```\n\n\n**Diagonal**\n\n```\n$result = $matrix-\u003ediagonal();\n\n//result \n[0, 5, 10]\n\n```\n\nor you can set offset for diagonal\n\n```\n$result = $matrix-\u003ediagonal(2);\n\n//result \n[2, 7]\n\n```\n\n## Changing dimensions\n\n**Flatten matrix**\n\nYou can get 1-D array from matrix.\n\n```\n$result = $matrix-\u003eflatten();\n\n//result\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n```\n\n**Reshaping**\n\nYou also can change current shape of matrix to any desired.\n\n```\n$result = $matrix-\u003ereshape([6, 2]);\n\n//result\n[[ 0,  1],\n [ 2,  3],\n [ 4,  5],\n [ 6,  7],\n [ 8,  9],\n [10, 11]]\n```\n\n\n## Concatenation\n\n**concatenate arrays**\n\nYou can concatenate two or more arrays into one. Logic is similar to [array_merge](http://php.net/manual/en/function.array-merge.php) native php method\n\n```\n$l1 = Generator::arange(1, 5);\n$l2 = Generator::arange(5, 8);\n$l3 = Generator::arange(8, 10);\n\n$result = np::concatenate($l1, $l2, $l3)\n\n//result\n[1, 2, 3, 4, 5, 6, 7, 8, 9]\n```\n\n**concatenate matrixes**\n\nThe same logic can be applied to matrixes\n\n```\n$m2 = Generator::ones([1, 4]);\n$result = np::concatenate($matrix, $m2)\n\n//result\n[[ 0,  1,  2,  3],\n [ 4,  5,  6,  7],\n [ 8,  9, 10, 11],\n [ 1,  1,  1,  1]]\n\n```","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollonin%2Fnumphp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapollonin%2Fnumphp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapollonin%2Fnumphp/lists"}