/** @param array $set [ varname => fraction ] */ function __construct(array $set) { foreach ($set as $var => $coeff) { $set[$var] = Fraction::create($coeff); } ksort($set); $this->set = $set; }
/** * @param string $var * @param array $set * @param Fraction|numeric $b */ function __construct($var, array $set, $b) { parent::__construct($set); $this->var = (string) $var; $this->b = Fraction::create($b); }
/** * @param array $set * @param Fraction|numeric $value */ function __construct(array $set, $value) { parent::__construct($set); $this->value = Fraction::create($value); }
/** * @param array $set * @param int $type * @param Fraction|numeric $limit */ function __construct(array $set, $type, $limit) { parent::__construct($set); $this->type = (int) $type; $this->limit = Fraction::create($limit); }
/** @return Table */ function nextStep() { if ($this->z2 !== NULL && !$this->hasHelperInBasis()) { $this->removeHelpers(); } $newrows = array(); $keycol = $this->getKeyColumn(); $keyrow = $this->getKeyRow(); if ($keycol === NULL || $keyrow === NULL) { return $this; } foreach ($this->rows as $row) { $rowset = array(); if ($row->getVar() === $keyrow->getVar()) { $var = $keycol; $b = $row->getB()->divide($keyrow->get($keycol)); foreach ($row->getSet() as $v => $c) { $rowset[$v] = $c->divide($keyrow->get($keycol)); } } else { $var = $row->getVar(); $set = $row->getSet(); $dvd = $set[$keycol]->multiply(-1)->divide($keyrow->get($keycol)); $b = $dvd->multiply($keyrow->getB())->add($row->getB()); foreach ($row->getSet() as $v => $c) { $rowset[$v] = $dvd->multiply($keyrow->get($v))->add($c); } } $newrows[$var] = array($rowset, $b); } $this->rows = array(); foreach ($newrows as $var => $meta) { $this->addRow(new TableRow($var, $meta[0], $meta[1])); } foreach (array('z', 'z2') as $zvar) { if ($this->{$zvar} === NULL) { continue; } $zcoeffs = array(); $zdvd = $this->{$zvar}->get($keycol)->multiply(-1)->divide($keyrow->get($keycol)); foreach ($this->{$zvar}->getSet() as $v => $c) { $zcoeffs[$v] = $zdvd->multiply($keyrow->get($v))->add($c); } $this->{$zvar} = new TableRow($zvar, $zcoeffs, $zdvd->multiply($keyrow->getB())->add($this->{$zvar}->getB())); } // find solution (if any) if ($this->isSolved()) { if ($this->solution !== FALSE) { $this->solution = array(); foreach ($this->z->getVariableList() as $var) { if (strncmp($var, 'y', 1) === 0) { continue; } foreach ($this->rows as $row) { if ($row->getVar() === $var) { $this->solution[$var] = $row->getB(); continue 2; } } $this->solution[$var] = Fraction::create(0); } ksort($this->solution); } } return $this; }
/** @return Table */ function toTable() { $zcoeffs = array(); foreach ($this->function->getSet() as $var => $coeff) { $zcoeffs[$var] = $coeff->multiply(-1); } $z = new ValueFunc($zcoeffs, 0); $z2b = Fraction::create(0); $z2coeffs = array(); foreach ($this->restrictions as $idx => $r) { foreach ($r->getSet() as $var => $coeff) { if (strncmp($var, 'y', 1) === 0 && $coeff->isEqualTo(1)) { foreach ($r->getSet() as $v => $c) { !isset($z2coeffs[$v]) && ($z2coeffs[$v] = Fraction::create(0)); strncmp($v, 'y', 1) !== 0 && ($z2coeffs[$v] = $z2coeffs[$v]->subtract($c)); } $z2b = $z2b->subtract($r->getLimit()); } } } $z2 = count($z2coeffs) ? new ValueFunc($z2coeffs, $z2b) : NULL; $table = new Table($z, $z2); foreach ($this->basismap as $var => $idx) { $table->addRow(new TableRow($var, $this->restrictions[$idx]->getSet(), $this->restrictions[$idx]->getLimit())); } return $table; }