Giter VIP home page Giter VIP logo

phpsci-ext's Introduction

PHPSci CArray Extension

Build Status Documentation Status

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.

Building

It's really easy to compile this extension using Linux environments.

Requirements

  • php-devel (php-dev)
  • PHP 7.x
  • OpenBLAS

Compiling

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.

Using CArrays

Creating CArrays

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:

Converting CArrays to PHP Arrays

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.

Creating from PHP Arrays

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
        )

)

Basic Operations

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
        )

)

Destroying CArrays [IMPORTANT]

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.

How it works?

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.

Roadmap for version 1.0

Before release, we need to finish the following:

  1. Implement sum, abs, add, subtract, multiply,exp and std operations
  2. Implement array1d and array0d capabilities (allow 1-D and double operations and return)
  3. Implement zeros_like, ones, ones_like, linspace, eye, empty, fill and arange initializers
  4. Implement ravel and flatten transformations
  5. Implement matrix_power, inner, outer, kron and matmul product operations
  6. Implement invert from matrix inversion

phpsci-ext's People

Contributors

henrique-borba avatar randptr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.