Class ArrayMath

java.lang.Object
  |
  +--ArrayMath

public class ArrayMath
extends java.lang.Object

ArrayMath is used to perform various calculations on arrays-- addition, multiplication, transposition. Invoke: java ArrayMath


Constructor Summary
ArrayMath()
           
 
Method Summary
static int[][] addArrays(int[][] array1, int[][] array2)
          Matrix addition.
static void addOneToArray(int[][] array)
          addOne adds one to every element of the array.
static int dotProduct(int[] firstVector, int[] secondVector)
          Computes the dot product of two single-dimension vectors.
static void main(java.lang.String[] args)
          The main method, the entry point for the class when run from the command line.
static int[][] multiplyArrays(int[][] array1, int[][] array2)
          Matrix multiplication.
static void printArray(int[][] array)
          Print out the contents of a 2-dimensional array.
static void printVector(int[] vector)
          prints out the contents of a one-dimensional array in the format (a, b, c).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayMath

public ArrayMath()
Method Detail

main

public static void main(java.lang.String[] args)
The main method, the entry point for the class when run from the command line. This launches several test cases for array math, such as dot products, addition, etc.
Parameters:
args[] - An array of String objects that contain command line arguments
Returns:
void

printVector

public static void printVector(int[] vector)
prints out the contents of a one-dimensional array in the format (a, b, c).
Parameters:
vector - the vector to be printed out

printArray

public static void printArray(int[][] array)
Print out the contents of a 2-dimensional array.
Parameters:
array[][] - the array to be printed out

dotProduct

public static int dotProduct(int[] firstVector,
                             int[] secondVector)
Computes the dot product of two single-dimension vectors. given (a, b, c) and (d, e, f), this computes a*d + b*e + c*f. A new array to hold the result is allocated and returned to the caller. the input arrays are not changed. The input arrays must be the same length. Later, we'll see how to throw an exception if the arrays are not the same length; for now, if the arrays do not match, we simply return -1. This is a bad error value, since it may match legitimate results, but is good enough to demonstrate what is going on with methods and arrays.
Parameters:
firstVector - first input, single dimensioned, not changed
secondVector - second iput, single dimensioned, not changed
Returns:
the dot product, -1 for mismatched array length

addArrays

public static int[][] addArrays(int[][] array1,
                                int[][] array2)
Matrix addition. Given two two-dimensional arrays, add them together. A new array is allocated and returned to the caller. The input arrays are not modified.

This is hard-wired to handle two-dimensional arrays.

Again, it would be better to throw an exception if we have some show-stopping problem like mismatched array sizes rather than just return a null. But we don't know enough about exceptions yet.

Parameters:
array1 - The first input array, must be two-dimensional, not modified by this method
array2 - second input array, not modified by this method
Returns:
A newly allocated array, equal to array1 + array2

multiplyArrays

public static int[][] multiplyArrays(int[][] array1,
                                     int[][] array2)
Matrix multiplication. Given an m x r and an r x n matrix, the result of a*b is an r x n matrix. Each cell ij in the resulting matrix is the result of the dot product of the ith row of a and the jth column of b. The resulting matrix is allocated within this method and returned to the caller.

The number of coluns in a must match the number of rows in b. If this is not the case, the arrays can't be multiplied; null is returned. It would be better to throw an exception.

This is less than perfect, since it doesn't handle single-dimensioned arrays, which are legal; (1, 2, 3) * (1, 2, 3)^T, for example. Everything has to be framed in terms of 2D arrays.

Parameters:
array1 - some 2-dimensional array, number of columns must match number of rows in array2
array2 - some 2-dimensional array, number of rows must match number of columns in array1
Returns:
an array with the number of rows of a and the number of columns of b.

addOneToArray

public static void addOneToArray(int[][] array)
addOne adds one to every element of the array. Note that this modifies the array passed in; arrays are passed by reference.
Parameters:
array - The array to be modified
Returns:
void