public function testMultiply() { $matrixData1 = [[1, 4], [2, 5], [3, 6]]; $matrixData2 = [[7, 8, 9], [10, 11, 12]]; $matrixData3 = [[47, 52, 57], [64, 71, 78], [81, 90, 99]]; $matrix1 = Matrix::matrixFromDoubles($matrixData1); $matrix2 = Matrix::matrixFromDoubles($matrixData2); $matrix3 = Matrix::matrixFromDoubles($matrixData3); $result = MatrixMath\multiply($matrix1, $matrix2); $this->assertTrue($result->equals($matrix3)); }
/** * Train the neural network for the specified pattern. * The neural network * can be trained for more than one pattern. To do this simply call the * train method more than once. * * @param * MLData pattern * The pattern to train for. */ public function addPattern(MLData $pattern) { if ($pattern->size() != $this->getNeuronCount()) { throw new NeuralNetworkError("Network with " + $this->getNeuronCount() + " neurons, cannot learn a pattern of size " + $pattern->size()); } // Create a row matrix from the input, convert boolean to bipolar $m2 = Matrix::createRowMatrix($pattern->getData()); // Transpose the matrix and multiply by the original input matrix $m1 = MatrixMath\transpose($m2); $m3 = MatrixMath\multiply($m1, $m2); // matrix 3 should be square by now, so create an identity // matrix of the same size. $identity = MatrixMath\identity($m3->getRows()); // subtract the identity matrix $m4 = MatrixMath\subtract($m3, $identity); // now add the calculated matrix, for this pattern, to the // existing weight matrix. $this->convertHopfieldMatrix($m4); }
public function testMatrixMultiply() { $a = [[1, 0, 2], [-1, 3, 1]]; $b = [[3, 1], [2, 1], [1, 0]]; $c = [[5, 1], [4, 2]]; $matrixA = Matrix::matrixFromDoubles($a); $matrixB = Matrix::matrixFromDoubles($b); $matrixC = Matrix::matrixFromDoubles($c); $result = clone $matrixA; $result = MatrixMath\multiply($matrixA, $matrixB); $this->assertTrue($result->equals($matrixC)); $a2 = [[1, 2, 3, 4], [5, 6, 7, 8]]; $b2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]; $c2 = [[70, 80, 90], [158, 184, 210]]; $matrixA = Matrix::matrixFromDoubles($a2); $matrixB = Matrix::matrixFromDoubles($b2); $matrixC = Matrix::matrixFromDoubles($c2); $result = MatrixMath\multiply($matrixA, $matrixB); $this->assertTrue($result->equals($matrixC)); $result = clone $matrixB; try { MatrixMath\multiply($matrixB, $matrixA); $this->assertTrue(false); } catch (MatrixError $e) { } }