#!/usr/bin/python -Wall

# ================================================================
# John Kerl
# kerl.john.r@gmail.com
# 2007-05-20
#
# Uses Householder transformations to factor a square matrix A
# into Q*R where Q is orthogonal and R is upper-triangular.
# ================================================================
# This software is released under the terms of the GNU GPL.
# Please see LICENSE.txt in the same directory as this file.
# ================================================================

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

argc = len(sys.argv)
if (argc != 4):
	print >> sys.stderr, "Usage: %s {A file name} {Q file name} {R file name}" % (sys.argv[0])
	print >> sys.stderr, "Use - as file name(s) to print to standard output."
	sys.exit(1)

A_file_name = sys.argv[1]
Q_file_name = sys.argv[2]
R_file_name = sys.argv[3]

A = read_matrix(float, A_file_name)
n = A.square_dim()
Q = make_zero_matrix(n, n)
R = make_zero_matrix(n, n)
A.QR_decomp(Q, R)
write_matrix(Q,Q_file_name)
if ((Q_file_name == "-") and (R_file_name == "-")):
	print
write_matrix(R,R_file_name)

