#!/usr/bin/python -Wall

# ================================================================
# This software is released under the terms of the GNU GPL.
# Please see LICENSE.txt in the same directory as this file.
# Copyright (c) John Kerl 2007
# kerl.john.r@gmail.com
# ================================================================

from __future__ import division # 1/2 = 0.5, not 0.
import sys
from sackmat_m import *

# ----------------------------------------------------------------
def usage():
	print >> sys.stderr, "Usage: %s {text file}" % (sys.argv[0])
	print >> sys.stderr, "Or:    %s -b {n} {input binary file} {output binary file}" % (sys.argv[0])
	sys.exit(1)

# ----------------------------------------------------------------
argc = len(sys.argv)
if (argc >= 2) and (sys.argv[1] == '-b'):
	# E.g. pmatinv -b 10 a.bin b.bin
	if (argc != 5):
		usage()
	n = int(sys.argv[2])
	input_file_name  = sys.argv[3]
	output_file_name = sys.argv[4]

	A = read_float_matrix_binary(n, n, input_file_name)
	B = A.inv()
	write_float_matrix_binary(B, output_file_name)

else:
	argc = len(sys.argv)
	if (argc < 2):
		usage()

	argi = 1
	while (argi < argc):
		A = read_matrix(float, sys.argv[argi])
		B = A.inv()
		print_matrix(B)
		argi += 1
		if (argi < argc):
			print
