From 3958af6e1f7fa01fdb46ef892bf8d1fc486d4119 Mon Sep 17 00:00:00 2001 From: mercury Date: Wed, 3 Dec 2003 17:39:52 +0000 Subject: [PATCH] Initial revision --- Matrices.java | 24 ++++++++++ Matrix.java | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 Matrices.java create mode 100644 Matrix.java diff --git a/Matrices.java b/Matrices.java new file mode 100644 index 0000000..1a6657a --- /dev/null +++ b/Matrices.java @@ -0,0 +1,24 @@ +// Andrew Coleman +// Matrices.java +// demonstrates the use of the Matrix class + +import Matrix; + +public class Matrices { + public static void main (String[] args) { + int[][] matrix1 = {{1, 1, 1}, {5, 6, 4}, {0, 2, 3}}; + int[][] matrix2 = {{0, 0, 0}, {4, 9, 2}, {1, -2, 6}}; + Matrix m1 = new Matrix(matrix1); + Matrix m2 = new Matrix(matrix2); + System.out.println(m1 + "\n"); + System.out.println(m2 + "\n"); + System.out.println("Addition:"); + System.out.println(m1.add(m2)+"\n"); + System.out.println("Subtraction:"); + System.out.println(m1.subtract(m2)+"\n"); + System.out.println("Multiplication:"); + System.out.println(m1.multiply(m2)+"\n"); + System.out.println("scalar multiplication (m1 * 2):"); + System.out.println(m1.scalar_multiply(2)+"\n"); + } +} \ No newline at end of file diff --git a/Matrix.java b/Matrix.java new file mode 100644 index 0000000..3bfdf6b --- /dev/null +++ b/Matrix.java @@ -0,0 +1,129 @@ +// Andrew Coleman +// Matrix.java +// Class for using a matrix of integers +// and a set of algorithms to perform on the matrix + +public class Matrix { + private int rows = 0, cols = 0; + private int[][] matrix; + + // the standard constructor + public Matrix (int r, int c) { + rows = r;cols = c; + matrix = new int[rows][cols]; + } + + // constructor to use with an array + public Matrix (int[][] array) { + rows = array.length; + cols = array[0].length; + matrix = new int[rows][cols]; + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + matrix[r][c] = array[r][c]; + } + + public int getRows () { + return rows; + } + + public int getColumns () { + return cols; + } + + public int getElement (int r, int c) { + if (r <= rows && c <= cols) + return matrix[r][c]; + return -999; + } + + public Matrix setElement (int r, int c, int value) { + if (r <= rows && c <= cols) + matrix[r][c] = value; + return this; + } + + // adds the totals in a row + public int addRow (int r) { + int result = 0; + for (int i = 0; i < rows; i++) + result += matrix[r][i]; + return result; + } + + // adds the totals in a column + public int addColumn (int c) { + int result = 0; + for (int i = 0; i < cols; i++) + result += matrix[i][c]; + return result; + } + + // adds two matrices + public Matrix add (Matrix m2) { + if (m2.getRows() == rows && m2.getColumns() == cols) { + int[][] result = new int[rows][cols]; + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + result[r][c] = this.getElement(r, c) + m2.getElement(r, c); + return new Matrix(result); + } + return this; + } + + // subtracts two matrices + public Matrix subtract (Matrix m2) { + if (m2.getRows() == rows && m2.getColumns() == cols) { + int[][] result = new int[rows][cols]; + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + result[r][c] = this.getElement(r, c) - m2.getElement(r, c); + return new Matrix(result); + } + return this; + } + + // multiplies two matricies + public Matrix multiply (Matrix m2) { + if (m2.getRows() == cols && rows == m2.getColumns() ) { + int[][] result = new int[rows][m2.getColumns()]; + for (int r = 0; r < rows; r++) + for (int c = 0; c < m2.getColumns(); c++) + result[r][c] = this.addRow(r) + m2.addColumn(c); + return new Matrix(result); + } + return this; + } + + // multiplies the current matrix by a scalar value + public Matrix scalar_multiply (int value) { + int[][] result = new int[rows][cols]; + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + result[r][c] = matrix[r][c] * value; + return new Matrix(result); + } + + // checks equality on another matrix + public boolean equals(Matrix m2) { + boolean result = true; + if (rows == m2.getRows() && cols == m2.getColumns() ) + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + if (matrix[r][c] != m2.getElement(r, c) && result) + result = false; + return result; + } + + // overridden toString method to give you a very pretty output + public String toString() { + String result = ""; + for (int r = 0; r < rows; r++) { + result += " [ "; + for (int c = 0; c < cols; c++) + result += (matrix[r][c] + " "); + result += "]\n"; + } + return result; + } +} \ No newline at end of file