// Copyright (c) 1996, 1997, 1998 Bruno R. Preiss, P.Eng. // $Author: brpreiss $ // $Date: 1998/12/14 02:52:01 $ // $RCSfile: Demo1.java,v $ // $Revision: 3.5 $ package Opus5; /** * Demonstration program that tests the following classes: * DenseMatrix, * SparseMatrixAsArray, * SparseMatrixAsVector, and * SparseMatrixAsLinkedList. * @see DenseMatrix * @see SparseMatrixAsArray * @see SparseMatrixAsVector * @see SparseMatrixAsLinkedList * @author Bruno R. Preiss, P.Eng. * @version $Id: Demo1.java,v 3.5 1998/12/14 02:52:01 brpreiss Exp $ **/ public class Demo1 { //[ final String copyright = "@(#) Copyright (c) 1998 by Bruno R. Preiss, P.Eng."; //] /** * Performs a number of tests on the specified matrix. * @param mat The matrix to be tested. **/ private static void testMatrix (Matrix mat) { Terminal.out.println ("Matrix Test (" + mat.getClass ().getName ()+ ")"); int k = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) mat.put (i, j, k++); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) Terminal.out.print (mat.get (i, j) + " "); Terminal.out.println (); } mat = mat.times (mat); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) Terminal.out.print (mat.get (i, j) + " "); Terminal.out.println (); } } /** * Performs a number of tests on the specified sparse matrix * @param mat The sparse matrix to be tested. **/ private static void testTranspose (SparseMatrix mat) { try { Terminal.out.println ("Transpose Test (" + mat.getClass ().getName ()+ ")"); mat.put (0,0,31); mat.put (0,2,41); mat.put (0,3,59); mat.put (1,1,26); mat.put (2,3,53); mat.put (2,4,58); mat.put (4,2,97); mat.put (5,1,93); mat.put (5,5,23); for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) Terminal.out.print (mat.get (i, j) + " "); Terminal.out.println (); } mat.putZero (2,4); mat.putZero (5,3); mat = (SparseMatrix) mat.transpose (); for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) Terminal.out.print (mat.get (i, j) + " "); Terminal.out.println (); } } catch (MethodNotImplemented exception) { Terminal.out.println (exception); } } /** * Tests matrix multiplication by multiplying the specified matrices. * @param mat1 A matrix. * @param mat2 A matrix **/ private static void testTimes (Matrix mat1, Matrix mat2) { try { Terminal.out.println ("Multiply Test (" + mat1.getClass ().getName ()+ ")"); mat1.put (0, 0, 1); mat1.put (0, 1, 2); mat1.put (0, 2, 3); mat2.put (0, 0, 1); mat2.put (1, 0, 2); mat2.put (2, 0, 3); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) Terminal.out.print (mat1.get (i, j) + " "); Terminal.out.println (); } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) Terminal.out.print (mat2.get (i, j) + " "); Terminal.out.println (); } mat1 = (SparseMatrix) mat2.times (mat1); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) Terminal.out.print (mat1.get (i, j) + " "); Terminal.out.println (); } } catch (MethodNotImplemented exception) { Terminal.out.println (exception); } } /** * The (static) main program. * Invokes various test routines. * @param args The command-line arguments. Ignored. **/ public static void main (String[] args) { Matrix mat = new DenseMatrix (3, 3); testMatrix (mat); SparseMatrix smat = new SparseMatrixAsVector (6, 6, 6); testTranspose (smat); Matrix mat1 = new SparseMatrixAsVector (3, 3, 9); Matrix mat2 = new SparseMatrixAsVector (3, 3, 9); testTimes (mat1, mat2); smat = new SparseMatrixAsArray (6, 6, 6); testTranspose (smat); mat1 = new SparseMatrixAsArray (3, 3, 9); mat2 = new SparseMatrixAsArray (3, 3, 9); testTimes (mat1, mat2); smat = new SparseMatrixAsLinkedList (6, 6); testTranspose (smat); mat1 = new SparseMatrixAsLinkedList (3, 3); mat2 = new SparseMatrixAsLinkedList (3, 3); testTimes (mat1, mat2); } }