Git Product home page Git Product logo

phpnum's Introduction

phpnum

Build Status

A PHP extension for scientific computing. Inspired by NumPy & NumPHP.

查看中文文档

Table of contents


  1. Installing/Configuring
  2. Classes and methods

Installing/Configuring


Everything you should need to install phpnum on your system.

requirement

  • PHP 7 +

Installation

phpize
./configure
make && make install

make install copies num.so to an appropriate location, but you still need to enable the module in the PHP config file. To do so, either edit your php.ini or add a num.ini file in /path/to/php.d with the following contents: extension=num.so.

Classes and methods


Usage

  1. Class Num - Creates a Num object
  2. Class NumNdarray - Creates a N-dimensional array (ndarray) object
  3. Printing - Prints a ndarray object

Class Num


Description: Creates a Num object

Example
$num = new Num();

Class NumNdarray


Description: Creates a N-dimensional array (ndarray) object

Parameters

array: Array

Return value

ndarray: Object

Example
$num = new Num();
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);

Printing


Description: Prints a ndarray object

Example
$num = new Num();
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);
echo $ndarray;
/* output: 
array([
  [1,2,3],
  [2,3,4]
])
*/

Characteristics

  1. getData - Data of the ndarray
  2. getShape - Shape of ndarray dimensions
  3. getNdim - Number of ndarray dimensions
  4. getSize - Number of elements in the ndarray

getData


Description: Data of the ndarray

Parameters

None

Return value

data: Array

Example
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$ndarray->getData(); // returns array([1.0, 2, 3], [2, 3, 4])

getShape


Description: Shape of ndarray dimensions

Parameters

None

Return value

shape: Array

Example
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$ndarray->getShape(); // returns array(2, 3)

getNdim


Description: Number of ndarray dimensions

Parameters

None

Return value

ndim: LONG

Example
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$ndarray->getNdim(); // returns 2

getSize


Description: Number of elements in the ndarray

Parameters

None

Return value

size: LONG

Example
$ndarray = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$ndarray->getSize(); // returns 6

Statistics

  1. amin - Return the minimum of an array
  2. amax - Return the maximum of an array

amin


Description: Return the minimum of an array

Parameters

ndarray: Object

Return value

amin: Double

Example
$ndarray = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
$num->min($ndarray); // returns 1

amax


Description: Return the maximum of an array

Parameters

ndarray: Object

Return value

amax: Double

Example
$ndarray = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
$num->amax($ndarray); // returns 4

Arithmetic Operations

  1. add - Add an array to an other array
  2. sub - Subtract an array from an other array
  3. mult - Multiply an array by an other array
  4. div - an array divided by an other array

add


Description: Add an array to an other array

Parameters

ndarray: Object

Return value

ndarray: Object

Example
$a = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$b = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
echo $a->add($b);
/* output:
array([
  [4.2,3.5,4],
  [4.5,7,6]
])
*/

sub


Description: Subtract an array from an other array

Parameters

ndarray: Object

Return value

ndarray: Object

Example
$a = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$b = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
echo $a->sub($b);
/* output:
array([
  [-2.2,0.5,2],
  [-0.5,-1,2]
])
*/

mult


Description: Multiply an array by an other array

Parameters

ndarray: Object

Return value

ndarray: Object

Example
$a = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$b = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
echo $a->mult($b);
/* output:
array([
  [3.2,3,3],
  [5,12,8]
])
*/

div


Description: an array divided by an other array

Parameters

ndarray: Object

Return value

ndarray: Object

Example
$a = $num->array([[1.0, 2, 3], [2, 3, 4]]);
$b = $num->array([[3.2, 1.5, 1], [2.5, 4, 2]]);
echo $a->div($b);
/* output:
array([
  [0.3125,1.3333333333333,3],
  [0.8,0.75,2]
])
*/

Universal functions

  1. power - First array elements raised to powers from second array, element-wise
  2. square - Return the element-wise square of the input
  3. sqrt - Return the positive square-root of an array, element-wise
  4. exp - Calculate the exponential of all elements in the input array
  5. log - Natural logarithm, element-wise
  6. log10 - Return the base 10 logarithm of the input array, element-wise
  7. sin - Trigonometric sine, element-wise
  8. cos - Cosine element-wise
  9. tan - Compute tangent element-wise
  10. ceil - Return the ceiling of the input, element-wise
  11. floor - Return the floor of the input, element-wise
  12. fabs - Compute the absolute values element-wise

power


Description: First array elements raised to powers from second array, element-wise

Parameters

base: Object

exponent: Object or Double

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->power($ndarray, 3);
// returns array([[0.125, 1.0], [0.0, 8.0]])

square


Description: Return the element-wise square of the input

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->square($ndarray);
// returns array([[0.5, 1.0], [0.0, 4.0]])

sqrt


Description: Return the positive square-root of an array, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->sqrt($ndarray);
// returns array([[0.70710678118654757, 1.0], [0.0, 1.4142135623730951]])

exp


Description: Calculate the exponential of all elements in the input array

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->exp($ndarray);
// returns array([[1.6487212707001282, 2.7182818284590451], [1.0, 7.3890560989306504]])

log


Description: Natural logarithm, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [10, 2]]);
$num->log($ndarray);
// returns array([[-0.69314718055994529, 0.0], [2.3025850929940459, 0.69314718055994529]])

log10


Description: Return the base 10 logarithm of the input array, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[1e-1, 1], [10, 100]]);
$num->log10($ndarray);
// returns array([[-1.0, 0.0], [1.0, 2.0]])

sin


Description: Trigonometric sine, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->sin($ndarray);
// returns array([[0.47942553860420301, 0.8414709848078965], [0.0, 0.90929742682568171]])

cos


Description: Cosine element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->cos($ndarray);
// returns array([[0.87758256189037276, 0.54030230586813977], [1.0, -0.41614683654714241]])

tan


Description: Compute tangent element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->tan($ndarray);
// returns array([[0.54630248984379048, 1.5574077246549023], [0.0, -2.1850398632615189]])

ceil


Description: Return the ceiling of the input, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->ceil($ndarray);
// returns array([[1.0, 1.0], [0.0, 2.0]])

floor


Description: Return the floor of the input, element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, 2]]);
$num->floor($ndarray);
// returns array([[0.0, 1.0], [0.0, 2.0]])

fabs


Description: Compute the absolute values element-wise

Parameters

ndarray: Object

Return value

array: Array

Example
$ndarray = $num->array([[0.5, 1], [0, -2]]);
$num->fabs($ndarray);
// returns array([[0.0, 1.0], [0.0, 2.0]])

phpnum's People

Contributors

astraeux avatar

Watchers

James Cloos avatar PadagOne 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.