#!/usr/bin/perl -w

# ----------------------------------------------------------------
# John Kerl
# kerl.john.r@gmail.com
# 2005-01-11
#
# This is a real matrix determinant.  Example:
#
# bash$ cat b.txt
# -2  1  0  0  0  0
#  1 -2  1  0  0  0
#  0  1 -2  1  0  0
#  0  0  1 -2  1  0
#  0  0  0  1 -2  1
#  0  0  0  0  1 -2
# bash$ pdet a.txt
#     7.00000000000
# ----------------------------------------------------------------

# Setup information:
# (1) Obtain my PMATLIB.pm;
# (2) Put PMATLIB.pm somewhere, e.g. the $HOME/bin directory;
# (3) Include that directory in the PERLLIB environment variable.
#     For bash, if PERLLIB exists:         export PERLLIB=$HOME/bin
#     For bash, if PERLLIB does not exist: export PERLLIB=$PERLLIB:$HOME/bin
#     For csh, if PERLLIB exists:          setenv PERLLIB $HOME/bin
#     For csh, if PERLLIB does not exist:  setenv PERLLIB ${PERLLIB}:$HOME/bin
use PMATLIB;

while (@ARGV) {
	last unless $ARGV[0] =~ m/^-/;

	if (pmatlib_opt(\@ARGV)) {
		;
	}
	else {
		usage();
	}
}

if (@ARGV > 1) {
	usage();
}

@A   = (); $nr = 0; $nc = 0; 

read_matrix(\@A, \$nr, \$nc, $ARGV[0]);
die "$0:  Non-square input.  Got $nr x $nc.\n" unless ($nr == $nc);
$det = matdet(\@A, $nr);
print_scalar($det);
print "\n";

# ----------------------------------------------------------------
sub usage
{
	die
		"Usage: $0 [options] [input file name]\n" .
		"Options:\n" .
		"  -w:  specify field width\n" .
		"  -p:  specify number of decimal places\n" .
		"  -f:  use %f format\n" .
		"  -d:  use %d format\n";
}
