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/
You can install PHPSci using composer:
composer require phpsci/phpsci:dev-master
ATTENTION: You must install PHPSci extension, otherwise it won't work.
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.
$a = [[1,2],[3,4]];
print_r($a);
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
$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.
There are two ways to view your data in an PHPSci array:
$a = PHPSci::fromArray([[1,2],[3,4]]);
echo $a;
[
[ 1.000000 2.000000 ]
[ 3.000000 4.000000 ]
]
$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.