util.php

<?php
declare(strict_types=1);
/**
 * @param array<int, mixed> $c
 * @param array<int, array<int, mixed>> $a
 * @param array<int, string> $lge
 * @param array<int, mixed> $b
 */
function mostra_equazioni(string $minmax, int $numVariables, int $numConstraints, array $c, mixed $d, array $a, array $lge, array $b, string $intera): string
	/* mostra il problema come e' stato immesso */ // Copiata da matrice.display_equations()
{
	$content = '
        <strong>' . $minmax . ' z = ';

	// 1ma riga: c^t x + ...
	if (!isset($c[1]))
		echo "Errore: non esiste c[1]\n";
	elseif ($c[1]->value() == 1) {
		$content .= sprintf(" x<sub>1</sub>");
	} elseif ($c[1]->num() == 0) {
		// $content .= sprintf("&nbsp; &nbsp;");
	} elseif ($c[1]->value() == -1) {
		$content .= sprintf(" - x<sub>1</sub>");
	} else {
		$content .= sprintf(" %s x<sub>1</sub>", $c[1]->fractoa());
	}
	for ($j = 2; $j < $numVariables + 1; $j++) {
		if (!isset($c[$j])) { // == 0
			// $content .= sprintf("&nbsp; &nbsp;");
		} elseif ($c[$j]->num() >= 0) {
			if ($c[$j]->value() == 1) { // == 1
				$content .= sprintf(" + x<sub>%d</sub>", $j);
			} elseif ($c[$j]->num() == 0) { // == 0
				// $content .= sprintf("&nbsp; &nbsp;");
			} else { // > 0 && != 1
				$content .= sprintf(" + %s x<sub>%d</sub>", $c[$j]->fractoa(), $j);
			}
		} elseif ($c[$j]->value() == -1) { // = -1
			$content .= sprintf(" - x<sub>%d</sub>", $j);
		} else {  // < 0 != -1
			$c1 = new razionale ((int)-$c[$j]->num(), (int)$c[$j]->den());
			if ($c[$j]->value() == -1) { // == -1
				$content .= sprintf(" - x<sub>%d</sub>", $j);
			} else {
				$content .= sprintf(" - %s x<sub>%d</sub>", $c1->fractoa(), $j);
			}
			unset ($c1);
		}
	}
	// ... + (-d)
	if (!isset($d))
		; // d == 0 => non visualizzare niente
	elseif ($d->num() > 0) {
		$content .= sprintf(" + %s ", $d->fractoa());
	} elseif ($d->num() < 0) {
		$d1 = new razionale ((int)-$d->num(), (int)$d->den());
		$content .= sprintf(" - %s ", $d1->fractoa());
		unset ($d1);
	} else
		; // d == 0 => non visualizzare niente

	$content .= '<br><br>
        Soggetto a<br><br>
        ';
	// righe dei vincoli
	for ($i = 1; $i < $numConstraints + 1; $i++) {
		// riga del vincolo
		$content .= $i . ') ';
		// variabile 1ma colonna
		if (!isset($a[$i][1]))
			;
		elseif ($a[$i][1]->value() == 1) {
			$content .= sprintf(" x<sub>1</sub>");
		} elseif ($a[$i][1]->num() == 0) {
			// $content .= sprintf("&nbsp; &nbsp;");
		} elseif ($a[$i][1]->value() == -1) {
			$content .= sprintf(" - x<sub>1</sub>");
		} else {
			$content .= sprintf(" %s x<sub>1</sub>", $a[$i][1]->fractoa());
		}
		// le altre colonne
		for ($j = 2; $j < $numVariables + 1; $j++) {
			if (!isset($a[$i][$j]))
				;
			elseif ($a[$i][$j]->num() >= 0) { // >= 0
				if ($a[$i][$j]->value() == 1) {
					$content .= sprintf(" + x<sub>%d</sub>", $j);
				} elseif ($a[$i][$j]->num() == 0) {
					// $content .= sprintf("&nbsp; &nbsp;");
				} else {
					$content .= sprintf(" + %s x<sub>%d</sub>", $a[$i][$j]->fractoa(), $j);
				}
			} elseif ($a[$i][$j]->value() == -1) { // == -1
				$content .= sprintf(" - x<sub>%d</sub>", $j);
			} else {
				$a1 = new razionale ((int)-$a[$i][$j]->num(), (int)$a[$i][$j]->den());
				$content .= sprintf(" - %s x<sub>%d</sub>", $a1->fractoa(), $j);
				unset ($a1);
			}
		}
		// risorse
		$content .= sprintf(" %s %s <br>", htmlentities($lge[$i]), $b[$i]->fractoa());
	}
	// non negativita'
	$content .= '
        &nbsp; &nbsp; &nbsp; &nbsp; x<sub>i</sub> &ge; 0';
	// ed eventuale interezza
	if (!strcmp($intera, "true"))
		$content .= ' e INTERI';
	$content .= " &nbsp; &nbsp; per i = 1,...,$numVariables</strong><br><br>\n\n";

	return $content;
}

?>