public function testDouble2bipolar() { // test a 1x4 $doubleData1 = [1, -1, 1, -1]; $checkData1 = [true, false, true, false]; $result1 = BiPolarUtil\double2bipolar($doubleData1); for ($i = 0; $i < count($checkData1); ++$i) { $this->assertEquals($checkData1[$i], $result1[$i]); } // test a 2x2 $doubleData2 = [[1, -1], [-1, 1]]; $checkData2 = [[true, false], [false, true]]; $result2 = BiPolarUtil\double2bipolar($doubleData2); for ($r = 0; $r < count($doubleData2); ++$r) { for ($c = 0; $c < count($doubleData2[0]); ++$c) { $this->assertEquals($result2[$r][$c], $checkData2[$r][$c]); } } }
/** * Set the specified index of this object as a boolean. * This value will be * converted into bipolar. * * @param int $index * The index to set. * @param boolean $value * The value to set. */ public function setData($index, $value) { if (is_bool($value)) { $this->data[$index] = $value; } else { if (is_number($value)) { $this->data[$index] = BiPolarUtil\double2bipolar($value); } else { throw new MLDataError("Unsupported value type, must be a double or a boolean."); } } }
/** * Note: for Hopfield networks, you will usually want to call the "run" * method to compute the output. * * This method can be used to copy the input data to the current state. A * single iteration is then run, and the new current state is returned. * * @param * MLData input * The input pattern. * @return MLData The new current state. */ public function compute(MLData $input) { $result = new BiPolarNeuralData($input->size()); EngineArray\arrayCopy($input->getData(), $this->getCurrentState()->getData()); $this->run(); for ($i = 0; $i < $this->getCurrentState()->size(); ++$i) { $result->setData($i, BiPolarUtil\double2bipolar($this->getCurrentState()->getData($i))); } EngineArray\arrayCopy($this->getCurrentState()->getData(), $result->getData()); return $result; }