// purpose: provide basic and not-so-basic matrix manipulations import java.util.*; public class Matrix { // print(): display a to standard output public static void print(int[][] a) { int rows = a.length; int columns = a[0].length; int[][] b = new int[ rows ][ columns ]; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { int v = a[ r ][ c ]; System.out.printf( "%2d ", v ); } System.out.println(); } } // set(): assigns v to all elements of a public static void set(int[][] a, int v) { int rows = a.length; int columns = a[0].length; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { a[ r ][ c ] = v; } } } // randomize(): assign elements of matrix a, random values from the // interval 0 ... k-1 public static void randomize( int[][] a, int k ) { int rows = a.length; int columns = a[0].length; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { Random die = new Random(); a[ r ][ c ] = die.nextInt( k ); } } } // identity(): makes a the identity matrix public static void identity( int[][] a ) { int rows = a.length; int columns = a[0].length; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { } } } // copy(): return a new copy of a public static int[][] copy(int[][] a) { int rows = a.length; int columns = a[0].length; int[][] b = new int[ rows ][ columns ]; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { int v = a[ r ][ c ]; b[ r ][ c ] = v; } } return b; } // add(): returns a new matrix equal to a + b public static int[][] add( int[][] a, int[][] b ) { int rows = a.length; int columns = a[0].length; int[][] sum = new int[ rows ][ columns ]; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { int v1 = a[ r ][ c ]; int v2 = b[ r ][ c ]; sum[ r ][ c ] = v1 + v2; } } return sum; } // subtract(): returns a new matrix equal to a - b public static int[][] subtract( int[][] a, int[][] b ) { return null; // remove and replace with proper code } // equal(): tests whether a and b are duplicates public static boolean equal(int[][] a, int[][] b) { int rows = a.length; int columns = a[0].length; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { int v1 = a[ r ][ c ]; int v2 = b[ r ][ c ]; } } return true; } // average(): returns a new matrix whose elements are equal to the // average of the corresponding elements in a and b public static int[][] average( int[][] a, int[][] b ) { return null; // remove and replace with proper code } // pixelize(): makes a so that it elements are bytewise 0rgb public static void pixelize( int[][] a ) { int rows = a.length; int columns = a[0].length; for ( int r = 0; r < rows; ++r ) { for ( int c = 0; c < columns; ++c ) { int v = a[ r ][ c ]; v = Math.abs( v ); v = v % ( 256 * 256 * 256 ); a[ r ][ c ] = v; } } } }