/* * 22C:021 Java Structures * Project 1 - MatrixMultiplication Implementaions * * By TA: Zhihong Wang (2004-10-25) * * Methods implemented: * - multiplySparseMatrix(SparseMatrix sm1,SparseMatrix sm2 ) * - multiplyMatrix(Matrix m1,Matrix m2) * - randomInt(double p) * - createRandomSparseMatrix(int n) * - createRandomMatrix(int n) */ import structure.*; import java.util.Random; public class MatrixMultiplication { protected static Random rand = new Random();; /** * Multiply two SparseMatrix objects sm1 and sm2. * Pre (sm1!=null&&sm2!=null) &&(sm1.width()==sm2.height()) * Retrun a SparseMatrix object which is the product of sm1 and sm2. */ public static SparseMatrix multiplySparseMatrix(SparseMatrix sm1,SparseMatrix sm2 ) { Assert.pre(sm1!=null&&sm2!=null, "Non-null SparseMatrixReference(s)"); Assert.pre(sm1.width()==sm2.height(), "Muliplyable matrices"); SparseMatrix sm3 = new SparseMatrix (sm1.height(), sm2.width()); Matrix row_mat, col_mat; for( int i=0;i=0 && 0<= p <= 1 * Retrun the generated Matrix object */ public static Matrix createRandomMatrix(int n, double p) { Assert.pre(n>=0, "Positive Matrix dimension"); Assert.pre(p>=0 && p<=1, "Probability between 0.0 and 1.0"); Matrix rand_mat = new Matrix(n,n); for(int i=0; i