This is the extension used by PHPSci. It offers the CArray object in place of PHP arrays to make scientific calculations faster.
Although it is not necessary, or you want to create your own library of arrays, we recommend that you use PHPSci together with this extension.
ATTENTION: Misuse of this extension can cause excessive memory consumption and consequently system crash. See the CArray internals section if you want to know more about the internal operation of the extension.
It's really easy to compile this extension using Linux environments.
- php-devel (php-dev)
- PHP 7.x
- OpenBLAS
Clone the repository, cd
to the source folder and:
$ phpize
$ ./configure
$ make CFLAGS=-lopenblas
$ make test
$ make install
Don't forget to check if the extension is enabled in your php.ini file.
Apache/NGINX Users: Don't forget to restart your services.
Let's create two CArrays using the Identity
initializer:
$a = CArray::identity(2);
$b = CArray::identity(4);
print_r($a);
print_r($b);
CArray Object
(
[uuid] => 0
[x] => 2
[y] => 2
)
CArray Object
(
[uuid] => 1
[x] => 4
[y] => 4
)
It sounds strange, but calm down! You will not be able to view your array using
print_r
because CArrays are not PHP arrays. It's just pointers to memory, to view
your array you'll need to convert it to PHP Array:
Remember that this may require considerable time depending on the size of your CArray. Try performing all operations before converting to a PHP Array, and only, if needed of course.
$php_array = CArray::toArray($a->uuid, 2, 2);
print_r($php_array);
The
toArray()
static method receive 3 arguments:public static toArray(int uuid, int rows, int cols);
. For now, It's your job to keep track of your array dimension and sizes. Misuse can cause segment faults.
Array
(
[0] => Array
(
[0] => 1
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 1
)
)
Now we can see our array and use it with other PHP general libraries.
You also create CArrays from PHP Arrays, use the static toArray()
method
to create a CArray from PHP Array:
$a = CArray::fromArray([[0,1],[2,3]]);
print_r(CArray::toArray($a);
Array
(
[0] => Array
(
[0] => 0
[1] => 1
)
[1] => Array
(
[0] => 2
[1] => 3
)
)
Let's tranpose the CArray (matrix) we created above:
$c = CArray::transpose($a->uuid, 2,2);
print_r(CArray::toArray($c->uuid,2,2));
Array
(
[0] => Array
(
[0] => 0
[1] => 2
)
[1] => Array
(
[0] => 1
[1] => 3
)
)
After we are done with some CArrays or temporary ones, it's good to
destroy them by calling the destroy()
static method.
CArray::destroy($a->uuid);
CArray::destroy($b->uuid);
CArray::destroy($c->uuid);
In small cases, this may not cause trouble, but in larger scales if you don't destroy temporary CArrays, they will stay in memory until PHP runtime send the shutdown signal.
Internally CArrays are just C structures that can handle multiple arrays of data.
/************* /*************** /***************
/* CARRAY * ====> /* array1d * /* double[] *
/************* /*************** /***************
/*************** /*************** /************
/* array2d * /* array1d * /* double *
/*************** /*************** /************
...
A buffer called MemoryStack
handles all CArrays storage within your PC memory:
/********************** /*******************
/* MEMORYSTACK * / CArray UUID 0 *
/* Dynamic Allocated * =====> /*******************
/* Buffer * /* CArray UUID 1 *
/********************** /*******************
/* CArray UUID 2 *
/*******************
...
CArray talks with PHP frontend using only the MemoryPointer
object, it's the
CArray Object
you see returned during use and contains the uuid
property with
the position of your CArray
inside the MemoryStack
So, when you do operations like transpose
, the operation itself is only performed
with C
objects and absolutely no PHP arrays are involved in the process.
That's what makes PHPSci so much faster them PHP Arrays.
Before release, we need to finish the following:
- Implement
sum
,abs
,add
,subtract
,multiply
,exp
andstd
operations - Implement
array1d
andarray0d
capabilities (allow 1-D and double operations and return) - Implement
zeros_like
,ones
,ones_like
,linspace
,eye
,empty
,fill
andarange
initializers - Implement
ravel
andflatten
transformations - Implement
matrix_power
,inner
,outer
,kron
andmatmul
product operations - Implement
invert
from matrix inversion