• PHP Video Tutorials

PHP - Stats Stat Powersum() Function



Definition and Usage

The stats_stat_powersum() function can return the power sum of a vector.

Syntax

  float stats_stat_powersum( array $arr, float $power )

Parameters

Sr.No Parameter Description
1

arr

The input array

2

power

The power

Return Values

The stats_stat_powersum() function can return the sum of power-th power of a vector represented as an array arr.

Dependencies

This function was first introduced in statistics extension (PHP 4.0.0 and PEAR 1.4.0). We have used latest release of stats-2.0.3 (PHP 7.0.0 or newer and PEAR 1.4.0 or newer) for this tutorial

Example

In the following example, we compute sum of 2-th power of a vector represented as an array array(1, 2, 4).

<?php
   var_dump(stats_stat_powersum(array(1, 2, 4), 2));
?>

Output

This will produce following result −

float(21)

Example

In the following example, In the following example, we compute sum of 0-th power of a vector represented as an array array(1, 2, 4).

<?php
   var_dump(stats_stat_powersum(array(1, 2, 4), 0));
?>

Output

This will produce following result −

float(3)

Example

In the following example, we compute sum of -2-th power of a vector represented as an array array(1, 2, 4).

<?php
   var_dump(stats_stat_powersum(array(1, 2, 4), -2));
?>

Output

This will produce following result −

float(1.3125)

Example

Following is an error case. In the following example, we pass empty array. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_stat_powersum(array(), 1));
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_stat_powersum(): Both value and power are zero

float(0)
Advertisements