Skip to content

Commit e3b7cb5

Browse files
author
Mark Baker
authored
I'm ignoring SUMIF() and SUMIFS() for the moment; so this should complete all the applicable Math/Trig functions;so the subset SERIESSUM() function and RANDBETWEEN() should complete Math/Trig (#2586)
1 parent c9d1df8 commit e3b7cb5

File tree

6 files changed

+83
-15
lines changed

6 files changed

+83
-15
lines changed

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public static function QUOTIENT($numerator, $denominator)
597597
* @param int $min Minimal value
598598
* @param int $max Maximal value
599599
*
600-
* @return float|int|string Random number
600+
* @return array|float|int|string Random number
601601
*/
602602
public static function RAND($min = 0, $max = 0)
603603
{
@@ -679,7 +679,7 @@ public static function ROUNDDOWN($number, $digits)
679679
* @param mixed $m Step
680680
* @param mixed[] $args An array of coefficients for the Data Series
681681
*
682-
* @return float|string The result, or a string containing an error
682+
* @return array|float|string The result, or a string containing an error
683683
*/
684684
public static function SERIESSUM($x, $n, $m, ...$args)
685685
{

src/PhpSpreadsheet/Calculation/MathTrig/Random.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Random
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* RAND.
1215
*
@@ -21,12 +24,20 @@ public static function rand()
2124
* RANDBETWEEN.
2225
*
2326
* @param mixed $min Minimal value
27+
* Or can be an array of values
2428
* @param mixed $max Maximal value
29+
* Or can be an array of values
2530
*
26-
* @return float|int|string Random number
31+
* @return array|float|int|string Random number
32+
* If an array of numbers is passed as an argument, then the returned result will also be an array
33+
* with the same dimensions
2734
*/
2835
public static function randBetween($min, $max)
2936
{
37+
if (is_array($min) || is_array($max)) {
38+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $min, $max);
39+
}
40+
3041
try {
3142
$min = (int) Helpers::validateNumericNullBool($min);
3243
$max = (int) Helpers::validateNumericNullBool($max);

src/PhpSpreadsheet/Calculation/MathTrig/SeriesSum.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class SeriesSum
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* SERIESSUM.
1215
*
@@ -17,10 +20,14 @@ class SeriesSum
1720
* @param mixed $m Step
1821
* @param mixed[] $args An array of coefficients for the Data Series
1922
*
20-
* @return float|string The result, or a string containing an error
23+
* @return array|float|string The result, or a string containing an error
2124
*/
2225
public static function evaluate($x, $n, $m, ...$args)
2326
{
27+
if (is_array($x) || is_array($n) || is_array($m)) {
28+
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 3, $x, $n, $m, ...$args);
29+
}
30+
2431
try {
2532
$x = Helpers::validateNumericNullSubstitution($x, 0);
2633
$n = Helpers::validateNumericNullSubstitution($n, 0);

src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ function ($index) use ($cellReference) {
5252

5353
/** @var callable[] */
5454
private const CALL_FUNCTIONS = [
55-
1 => [Statistical\Averages::class, 'average'],
56-
[Statistical\Counts::class, 'COUNT'], // 2
57-
[Statistical\Counts::class, 'COUNTA'], // 3
58-
[Statistical\Maximum::class, 'max'], // 4
59-
[Statistical\Minimum::class, 'min'], // 5
60-
[Operations::class, 'product'], // 6
61-
[Statistical\StandardDeviations::class, 'STDEV'], // 7
62-
[Statistical\StandardDeviations::class, 'STDEVP'], // 8
63-
[Sum::class, 'sumIgnoringStrings'], // 9
64-
[Statistical\Variances::class, 'VAR'], // 10
65-
[Statistical\Variances::class, 'VARP'], // 11
55+
1 => [Statistical\Averages::class, 'average'], // 1 and 101
56+
[Statistical\Counts::class, 'COUNT'], // 2 and 102
57+
[Statistical\Counts::class, 'COUNTA'], // 3 and 103
58+
[Statistical\Maximum::class, 'max'], // 4 and 104
59+
[Statistical\Minimum::class, 'min'], // 5 and 105
60+
[Operations::class, 'product'], // 6 and 106
61+
[Statistical\StandardDeviations::class, 'STDEV'], // 7 and 107
62+
[Statistical\StandardDeviations::class, 'STDEVP'], // 8 and 108
63+
[Sum::class, 'sumIgnoringStrings'], // 9 and 109
64+
[Statistical\Variances::class, 'VAR'], // 10 and 110
65+
[Statistical\Variances::class, 'VARP'], // 111 and 111
6666
];
6767

6868
/**

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/RandBetweenTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class RandBetweenTest extends AllSetupTeardown
68
{
79
/**
@@ -43,4 +45,30 @@ public function providerRANDBETWEEN(): array
4345
{
4446
return require 'tests/data/Calculation/MathTrig/RANDBETWEEN.php';
4547
}
48+
49+
/**
50+
* @dataProvider providerRandBetweenArray
51+
*/
52+
public function testRandBetweenArray(
53+
int $expectedRows,
54+
int $expectedColumns,
55+
string $argument1,
56+
string $argument2
57+
): void {
58+
$calculation = Calculation::getInstance();
59+
60+
$formula = "=RandBetween({$argument1}, {$argument2})";
61+
$result = $calculation->_calculateFormulaValue($formula);
62+
self::assertIsArray($result);
63+
self::assertCount($expectedRows, $result);
64+
self::assertIsArray($result[0]);
65+
self::assertCount($expectedColumns, $result[0]);
66+
}
67+
68+
public function providerRandBetweenArray(): array
69+
{
70+
return [
71+
'row/column vectors' => [2, 2, '{1, 10}', '{10; 100}'],
72+
];
73+
}
4674
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SeriesSumTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
56
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
67

78
class SeriesSumTest extends AllSetupTeardown
@@ -43,4 +44,25 @@ public function providerSERIESSUM(): array
4344
{
4445
return require 'tests/data/Calculation/MathTrig/SERIESSUM.php';
4546
}
47+
48+
/**
49+
* @dataProvider providerSeriesSumArray
50+
*/
51+
public function testSeriesSumArray(array $expectedResult, string $x, string $n, string $m, string $values): void
52+
{
53+
$calculation = Calculation::getInstance();
54+
55+
$formula = "=SERIESSUM({$x}, {$n}, {$m}, {$values})";
56+
$result = $calculation->_calculateFormulaValue($formula);
57+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
58+
}
59+
60+
public function providerSeriesSumArray(): array
61+
{
62+
return [
63+
'row vector #1' => [[[3780, 756]], '5', '{1, 0}', '1', '{1, 1, 0, 1, 1}'],
64+
'column vector #1' => [[[54], [3780]], '{2; 5}', '1', '1', '{1, 1, 0, 1, 1}'],
65+
'matrix #1' => [[[54, 27], [3780, 756]], '{2; 5}', '{1, 0}', '1', '{1, 1, 0, 1, 1}'],
66+
];
67+
}
4668
}

0 commit comments

Comments
 (0)