Skip to content

Commit a5eb64c

Browse files
author
Mark Baker
authored
Matrix (#790)
* Refactor Excel Matrix functions to use external Matrix library rather than JAMA
1 parent b6a73ae commit a5eb64c

File tree

11 files changed

+267
-812
lines changed

11 files changed

+267
-812
lines changed

.php_cs.dist

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

33
$finder = PhpCsFixer\Finder::create()
4-
->exclude('vendor')
4+
->exclude(['vendor', 'tests/data/Calculation'])
55
->in('samples')
66
->in('src')
7-
->in('tests');
7+
->in('tests/PhpSpreadsheetTests')
8+
;
89

910
return PhpCsFixer\Config::create()
1011
->setRiskyAllowed(true)

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Refactored Matrix Functions to use external Matrix library
13+
14+
1015
## [1.5.2] - 2018-11-25
1116

1217
### Security

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
"ext-zip": "*",
5454
"ext-zlib": "*",
5555
"psr/simple-cache": "^1.0",
56-
"markbaker/complex": "^1.4.1"
56+
"markbaker/complex": "^1.4",
57+
"markbaker/matrix": "^1.1"
5758
},
5859
"require-dev": {
5960
"tecnickcom/tcpdf": "^6.2",
6061
"phpunit/phpunit": "^5.7",
62+
"doctrine/instantiator": "^1.0.0",
6163
"dompdf/dompdf": "^0.8.0",
6264
"mpdf/mpdf": "^7.0.0",
6365
"jpgraph/jpgraph": "^4.0",

composer.lock

Lines changed: 72 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/references/function-list-by-category.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ DATE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATE
3737
DATEDIF | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEDIF
3838
DATEVALUE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEVALUE
3939
DAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYOFMONTH
40+
DAYS | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS
4041
DAYS360 | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS360
4142
EDATE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::EDATE
4243
EOMONTH | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::EOMONTH
@@ -45,6 +46,7 @@ ISOWEEKNUM | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::ISOWEEKNUM
4546
MINUTE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::MINUTE
4647
MONTH | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::MONTHOFYEAR
4748
NETWORKDAYS | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::NETWORKDAYS
49+
NETWORKDAYS.INTL | **Not yet Implemented**
4850
NOW | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATETIMENOW
4951
SECOND | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::SECOND
5052
TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::TIME
@@ -53,6 +55,7 @@ TODAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATENOW
5355
WEEKDAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKDAY
5456
WEEKNUM | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKNUM
5557
WORKDAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WORKDAY
58+
WORKDAY.INTL | **Not yet Implemented**
5659
YEAR | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::YEAR
5760
YEARFRAC | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::YEARFRAC
5861

@@ -66,7 +69,12 @@ BESSELK | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BESSELK
6669
BESSELY | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BESSELY
6770
BIN2DEC | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTODEC
6871
BIN2HEX | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOHEX
69-
BIN2OCT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOOCT
72+
BIN2OCT | \PhpOffice\PhpSpreadsheet\Calculation\Engineeri
73+
BITAND | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITAND
74+
BITLSHIFT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITLSHIFT
75+
BITOR | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITOR
76+
BITRSHIFT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITRSHIFT
77+
BITXOR | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITXOR
7078
COMPLEX | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::COMPLEX
7179
CONVERT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::CONVERTUOM
7280
DEC2BIN | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::DECTOBIN

docs/references/function-list-by-name.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ BIN2DEC | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet
4343
BIN2HEX | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOHEX
4444
BIN2OCT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOOCT
4545
BINOMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::BINOMDIST
46+
BITAND | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITAND
47+
BITLSHIFT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITLSHIFT
48+
BITOR | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITOR
49+
BITRSHIFT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITRSHIFT
50+
BITXOR | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITXOR
4651

4752
## C
4853

@@ -104,6 +109,7 @@ DATEDIF | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet
104109
DATEVALUE | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEVALUE
105110
DAVERAGE | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database::DAVERAGE
106111
DAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYOFMONTH
112+
DAYS | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS
107113
DAYS360 | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS360
108114
DB | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial::DB
109115
DCOUNT | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database::DCOUNT
@@ -317,6 +323,7 @@ N | CATEGORY_INFORMATION | \PhpOffice\PhpSpreadsheet
317323
NA | CATEGORY_INFORMATION | \PhpOffice\PhpSpreadsheet\Calculation\Functions::NA
318324
NEGBINOMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NEGBINOMDIST
319325
NETWORKDAYS | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::NETWORKDAYS
326+
NETWORKDAYS.INTL | CATEGORY_DATE_AND_TIME | **Not yet Implemented**
320327
NOMINAL | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial::NOMINAL
321328
NORMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NORMDIST
322329
NORMINV | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NORMINV
@@ -494,6 +501,7 @@ WEEKDAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet
494501
WEEKNUM | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKNUM
495502
WEIBULL | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::WEIBULL
496503
WORKDAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WORKDAY
504+
WORKDAY.INTL | CATEGORY_DATE_AND_TIME | **Not yet Implemented**
497505

498506
## X
499507

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation;
44

5-
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6-
use PhpOffice\PhpSpreadsheet\Shared\JAMA\Matrix;
5+
use Matrix\Exception as MatrixException;
6+
use Matrix\Matrix;
77

88
class MathTrig
99
{
@@ -498,23 +498,23 @@ public static function MDETERM($matrixValues)
498498
if ((is_string($matrixCell)) || ($matrixCell === null)) {
499499
return Functions::VALUE();
500500
}
501-
$matrixData[$column][$row] = $matrixCell;
501+
$matrixData[$row][$column] = $matrixCell;
502502
++$column;
503503
}
504504
if ($column > $maxColumn) {
505505
$maxColumn = $column;
506506
}
507507
++$row;
508508
}
509-
if ($row != $maxColumn) {
509+
510+
$matrix = new Matrix($matrixData);
511+
if (!$matrix->isSquare()) {
510512
return Functions::VALUE();
511513
}
512514

513515
try {
514-
$matrix = new Matrix($matrixData);
515-
516-
return $matrix->det();
517-
} catch (PhpSpreadsheetException $ex) {
516+
return $matrix->determinant();
517+
} catch (MatrixException $ex) {
518518
return Functions::VALUE();
519519
}
520520
}
@@ -550,25 +550,27 @@ public static function MINVERSE($matrixValues)
550550
if ((is_string($matrixCell)) || ($matrixCell === null)) {
551551
return Functions::VALUE();
552552
}
553-
$matrixData[$column][$row] = $matrixCell;
553+
$matrixData[$row][$column] = $matrixCell;
554554
++$column;
555555
}
556556
if ($column > $maxColumn) {
557557
$maxColumn = $column;
558558
}
559559
++$row;
560560
}
561-
foreach ($matrixValues as $matrixRow) {
562-
if (count($matrixRow) != $maxColumn) {
563-
return Functions::VALUE();
564-
}
561+
562+
$matrix = new Matrix($matrixData);
563+
if (!$matrix->isSquare()) {
564+
return Functions::VALUE();
565565
}
566566

567-
try {
568-
$matrix = new Matrix($matrixData);
567+
if ($matrix->determinant() == 0.0) {
568+
return Functions::NAN();
569+
}
569570

570-
return $matrix->inverse()->getArray();
571-
} catch (PhpSpreadsheetException $ex) {
571+
try {
572+
return $matrix->inverse()->toArray();
573+
} catch (MatrixException $ex) {
572574
return Functions::VALUE();
573575
}
574576
}
@@ -629,8 +631,8 @@ public static function MMULT($matrixData1, $matrixData2)
629631
return Functions::VALUE();
630632
}
631633

