Skip to content

Commit

Permalink
Matrix (PHPOffice#790)
Browse files Browse the repository at this point in the history
* Refactor Excel Matrix functions to use external Matrix library rather than JAMA
  • Loading branch information
Mark Baker authored and guillaume-ro-fr committed Jun 12, 2019
1 parent 475cd10 commit 05ba447
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 812 deletions.
5 changes: 3 additions & 2 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude(['vendor', 'tests/data/Calculation'])
->in('samples')
->in('src')
->in('tests');
->in('tests/PhpSpreadsheetTests')
;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]

### Added

- Refactored Matrix Functions to use external Matrix library


## [1.5.2] - 2018-11-25

### Security
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@
"ext-zip": "*",
"ext-zlib": "*",
"psr/simple-cache": "^1.0",
"markbaker/complex": "^1.4.1"
"markbaker/complex": "^1.4",
"markbaker/matrix": "^1.1"
},
"require-dev": {
"tecnickcom/tcpdf": "^6.2",
"phpunit/phpunit": "^5.7",
"doctrine/instantiator": "^1.0.0",
"dompdf/dompdf": "^0.8.0",
"mpdf/mpdf": "^7.0.0",
"jpgraph/jpgraph": "^4.0",
Expand Down
74 changes: 72 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion docs/references/function-list-by-category.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ DATE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATE
DATEDIF | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEDIF
DATEVALUE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEVALUE
DAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYOFMONTH
DAYS | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS
DAYS360 | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS360
EDATE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::EDATE
EOMONTH | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::EOMONTH
Expand All @@ -45,6 +46,7 @@ ISOWEEKNUM | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::ISOWEEKNUM
MINUTE | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::MINUTE
MONTH | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::MONTHOFYEAR
NETWORKDAYS | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::NETWORKDAYS
NETWORKDAYS.INTL | **Not yet Implemented**
NOW | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATETIMENOW
SECOND | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::SECOND
TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::TIME
Expand All @@ -53,6 +55,7 @@ TODAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATENOW
WEEKDAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKDAY
WEEKNUM | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKNUM
WORKDAY | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WORKDAY
WORKDAY.INTL | **Not yet Implemented**
YEAR | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::YEAR
YEARFRAC | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::YEARFRAC

Expand All @@ -66,7 +69,12 @@ BESSELK | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BESSELK
BESSELY | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BESSELY
BIN2DEC | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTODEC
BIN2HEX | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOHEX
BIN2OCT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOOCT
BIN2OCT | \PhpOffice\PhpSpreadsheet\Calculation\Engineeri
BITAND | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITAND
BITLSHIFT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITLSHIFT
BITOR | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITOR
BITRSHIFT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITRSHIFT
BITXOR | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITXOR
COMPLEX | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::COMPLEX
CONVERT | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::CONVERTUOM
DEC2BIN | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::DECTOBIN
Expand Down
8 changes: 8 additions & 0 deletions docs/references/function-list-by-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ BIN2DEC | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet
BIN2HEX | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOHEX
BIN2OCT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BINTOOCT
BINOMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::BINOMDIST
BITAND | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITAND
BITLSHIFT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITLSHIFT
BITOR | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITOR
BITRSHIFT | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITRSHIFT
BITXOR | CATEGORY_ENGINEERING | \PhpOffice\PhpSpreadsheet\Calculation\Engineering::BITXOR

## C

Expand Down Expand Up @@ -104,6 +109,7 @@ DATEDIF | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet
DATEVALUE | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEVALUE
DAVERAGE | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database::DAVERAGE
DAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYOFMONTH
DAYS | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS
DAYS360 | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DAYS360
DB | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial::DB
DCOUNT | CATEGORY_DATABASE | \PhpOffice\PhpSpreadsheet\Calculation\Database::DCOUNT
Expand Down Expand Up @@ -317,6 +323,7 @@ N | CATEGORY_INFORMATION | \PhpOffice\PhpSpreadsheet
NA | CATEGORY_INFORMATION | \PhpOffice\PhpSpreadsheet\Calculation\Functions::NA
NEGBINOMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NEGBINOMDIST
NETWORKDAYS | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::NETWORKDAYS
NETWORKDAYS.INTL | CATEGORY_DATE_AND_TIME | **Not yet Implemented**
NOMINAL | CATEGORY_FINANCIAL | \PhpOffice\PhpSpreadsheet\Calculation\Financial::NOMINAL
NORMDIST | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NORMDIST
NORMINV | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::NORMINV
Expand Down Expand Up @@ -494,6 +501,7 @@ WEEKDAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet
WEEKNUM | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WEEKNUM
WEIBULL | CATEGORY_STATISTICAL | \PhpOffice\PhpSpreadsheet\Calculation\Statistical::WEIBULL
WORKDAY | CATEGORY_DATE_AND_TIME | \PhpOffice\PhpSpreadsheet\Calculation\DateTime::WORKDAY
WORKDAY.INTL | CATEGORY_DATE_AND_TIME | **Not yet Implemented**

## X

Expand Down
40 changes: 21 additions & 19 deletions src/PhpSpreadsheet/Calculation/MathTrig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Calculation;

use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Shared\JAMA\Matrix;
use Matrix\Exception as MatrixException;
use Matrix\Matrix;

class MathTrig
{
Expand Down Expand Up @@ -498,23 +498,23 @@ public static function MDETERM($matrixValues)
if ((is_string($matrixCell)) || ($matrixCell === null)) {
return Functions::VALUE();
}
$matrixData[$column][$row] = $matrixCell;
$matrixData[$row][$column] = $matrixCell;
++$column;
}
if ($column > $maxColumn) {
$maxColumn = $column;
}
++$row;
}
if ($row != $maxColumn) {

$matrix = new Matrix($matrixData);
if (!$matrix->isSquare()) {
return Functions::VALUE();
}

try {
$matrix = new Matrix($matrixData);

return $matrix->det();
} catch (PhpSpreadsheetException $ex) {
return $matrix->determinant();
} catch (MatrixException $ex) {
return Functions::VALUE();
}
}
Expand Down Expand Up @@ -550,25 +550,27 @@ public static function MINVERSE($matrixValues)
if ((is_string($matrixCell)) || ($matrixCell === null)) {
return Functions::VALUE();
}
$matrixData[$column][$row] = $matrixCell;
$matrixData[$row][$column] = $matrixCell;
++$column;
}
if ($column > $maxColumn) {
$maxColumn = $column;
}
++$row;
}
foreach ($matrixValues as $matrixRow) {
if (count($matrixRow) != $maxColumn) {
return Functions::VALUE();
}

$matrix = new Matrix($matrixData);
if (!$matrix->isSquare()) {
return Functions::VALUE();
}

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

return $matrix->inverse()->getArray();
} catch (PhpSpreadsheetException $ex) {
try {
return $matrix->inverse()->toArray();
} catch (MatrixException $ex) {
return Functions::VALUE();
}
}
Expand Down Expand Up @@ -629,8 +631,8 @@ public static function MMULT($matrixData1, $matrixData2)
return Functions::VALUE();
}

return $matrixA->times($matrixB)->getArray();
} catch (PhpSpreadsheetException $ex) {
return $matrixA->multiply($matrixB)->toArray();
} catch (MatrixException $ex) {
return Functions::VALUE();
}
}
Expand Down
8 changes: 2 additions & 6 deletions tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,8 @@ public function providerMDETERM()
*/
public function testMINVERSE($expectedResult, ...$args)
{
$this->markTestIncomplete('TODO: This test should be fixed');

$result = MathTrig::MINVERSE(...$args);
self::assertEquals($expectedResult, $result, null, 1E-12);
self::assertEquals($expectedResult, $result, null, 1E-8);
}

public function providerMINVERSE()
Expand All @@ -299,10 +297,8 @@ public function providerMINVERSE()
*/
public function testMMULT($expectedResult, ...$args)
{
$this->markTestIncomplete('TODO: This test should be fixed');

$result = MathTrig::MMULT(...$args);
self::assertEquals($expectedResult, $result, null, 1E-12);
self::assertEquals($expectedResult, $result, null, 1E-8);
}

public function providerMMULT()
Expand Down
Loading

0 comments on commit 05ba447

Please sign in to comment.