akondas / php-sds-polyfill

A polyfill for the "PHP Scientific Data Structures" package.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP-SDS : Scientific Data Structures (polyfill)

Author Build Status Software License Packagist Version Total Downloads

This library is a pure and complete PHP replacement for the native SDS extension (Scientific Data Structures). This package is slower than its native counterpart, but it's useful to ensure compatibility with systems where no extensions can be installed.

The SDS library provides the following classes (mainly ported from Python (SciPy & Pandas)):

  • Tensor (abstract):
    • IntTensor
    • FloatTensor
  • Matrix (abstract):
    • IntMatrix : Not yet completed (but usable!)
    • FloatMatrix : Not yet completed (but usable!)
  • DataFrame : Not yet implemented
  • Series : Not yet implemented

Usage

How to install

To install this package in your project, type the command

composer require php-sds/polyfill

Tensor

Tensors are something like a multidimensional array, or a generalization of a matrix.

To construct them, we have to use some helper methods:

<?php

use SDS\FloatTensor;
use SDS\IntTensor;

// Example: We can represent 100 8x8 matrix
$fT = FloatTensor::zeros([100, 8, 8]);

// Example: We can represent 100 frames of 128x128px with three color channels
$iT = IntTensor::zeros([100, 3, 128, 128]);

// There are other default constructors/factories
$t = IntTensor::ones([100, 3, 128, 128]);
$t = IntTensor::constant(42, [100, 3, 128, 128]);

We can access every value contained inside a Tensor instance:

<?php

$iT[[37, 1, 74, 25]] = 389;
$v = $iT[[37, 1, 74, 25]]; // $v === 389;

We can slice Tensor instances:

<?php

/**
 * $t1 = 1 2 3
 *       4 5 6
 *       7 8 9
 * 
 * $t2 = 1 2
 *       4 5
 */
$t2 = $t1[[ [0, 1], [0, 1] ]];

We can assign slices to sub-regions of our Tensor instances:

<?php

/**
 * $t1 = 0 0 0
 *       0 0 0
 *       0 0 0
 */
$t1 = \SDS\IntTensor::zeros([3, 3]);

/**
 * $t2 = 1 1
 *       1 1
 */
$t2 = \SDS\IntTensor::ones([2, 2]);

/**
 * $t1 = 1 1 0
 *       1 1 0
 *       0 0 0
 */
$t1[[ [0, 1], [0, 1] ]] = $t2;

About

A polyfill for the "PHP Scientific Data Structures" package.

License:MIT License


Languages

Language:PHP 100.0%