📜 ⬆️ ⬇️

Numphp - library for working with numeric arrays

Hey.

I would like to introduce my first open source project hosted on github . This is a library that provides a convenient and completely new experience with php arrays of numbers. The inspiration for the creation was the numoy library in Python, which includes a wide range of possibilities for manipulating data and tools of linear algebra.

Honestly, I was surprised when I discovered that php still does not have a decent analogue of numpy. Of course, some attempts at implementation can be found on the githaba, but they are all very far from the original and do not share the same ideology. My goal was and is to create a similar library, at least in terms of usability and syntax brevity. Future plans will be improved performance.
')
In general, let's get started. Imagine that you have an array of numbers, for example, representing temperature values ​​over a period of time.

$list = [16, 22, -6, 23, -1, 13, 24, -23, 22]; 

And for further work you need to select only those values ​​from this array when the temperature was above zero. In the classic case, you would do something like this:

 $result = []; for($i=0; $i<count($list); $i++) if ($list[$i] >= 0) $result[] = $list[$i]; 

As a result, we obtain an array of 6 elements. But, for such a simple and trivial operation, we typed too much code. Of course, you can use short versions like array_walk or similar ones, but there will still be a lot of explicit logic.

The numphp library also provides a simple, but at the same time, rich functionality for retrieving data from a numeric array by condition. To solve the same problem, you can simply write:

 // cast our array to the numphp array $list = new np_array($list); $result = $list[$list['> 0']]; 

Just like that!

Moreover, this is only the beginning. You can manipulate this object as you like and even set values ​​according to a certain condition.

 $list[$list['< 0']] = 0; //result [16, 22, 0, 23, 0, 13, 24, 0, 22] 

But wait. So far, we have only talked about sampling and changing data. What about math operations? Good question.

Let's imagine, you are creating an RPG game and your hero has some abilities presented in this format:

 $powers = [62, 88, 34, 29]; 

Now, during a level up, you want to increase each ability by a certain indicator. Again, what would you do without numphp?

 for($i=0; $i<count($powers); $i++) $powers[$i]++; 

This code works, but does it look amazing? You need to write these cycles over and over again. Compare this with the elegant solution of the numphp library:

 $powers = $powers->add(1); 

Moreover, you can combine the previous two possibilities and, say, increase only those abilities that currently have a value less than 30:

 $powers = $powers[$powers['< 30']]->add(1); 

Another cool feature is that you can perform mathematical operations and comparisons between two arrays in the same simple and understandable syntax:

 $powers = [62, 88, 34, 29]; $intensify = [2, 5, 4, 1]; $result = $powers->add($intensify); //result [64, 93, 38, 30] 

As you can see, numphp provides elegant syntactic sugar for routine operations with numeric arrays. I described only the main idea and possibilities of the library at the moment. Additionally, convenient helpers like generators (an array of zeros, ones, a range, and even a Fibonacci series), a random module, and so on have already been implemented.

Please note that the library is only at the very beginning of its development and I plan to add new features. The most important of them at the moment is the ability to work with n-dimensional arrays (matrices, for example) and perform all basic types of operations from linear algebra.

Additional features and documentation can be found here .

Also, if you want to contribute to the development - I will be glad to discuss any issues.

Source: https://habr.com/ru/post/347470/


All Articles