Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.ac.brunel.cs1702;
- public class Matrix {
- private int [][] matrix;//2 dimensional arrays becasue we want rows and columns
- /**
- * Constructor to create a matrix of size rows by COLUMNS and to initialise
- * each element with the value of initialValue //CONSTRUCOR IS THE NAME GIVEN TO THE MTEHOD THAT INISTATES A NEW CLASS OBJECT//
- *
- * @param rows:
- * an integer value indicating the number of rows
- * @param COLUMNS:
- * an integer value indicating the number of columns
- * @param initialValue:
- * each element of the matrix is initialised to this value
- */
- public Matrix(int rows, int COLUMNS, int currentValue) {
- this.matrix = new int[rows][COLUMNS];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < COLUMNS; j++) {
- this.matrix[i][j] = currentValue;
- }
- }
- }
- /**
- * Constructor to create a matrix of size equivalent to the size of int[][]
- * initialData and to initialise each element with the values in
- * initialData.
- *
- * @param initialData:
- * An array of array of integers (int[][]) whose size determines
- * the size of the matrix. The array is cloned into the Matrix
- * object.
- */
- public Matrix(int[][] currentData) {
- this.matrix = currentData;
- }
- /**
- * @return Returns the number of rows of the Matrix object.
- */
- public int getRows() {
- return this.matrix.length;//this will return the number of rows of the matrix object.Will be used in ouput of the matrix//
- }
- /**
- * @return Returns the number of columns of the Matrix object.
- */
- public int getCols() {
- return this.matrix[0].length;//will return the number of columns of the matrix object. tis will be a one dimensional array.//
- }
- /**
- * @param i:
- * data element row index starting from 0
- * @param j:
- * data element column index starting from 0
- * @return: Returns the element at row i and column j of the Matrix object
- */
- public int getData(int i, int j) {
- return this.matrix[i][j];
- }
- /**
- * @return Returns a 2-dimensional array of integers (int[][]) where the
- * matrix elements are stored
- */
- public int[][] getData() {
- return this.matrix;
- }
- /**
- * @param matrix:
- * Matrix object to be added to the caller matrix
- * @return Returns a new matrix object which is the sum of self and the
- * parameter matrix. Returns null if dimensions do not match.
- */
- public Matrix add(Matrix matrix) {//Matrix addition//
- if (this.getCols() != matrix.getCols() || this.getRows() != matrix.getRows()) {//this checks matrices are the same size
- return null;//this will return null if the dimensions if the dimensions of columns and rows do not match. this is becasue you cannot add 2 different matrices of different sizes
- }
- int[][] sum = new int[matrix.getRows()][matrix.getCols()];
- for (int i = 0; i < matrix.getRows(); i++) {
- for (int j = 0; j < matrix.getCols(); j++) {
- sum[i][j] = this.matrix[i][j] + matrix.getData(i, j);
- }
- }
- return new Matrix(sum);
- }
- /**
- * @param matrix:
- * Matrix object to be subtracted from the caller matrix
- * @return Returns a new Matrix object which is the difference of self and
- * the parameter matrix. Returns null if dimensions do not match.
- */
- public Matrix sub(Matrix matrix) {//matrix subtraction//
- if (this.getCols() != matrix.getCols() || this.getRows() != matrix.getRows()) {//this checks matrices are the same size//
- return null;// this will return null if the dimenisomns of columns and rows do not match// this is becasue you cannot subtract 2 different matrices of different sizes
- }
- int[][] sum = new int[matrix.getRows()][matrix.getCols()];//2 dimensional array
- for (int i = 0; i < matrix.getRows(); i++) {
- for (int j = 0; j < matrix.getCols(); j++) {//for loop//
- sum[i][j] = this.matrix[i][j] - matrix.getData(i, j);//takes the sum of columns and rows and subtracts it from the data that is stored in a paramter.
- }//this.matrix lets code know that you are asking for data stored inside of the object that you are calling your method from// which is rows and columns
- }
- return new Matrix(sum);//returns a new matrix object which is difference of self and paramter matrix//
- }
- /**
- * @return Returns the transpose of the matrix as a new Matrix object
- */
- public Matrix transpose() {//matrix transponse//
- int[][] transa = new int[this.getCols()][this.getRows()];
- for (int i = 0; i < this.getCols(); i++) {
- for (int j = 0; j < this.getRows(); j++) {
- transa[i][j] = this.matrix[j][i];
- }
- }
- return new Matrix(transa);
- }
- /**
- * @param matrix:
- * Matrix object to be concatenated to the caller matrix.
- * @return: Returns the concatenation of the two matrices as a new Matrix
- * object. Returns null if the number of columns does not match.
- */
- public Matrix concat(Matrix matrix) {
- if (this.getCols() != matrix.getCols()) {
- return null;
- }
- int[][] conca = new int[matrix.getRows() + this.getRows()][matrix.getCols()];
- for (int i = 0; i < this.getRows(); i++) {
- for (int j = 0; j < this.getCols(); j++) {
- conca[i][j] = this.matrix[i][j];
- }
- }
- for (int i = 0; i < matrix.getRows(); i++) {
- for (int j = 0; j < matrix.getCols(); j++) {
- conca[this.getRows() + i][j] = matrix.getData(i, j);
- }
- }
- return new Matrix(conca);
- }
- /**
- * @param row:
- * row index starting from 1
- * @return: Returns an array (int[]) containing the requested row. Returns null if the row does not exist.
- */
- public int[] getRow(int row) {
- if (row < 0 || row > this.getRows()) {
- return null;
- }
- int[] rowsa = new int[this.getRows()];
- rowsa = this.matrix[row-1];
- return rowsa;
- }
- /**
- * @param col:
- * column index starting from 1
- * @return : Returns an array (int[]) containing the requested column. Returns null if the row does not exist.
- */
- public int[] getCol(int col) {
- if (col <= 0 || col > this.getCols()) {
- return null;
- }
- int[] colna = new int[this.getRows()];
- for (int i = 0; i < this.getRows(); i++) {
- colna[i] = this.matrix[i][col-1];
- }
- return colna;
- }
- /**
- * @return : Returns a String representation of the Matrix.
- */
- public String toString(){
- String result ="";
- String ls = System.getProperty("line.separator");
- for (int i = 0; i < this.getRows(); i++){
- for (int j = 0; j < this.getCols(); j++)
- result = result.concat(Integer.toString(this.getData(i,j)) + " ");
- result = result.concat(ls);
- }
- return result;
- }
- public static void main(String[] args){
- int[][] dataA = {{2, 2},{3, 3}};
- int[][] dataB = {{1, 1},{1, 1}};
- Matrix matrixA = new Matrix(dataA);
- Matrix matrixB = new Matrix(dataB);
- System.out.println("A+B");
- System.out.println(matrixA.add(matrixB).toString());
- System.out.println("A-B");
- System.out.println(matrixA.sub(matrixB).toString());
- System.out.println("transpose(A)");
- System.out.println(matrixA.transpose().toString());
- System.out.println("A concat B");
- System.out.println(matrixA.concat(matrixB).toString());
- System.out.println("B concat A");
- System.out.println(matrixB.concat(matrixA).toString());
- System.out.println("(A concat B) + (B concat A)");
- System.out.println(matrixA.concat(matrixB).add(matrixB.concat(matrixA)).toString());
- System.out.println("A+B-B");
- System.out.println(matrixA.add(matrixB).sub(matrixB).toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement