public function testBipolar2double() { // test a 1x4 $booleanData1 = [true, false, true, false]; $checkData1 = [1, -1, 1, -1]; $matrix1 = Matrix::createRowMatrix(BiPolarUtil\bipolar2double($booleanData1)); $checkMatrix1 = Matrix::createRowMatrix($checkData1); $this->assertTrue($matrix1->equals($checkMatrix1)); // test a 2x2 $booleanData2 = [[true, false], [false, true]]; $checkData2 = [[1, -1], [-1, 1]]; $matrix2 = Matrix::matrixFromDoubles(BiPolarUtil\bipolar2double($booleanData2)); $checkMatrix2 = Matrix::matrixFromDoubles($checkData2); $this->assertTrue($matrix2->equals($checkMatrix2)); }
/** * 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 testVectorLength() { $vectorData = [1.0, 2.0, 3.0, 4.0]; $vector = Matrix::createRowMatrix($vectorData); $this->assertEquals(5, intval(MatrixMath\vectorLength($vector))); $nonVector = new Matrix(2, 2); try { MatrixMath\vectorLength($nonVector); $this->assertTrue(false); } catch (MatrixError $e) { } }