Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.
/ phpsci-carray Public archive

[ARCHIVED] PHP library for scientific computing powered by C. See https://github.com/NumPower/numpower

License

MIT, BSD-3-Clause licenses found

Licenses found

MIT
LICENSE
BSD-3-Clause
COPYING
Notifications You must be signed in to change notification settings

phpsci/phpsci-carray

Repository files navigation

PHPSci

Latest Stable Version Total Downloads Latest Unstable Version License

Documentation Status Build Status Build Status Scrutinizer Code Quality

Efficient PHP library for data scientists

PHPSci is a PHP Library for scientific computing powered by C. You must compile and install PHPSci CArray Extension.

It enables scientific operations in PHP to be performed up to 800 times faster than current implementations.

http://phpsci.readthedocs.io/en/latest/

Installation

You can install PHPSci using composer:

composer require phpsci/phpsci:dev-master

ATTENTION: You must install PHPSci extension, otherwise it won't work.

Getting Started

PHPSci arrays are different from PHP arrays, they are called CArrays and work in a peculiar way. Let's look at the result of the print_r function in a PHP array and a twin CArray.

PHP Array

$a = [[1,2],[3,4]];
print_r($a);
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
        )

)

PHPSci CArray

$a = PHPSci::fromArray([[1,2],[3,4]]);
print_r($a);
PHPSci\PHPSci Object
(
    [internal_pointer:protected] => PHPSci\Kernel\Orchestrator\MemoryPointer Object
        (
            [uuid:protected] => 1
            [x:protected] => 2
            [y:protected] => 2
            [carray_internal:protected] => CArray Object
                (
                    [uuid] => 1
                    [x] => 2
                    [y] => 2
                )

        )

)

This happens because print_r only works with PHP's natural functions, objects, and arrays, which is not the case for a CArray. An array of PHPSci is just a pointer to memory. It carries with it the position of memory where its data has been allocated.

The MemoryPointer object is a mirror of the CArray object, it carries with it the information needed to communicate with the C backend.

To view your data, you can use the echo method or transform your CArray into a PHP array.

Data Visualization

There are two ways to view your data in an PHPSci array:

Using echo

$a = PHPSci::fromArray([[1,2],[3,4]]);
echo $a;
[
  [ 1.000000  2.000000 ]
  [ 3.000000  4.000000 ]
]

Transforming into a PHP array

$a = PHPSci::fromArray([[1,2],[3,4]]);
print_r($a->toArray());
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
        )

)

Try to perform all the necessary calculations before turning your PHPSci array into a PHP array.

The echo command is considerably more efficient than the toArray command. Try to use the toArray only when you want to use the results in a natively PHP function.