Test fixes

This commit is contained in:
Dennis Eichhorn 2018-10-07 20:26:51 +02:00
parent b0c58616ff
commit 4facb82bb3
4 changed files with 148 additions and 15 deletions

View File

@ -83,7 +83,7 @@ final class QRDecomposition
$nrm = Triangle::getHypot($nrm, $this->QR[$i][$k]);
}
if ($nrm != 0.0) {
if ($nrm != 0) {
// Form k-th Householder vector.
if ($this->QR[$k][$k] < 0) {
$nrm = -$nrm;

View File

@ -24,4 +24,109 @@ namespace phpOMS\Math\Matrix;
*/
final class SingularValueDecomposition
{
/**
* U matrix.
*
* @var array[]
* @since 1.0.0
*/
private $U = [];
/**
* V matrix.
*
* @var array[]
* @since 1.0.0
*/
private $V = [];
/**
* Singular values.
*
* @var array
* @since 1.0.0
*/
private $S = [];
/**
* Dimension m
*
* @var int
* @since 1.0.0
*/
private $m = 0;
/**
* Dimension n
*
* @var int
* @since 1.0.0
*/
private $n = 0;
public function getU() : Matrix
{
$matrix = new Matrix();
$matrix->setMatrix($this->U);
return $matrix;
}
public function getV() : Matrix
{
$matrix = new Matrix();
$matrix->setMatrix($this->V);
return $matrix;
}
public function getS() : Matrix
{
$S = [[]];
for ($i = 0; $i < $this->n; ++$i) {
for ($j = 0; $j < $this->n; ++$j) {
$S[$i][$j] = 0.0;
}
$S[$i][$i] = $this->s[$i];
}
$matrix = new Matrix();
$matrix->setMatrix($this->V);
return $matrix;
}
public function getSingularValues() : Vector
{
$vector = new Vector();
$vector->setMatrix($this->S);
return $vector;
}
public function norm2() : float
{
return $this->S[0];
}
public function cond() : float
{
return $this->S[0] / $this->S[\min($this->m, $this->n) - 1];
}
public function rank() : int
{
$eps = 0.00001;
$tol = \max($this->m, $this->n) * $this->S[0] * $eps;
$r = 0;
$length = \count($this->S);
for ($i = 0; $i < $length; ++$i) {
if ($this->S[$i] > $tol) {
++$r;
}
}
return $r;
}
}

View File

@ -19,7 +19,7 @@ use phpOMS\Math\Matrix\QRDecomposition;
class QRDecompositionTest extends \PHPUnit\Framework\TestCase
{
/**public function testDecomposition()
public function testDecomposition()
{
$A = new Matrix();
$A->setMatrix([
@ -32,24 +32,26 @@ class QRDecompositionTest extends \PHPUnit\Framework\TestCase
self::assertTrue($QR->isFullRank());
self::assertEquals([
[-6 / 7, 69 / 175, 58 / 175],
[-3 / 7, -158 / 175, -6 / 175],
[2 / 7, -6 / 35, 33 / 35],
], $QR->getQ()->toArray(), '', 0.2);
self::assertEquals([
[-14, -21, 14],
[0, -175, 70],
[0, 0, -35],
], $QR->getR()->toArray(), '', 0.2);
self::assertEquals($A->toArray(), $QR->getQ()->mult($QR->getR()), '', 0.2);
self::assertEquals([
[12, -69, -58 / 5],
[6, 158, 6 / 5],
[-4, 30, -33],
], $QR->getH()->toArray(), '', 0.2);
self::assertEquals([
[6 / 7, -69 / 175, -58 / 175],
[3 / 7, 158 / 175, 6 / 175],
[-2 / 7, 6 / 35, -33 / 35],
], $QR->getQ()->toArray(), '', 0.2);
self::assertEquals([
[14, 21, -14],
[0, 175, -70],
[0, 0, 35],
], $QR->getR()->toArray(), '', 0.2);
}*/
}
public function testSolve()
{

View File

@ -0,0 +1,26 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Math\Matrix;
use phpOMS\Math\Matrix\Matrix;
use phpOMS\Math\Matrix\Vector;
use phpOMS\Math\Matrix\SingularValueDecomposition;
class SingularValueDecompositionTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
{
self::markTestIncomplete();
}
}