import sys

if len(sys.argv) < 2:
    sys.stderr.write('Data file name must be supplied\n')
    sys.exit(1)

fname = sys.argv[1]
#M = HarwellBoeingMatrix(fname, patternOnly=True, readRhs=False)
M = RutherfordBoeingData(fname, patternOnly=True, readRhs=False)

if M.nrow != M.ncol or not M.issym:
    sys.stderr.write('Input matrix must be square and symmetric\n')
    sys.exit(1)

# Compute reverse Cuthill-McKee ordering
perm, rinfo = rcmk(M.nrow, M.ind, M.ip)

# Or: Compute Sloan's ordering
#perm, rinfo = sloan(M.nrow, M.ind, M.ip)


# Plot original matrix
(_, irow, jcol) = M.find()
left = pylab.subplot(121)
fast_spy(M.nrow, M.ncol, irow, jcol, sym=M.issym,
         ax=left.get_axes(), title='Original')

# Apply permutation and plot reordered matrix
right = pylab.subplot(122)
fast_spy(M.nrow, M.ncol, perm[irow], perm[jcol], sym=M.issym,
         ax=right.get_axes(), title='Reordered')
import cysparse.types.cysparse_types as types

from hsl.tools.spy import fast_spy
import numpy as np
import matplotlib.pyplot as plt


A = LLSparseMatrix(mm_filename='bcsstk01.mtx',
                   itype=types.INT32_T, dtype=types.FLOAT64_T)
m, n = A.shape
irow, jcol, val = A.find()

A_csc = A.to_csc()
colptr, rowind, values = A_csc.get_numpy_arrays()

perm1, rinfo1 = rcmk(n, rowind, colptr)   # Reverse Cuthill-McKee
perm2, rinfo2 = sloan(n, rowind, colptr)  # Sloan's method

left = plt.subplot(131)
fast_spy(n, n, irow, jcol, sym=True,
         ax=left.get_axes(), title='Original')

# Apply permutation 1 and plot reordered matrix
middle = plt.subplot(132)
fast_spy(n, n, perm1[irow], perm1[jcol], sym=True,
         ax=middle.get_axes(), title='Rev. Cuthill-McKee (semibandwidth=%d)' % rinfo1[2])

# Apply permutation 2 and plot reordered matrix
right = plt.subplot(133)
fast_spy(n, n, perm2[irow], perm2[jcol], sym=True,
         ax=right.get_axes(), title='Sloan (semibandwidth=%d)' % rinfo2[2])