[net.math] Recursive Determinant Evaluation

egs@epsilon.UUCP (Ed Sheppard) (02/25/85)

As Greg Kuperberg has pointed out in private mail (thanks Greg),
my determinant evaluator was wrong. In particular, the matrix

	[ 0 1 ]
	[ 1 0 ]

has a det of -1, not zero as my function would have reported. Well,
that's strike one. Let me try again with the following:

float	determ(n,a)
int	n;
float	a[n][n];	/* ugh! */
{
	int	i, j;
	float	t;

	if(n==1) return(a[0][0]);

	for(i=0; i<n; ++i)
	{
		if(a[i][0]!=0) break;
	}

	if(i)
	{
		if(i>=n) return(0);	/* zero column -> zero det */

			/* make a[0] = a[0]+a[i], a[i] = -a[0] */

		a[0][0] = a[i][0];
		for(j=1; j<n; ++j)
		{
			t = -a[0][j]; a[0][j] += a[i][j]; a[i][j] = t;
		}
	}
	for(++i; i<n; ++i)
	{
		factor = a[i][0]/a[0][0];
		for(j=1; j<n; ++j)
		{
			a[i][j] -= factor*a[0][j];
		}
	}
	return(a[0][0]*determ(n-1,&a[1][1]));
}

Maybe I got it right this time, or is this strike two :-)?

						Ed Sheppard
						Bellcore