summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2006-08-16 13:12:44 +0000
committerKore Nordmann <github@kore-nordmann.de>2006-08-16 13:12:44 +0000
commit441e0054ad7b4f38d4596cf3ceda08ec00cc2127 (patch)
tree108710ffe5523dabdf735e538693acb68478288d
parentbb028918891c1840d200530a0cb458db9a07cf95 (diff)
downloadzetacomponents-graph-441e0054ad7b4f38d4596cf3ceda08ec00cc2127.zip
zetacomponents-graph-441e0054ad7b4f38d4596cf3ceda08ec00cc2127.tar.gz
- Added transpose method to matrix class
-rw-r--r--src/math/matrix.php25
-rw-r--r--tests/matrix_test.php18
2 files changed, 43 insertions, 0 deletions
diff --git a/src/math/matrix.php b/src/math/matrix.php
index dae2f64..f64d7e4 100644
--- a/src/math/matrix.php
+++ b/src/math/matrix.php
@@ -228,6 +228,31 @@ class ezcGraphMatrix
}
}
+ /**
+ * Transpose matrix
+ *
+ * @return ezcGraphMatrix Transposed matrix
+ */
+ public function transpose()
+ {
+ $matrix = clone $this;
+
+ $this->rows = $matrix->columns();
+ $this->columns = $matrix->rows();
+
+ $this->matrix = array();
+
+ for ( $i = 0; $i < $this->columns; ++$i )
+ {
+ for ( $j = 0; $j < $this->rows; ++$j )
+ {
+ $this->matrix[$i][$j] = $matrix->get( $j, $i );
+ }
+ }
+
+ return $this;
+ }
+
/**
* Multiplies two matrices
*
diff --git a/tests/matrix_test.php b/tests/matrix_test.php
index 0afd631..79e8afc 100644
--- a/tests/matrix_test.php
+++ b/tests/matrix_test.php
@@ -258,6 +258,24 @@ class ezcGraphMatrixTest extends ezcTestCase
$this->fail( 'Expected ezcGraphMatrixInvalidDimensionsException.' );
}
+
+ public function testTransposeMatrix()
+ {
+ $matrix = new ezcGraphMatrix( 2, 3, array(
+ array( 1, 2, 3 ),
+ array( 4, 5, 6 ),
+ ) );
+ $matrix->transpose();
+
+ $this->assertEquals(
+ array(
+ array( 1, 4 ),
+ array( 2, 5 ),
+ array( 3, 6 ),
+ ),
+ $this->getAttribute( $matrix, 'matrix' )
+ );
+ }
}
?>
OpenPOWER on IntegriCloud