PHP Gauss

 avatar
unknown
php
4 years ago
3.4 kB
5
Indexable
<?php
// PHP program to demonstrate working
// of Guassian Elimination method

$N = 3; // Number of unknowns


// function to get matrix content
function gaussianElimination($mat)
{
	global $N;
	
	/* reduction into r.e.f. */
	$singular_flag = forwardElim($mat);

	/* if matrix is singular */
	if ($singular_flag != -1)
	{
		print("Singular Matrix.\n");

		/* if the RHS of equation corresponding to
		zero row is 0, * system has infinitely
		many solutions, else inconsistent*/
		if ($mat[$singular_flag][$N])
			print("Inconsistent System.");
		else
			print("May have infinitely many solutions.");

		return;
	}

	/* get solution to system and print it using
	backward substitution */
	backSub($mat);
}

// function for elementary operation
// of swapping two rows
function swap_row(&$mat, $i, $j)
{
	global $N;
	//printf("Swapped rows %d and %d\n", i, j);

	for ($k = 0; $k <= $N; $k++)
	{
		$temp = $mat[$i][$k];
		$mat[$i][$k] = $mat[$j][$k];
		$mat[$j][$k] = $temp;
	}
}

// function to print matrix content at any stage
function print1($mat)
{
	global $N;
	for ($i=0; $i<$N; $i++, print("\n"))
		for ($j=0; $j<=$N; $j++)
			print($mat[$i][$j]);

	print("\n");
}

// function to reduce matrix to r.e.f.
function forwardElim(&$mat)
{
	global $N;
	for ($k=0; $k<$N; $k++)
	{
		// Initialize maximum value and index for pivot
		$i_max = $k;
		$v_max = $mat[$i_max][$k];

		/* find greater amplitude for pivot if any */
		for ($i = $k+1; $i < $N; $i++)
			if (abs($mat[$i][$k]) > $v_max)
				{
					$v_max = $mat[$i][$k];
					$i_max = $i;
				}

		/* if a prinicipal diagonal element is zero,
		* it denotes that matrix is singular, and
		* will lead to a division-by-zero later. */
		if (!$mat[$k][$i_max])
			return $k; // Matrix is singular

		/* Swap the greatest value row with current row */
		if ($i_max != $k)
			swap_row($mat, $k, $i_max);


		for ($i = $k + 1; $i < $N; $i++)
		{
			/* factor f to set current row kth element to 0,
			* and subsequently remaining kth column to 0 */
			$f = $mat[$i][$k]/$mat[$k][$k];

			/* subtract fth multiple of corresponding kth
			row element*/
			for ($j = $k + 1; $j <= $N; $j++)
				$mat[$i][$j] -= $mat[$k][$j] * $f;

			/* filling lower triangular matrix with zeros*/
			$mat[$i][$k] = 0;
		}

		//print(mat); //for matrix state
	}
	//print(mat);	 //for matrix state
	return -1;
}

// function to calculate the values of the unknowns
function backSub(&$mat)
{
	global $N;
	$x = array_fill(0, $N, 0); // An array to store solution

	/* Start calculating from last equation up to the
	first */
	for ($i = $N - 1; $i >= 0; $i--)
	{
		/* start with the RHS of the equation */
		$x[$i] = $mat[$i][$N];

		/* Initialize j to i+1 since matrix is upper
		triangular*/
		for ($j = $i + 1; $j < $N; $j++)
		{
			/* subtract all the lhs values
			* except the coefficient of the variable
			* whose value is being calculated */
			$x[$i] -= $mat[$i][$j] * $x[$j];
		}

		/* divide the RHS by the coefficient of the
		unknown being calculated */
		$x[$i] = $x[$i] / $mat[$i][$i];
	}

	print("\nSolution for the system:\n");
	for ($i = 0; $i < $N; $i++)
		print(number_format(strval($x[$i]), 6)."\n");
}

// Driver program

	/* input matrix */
	$mat = array(array(3.0, 2.0,-4.0, 3.0),
						array(2.0, 3.0, 3.0, 15.0),
						array(5.0, -3, 1.0, 14.0));

	gaussianElimination($mat);

// This code is contributed by mits
?>
Editor is loading...