632-
return $matrixA->times($matrixB)->getArray();
633-
} catch (PhpSpreadsheetException $ex) {
634+
return $matrixA->multiply($matrixB)->toArray();
635+
} catch (MatrixException $ex) {
634636
return Functions::VALUE();
635637
}
636638
}

tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,8 @@ public function providerMDETERM()
281281
*/
282282
public function testMINVERSE($expectedResult, ...$args)
283283
{
284-
$this->markTestIncomplete('TODO: This test should be fixed');
285-
286284
$result = MathTrig::MINVERSE(...$args);
287-
self::assertEquals($expectedResult, $result, null, 1E-12);
285+
self::assertEquals($expectedResult, $result, null, 1E-8);
288286
}
289287

290288
public function providerMINVERSE()
@@ -299,10 +297,8 @@ public function providerMINVERSE()
299297
*/
300298
public function testMMULT($expectedResult, ...$args)
301299
{
302-
$this->markTestIncomplete('TODO: This test should be fixed');
303-
304300
$result = MathTrig::MMULT(...$args);
305-
self::assertEquals($expectedResult, $result, null, 1E-12);
301+
self::assertEquals($expectedResult, $result, null, 1E-8);
306302
}
307303

308304
public function providerMMULT()

0 commit comments

Comments
 (0)