mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-09 13:38:41 +00:00
Further matrix implementation
This commit is contained in:
parent
c2eefd10cd
commit
d57c626eaf
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace phpOMS\Math;
|
namespace phpOMS\Math\Matrix;
|
||||||
|
|
||||||
class IdentityMatrix extends Matrix {
|
class IdentityMatrix extends Matrix {
|
||||||
public function __constrcut(int $n)
|
public function __constrcut(int $n)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class Matrix implements ArrayAccess, Iterator
|
||||||
private $n = 0;
|
private $n = 0;
|
||||||
private $m = 0;
|
private $m = 0;
|
||||||
|
|
||||||
public function __construct(int $n, int $m)
|
public function __construct(int $m, int $n = 1)
|
||||||
{
|
{
|
||||||
$this->n = $n;
|
$this->n = $n;
|
||||||
$this->m = $m;
|
$this->m = $m;
|
||||||
|
|
@ -21,24 +21,143 @@ class Matrix implements ArrayAccess, Iterator
|
||||||
|
|
||||||
public function setMatrix(array $matrix)
|
public function setMatrix(array $matrix)
|
||||||
{
|
{
|
||||||
|
if($this->m !== count($matrix) || $this->n !== count($matrix[0])) {
|
||||||
|
throw new \Exception('Dimension');
|
||||||
|
}
|
||||||
|
|
||||||
$this->matrix = $matrix;
|
$this->matrix = $matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(int $n, int $m, $value)
|
public function getMatrix() : array
|
||||||
{
|
{
|
||||||
$this->matrix[$n][$m] = $value;
|
return $this->matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(int $n, int $m)
|
public function set(int $m, int $n, $value)
|
||||||
{
|
{
|
||||||
return $this->matrix[$n][$m];
|
$this->matrix[$m][$n] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(int $m, int $n)
|
||||||
|
{
|
||||||
|
return $this->matrix[$m][$n];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function transpose() : Matrix
|
public function transpose() : Matrix
|
||||||
{
|
{
|
||||||
$matrix = new Matrix($this->m, $this->n);
|
$matrix = new Matrix($this->n, $this->m);
|
||||||
$matrix->setMatrix(array_map(null, ...$matrix));
|
$matrix->setMatrix(array_map(null, ...$matrix));
|
||||||
|
|
||||||
return $matrix;
|
return $matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function mult($value) : Matrix
|
||||||
|
{
|
||||||
|
if($value instanceOf Matrix) {
|
||||||
|
return $this->multMatrix($value);
|
||||||
|
} elseif(is_scalar($value)) {
|
||||||
|
return $this->multScalar($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function multMatrix(Matrix $matrix) : Matrix
|
||||||
|
{
|
||||||
|
$nDim = $matrix->getN();
|
||||||
|
$mDim = $matrix->getM();
|
||||||
|
|
||||||
|
if($this->n !== $mDim) {
|
||||||
|
throw new \Exception('Dimension');
|
||||||
|
}
|
||||||
|
|
||||||
|
$matrixArr = $matrix->getMatrix();
|
||||||
|
$newMatrix = new Matrix($this->m, $nDim);
|
||||||
|
$newMatrixArr = $newMatrix->getMatrix();
|
||||||
|
|
||||||
|
for($i = 0; $i < $this->m; $i++) { // Row of $this
|
||||||
|
for($c = 0; $c < $nDim; $c++) { // Column of $matrix
|
||||||
|
$temp = 0;
|
||||||
|
|
||||||
|
for($j = 0; $j < $mDim; $i++) { // Row of $matrix
|
||||||
|
$temp += $this->matrix[$i][$j] * $matrixArr[$j][$c];
|
||||||
|
}
|
||||||
|
|
||||||
|
$newMatrixArr[$i][$c] = $temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newMatrix->setMatrix($newMatrixArr);
|
||||||
|
|
||||||
|
return $newMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function multScalar($value) : Matrix
|
||||||
|
{
|
||||||
|
$newMatrixArr = $this->matrix;
|
||||||
|
|
||||||
|
foreach($newMatrixArr as $i => $vector) {
|
||||||
|
foreach($vector as $j => $value) {
|
||||||
|
$newMatrixArr[$i][$j] *= $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newMatrix = new Matrix($this->m, $this->n);
|
||||||
|
$newMatrix->setMatrix($newMatrixArr);
|
||||||
|
|
||||||
|
return $newMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add($value) : Matrix
|
||||||
|
{
|
||||||
|
if($value instanceOf Matrix) {
|
||||||
|
return $this->addMatrix($value);
|
||||||
|
} elseif(is_scalar($value)) {
|
||||||
|
return $this->addScalar($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sub($value) : Matrix
|
||||||
|
{
|
||||||
|
if($value instanceOf Matrix) {
|
||||||
|
return $this->add($this->multMatrix(-1));
|
||||||
|
} elseif(is_scalar($value)) {
|
||||||
|
return $this->addScalar(-$value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addMatrix(Matrix $value) : Matrix
|
||||||
|
{
|
||||||
|
if($this->m !== $value->getM() || $this->n !== $value->getN()) {
|
||||||
|
throw new \Exception('Dimension');
|
||||||
|
}
|
||||||
|
|
||||||
|
$matrixArr = $value->getMatrix();
|
||||||
|
$newMatrixArr = $this->matrix;
|
||||||
|
|
||||||
|
foreach($newMatrixArr as $i => $vector) {
|
||||||
|
foreach($vector as $j => $value) {
|
||||||
|
$newMatrixArr[$i][$j] += $matrixArr[$i][$j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newMatrix = new Matrix($this->m, $this->n);
|
||||||
|
$newMatrix->setMatrix($newMatrixArr);
|
||||||
|
|
||||||
|
return $newMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addScalar($value) : Matrix
|
||||||
|
{
|
||||||
|
$newMatrixArr = $this->matrix;
|
||||||
|
|
||||||
|
foreach($newMatrixArr as $i => $vector) {
|
||||||
|
foreach($vector as $j => $value) {
|
||||||
|
$newMatrixArr[$i][$j] += $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newMatrix = new Matrix($this->m, $this->n);
|
||||||
|
$newMatrix->setMatrix($newMatrixArr);
|
||||||
|
|
||||||
|
return $newMatrix;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,67 +1,10 @@
|
||||||
abstract class Vector {
|
<?php
|
||||||
private $vector = null;
|
|
||||||
|
|
||||||
public function __construct() {
|
namespace phpOMS\Math\Matrix;
|
||||||
|
|
||||||
}
|
class Vector extends Matrix {
|
||||||
|
public function __constrcut(int $m)
|
||||||
public function setVector($vector) {
|
{
|
||||||
$this->vector = $vector;
|
parent::__construct($m);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setVectorElement($i, $element) {
|
|
||||||
$this->vector[$i] = $element;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addVector() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addScalar() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function subVector() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function subScalar() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function multVector() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function multScalar() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function divVector() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function divScalar() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLength() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function normalize() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getAngular() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function multMatrix() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function divMatrix() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user