#!/usr/bin/python -Wall # ================================================================ # Produces output as follows: # # bash$ tridiag 6 2 -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 -1 # 0 0 0 0 -1 2 # # John Kerl # kerl.john.r@gmail.com # 2006-05-26 # ================================================================ # 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 argc = len(sys.argv) if (argc != 4): print >> sys.stderr, "Usage: ", sys.argv[0], "{n} {diag element} {off-diag element}" sys.exit(1) n = int(sys.argv[1]) d = float(sys.argv[2]) u = float(sys.argv[3]) # Don't print decimal points for integer values. id = int(d) iu = int(u) if (d == float(id)): d = id if (u == float(iu)): u = iu for i in range(0, n): for j in range(0, n): if (i == j): print d, elif (i == j-1): print u, elif (i == j+1): print u, else: print 0, print