5 Star 3 Fork 1

Astraeux / phpnum

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

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]])
MIT License Copyright (c) 2016 phpnum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

PHP科学计算扩展 A PHP extension for scientific computing expand collapse
C
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
C
1
https://gitee.com/astraeux/phpnum.git
git@gitee.com:astraeux/phpnum.git
astraeux
phpnum
phpnum
master

Search