Advertisement
Guest User

A2 COMMENTS

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.81 KB | None | 0 0
  1. package uk.ac.brunel.cs1702;
  2.  
  3. public class Matrix {
  4.  
  5.     private int [][] matrix;//2 dimensional arrays becasue we want rows and columns
  6.    
  7.  
  8.     /**
  9.      * Constructor to create a matrix of size rows by COLUMNS and to initialise
  10.      * each element with the value of initialValue      //CONSTRUCOR IS THE NAME GIVEN TO THE MTEHOD THAT INISTATES A NEW CLASS OBJECT//
  11.      *
  12.      * @param rows:
  13.      *            an integer value indicating the number of rows
  14.      * @param COLUMNS:
  15.      *            an integer value indicating the number of columns
  16.      * @param initialValue:
  17.      *            each element of the matrix is initialised to this value
  18.    
  19.      */
  20.  
  21.     public Matrix(int rows, int COLUMNS, int currentValue) {
  22.  
  23.         this.matrix = new int[rows][COLUMNS];
  24.  
  25.         for (int i = 0; i < rows; i++) {
  26.  
  27.             for (int j = 0; j < COLUMNS; j++) {
  28.  
  29.                 this.matrix[i][j] = currentValue;
  30.  
  31.             }
  32.  
  33.         }
  34.  
  35.     }
  36.  
  37.  
  38.  
  39.  
  40.     /**
  41.      * Constructor to create a matrix of size equivalent to the size of int[][]
  42.      * initialData and to initialise each element with the values in
  43.      * initialData.
  44.      *
  45.      * @param initialData:
  46.      *            An array of array of integers (int[][]) whose size determines
  47.      *            the size of the matrix. The array is cloned into the Matrix
  48.      *            object.
  49.      */
  50.  
  51.     public Matrix(int[][] currentData) {
  52.  
  53.         this.matrix = currentData;
  54.  
  55.     }
  56.  
  57.  
  58.  
  59.  
  60.     /**
  61.      * @return Returns the number of rows of the Matrix object.
  62.      */
  63.  
  64.     public int getRows() {
  65.         return this.matrix.length;//this will return the number of rows of the matrix object.Will be used in ouput of the matrix//
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.    
  74.      /**
  75.       * @return Returns the number of columns of the Matrix object.
  76.       */
  77.  
  78.     public int getCols() {
  79.         return this.matrix[0].length;//will return the number of columns of the matrix object. tis will be a one dimensional array.//
  80.     }
  81.  
  82.  
  83.  
  84.  
  85.     /**
  86.    
  87.      * @param i:
  88.      *            data element row index starting from 0
  89.      * @param j:
  90.      *            data element column index starting from 0
  91.      * @return: Returns the element at row i and column j of the Matrix object
  92.      */
  93.  
  94.     public int getData(int i, int j) {
  95.         return this.matrix[i][j];
  96.     }
  97.  
  98.  
  99.  
  100.  
  101.     /**
  102.      * @return Returns a 2-dimensional array of integers (int[][]) where the
  103.      *         matrix elements are stored
  104.      */
  105.  
  106.     public int[][] getData() {
  107.         return this.matrix;
  108.     }
  109.  
  110.  
  111.  
  112.  
  113.     /**
  114.      * @param matrix:
  115.      *            Matrix object to be added to the caller matrix
  116.      * @return Returns a new matrix object which is the sum of self and the
  117.      *         parameter matrix. Returns null if dimensions do not match.
  118.      */
  119.  
  120.     public Matrix add(Matrix matrix) {//Matrix addition//
  121.  
  122.         if (this.getCols() != matrix.getCols() || this.getRows() != matrix.getRows()) {//this checks matrices are the same size
  123.  
  124.             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
  125.  
  126.         }
  127.         int[][] sum = new int[matrix.getRows()][matrix.getCols()];
  128.         for (int i = 0; i < matrix.getRows(); i++) {
  129.  
  130.             for (int j = 0; j < matrix.getCols(); j++) {
  131.  
  132.                 sum[i][j] = this.matrix[i][j] + matrix.getData(i, j);
  133.  
  134.             }
  135.  
  136.         }
  137.  
  138.         return new Matrix(sum);
  139.  
  140.     }
  141.  
  142.  
  143.  
  144.  
  145.     /**
  146.      * @param matrix:
  147.      *            Matrix object to be subtracted from the caller matrix
  148.      * @return Returns a new Matrix object which is the difference of self and
  149.      *         the parameter matrix. Returns null if dimensions do not match.
  150.      */
  151.  
  152.     public Matrix sub(Matrix matrix) {//matrix subtraction//
  153.  
  154.  
  155.         if (this.getCols() != matrix.getCols() || this.getRows() != matrix.getRows()) {//this checks matrices are the same size//
  156.  
  157.             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
  158.            
  159.         }
  160.         int[][] sum = new int[matrix.getRows()][matrix.getCols()];//2 dimensional array
  161.         for (int i = 0; i < matrix.getRows(); i++) {
  162.  
  163.             for (int j = 0; j < matrix.getCols(); j++) {//for loop//
  164.  
  165.                 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.
  166.  
  167.             }//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
  168.  
  169.         }
  170.  
  171.         return new Matrix(sum);//returns a new matrix object which is difference of self and paramter matrix//
  172.  
  173.     }
  174.  
  175.  
  176.  
  177.  
  178.     /**
  179.      * @return Returns the transpose of the matrix as a new Matrix object
  180.      */
  181.  
  182.     public Matrix transpose() {//matrix transponse//
  183.  
  184.         int[][] transa = new int[this.getCols()][this.getRows()];
  185.  
  186.         for (int i = 0; i < this.getCols(); i++) {
  187.  
  188.             for (int j = 0; j < this.getRows(); j++) {
  189.  
  190.                 transa[i][j] = this.matrix[j][i];
  191.  
  192.             }
  193.  
  194.         }
  195.  
  196.         return new Matrix(transa);
  197.  
  198.     }
  199.  
  200.  
  201.  
  202.  
  203.     /**
  204.      * @param matrix:
  205.      *            Matrix object to be concatenated to the caller matrix.
  206.      * @return: Returns the concatenation of the two matrices as a new Matrix
  207.      *          object. Returns null if the number of columns does not match.
  208.      */
  209.  
  210.     public Matrix concat(Matrix matrix) {
  211.  
  212.         if (this.getCols() != matrix.getCols()) {
  213.  
  214.             return null;
  215.  
  216.         }
  217.         int[][] conca = new int[matrix.getRows() + this.getRows()][matrix.getCols()];
  218.         for (int i = 0; i < this.getRows(); i++) {
  219.  
  220.             for (int j = 0; j < this.getCols(); j++) {
  221.  
  222.                 conca[i][j] = this.matrix[i][j];
  223.  
  224.             }
  225.  
  226.         }
  227.  
  228.         for (int i = 0; i < matrix.getRows(); i++) {
  229.  
  230.             for (int j = 0; j < matrix.getCols(); j++) {
  231.  
  232.                 conca[this.getRows() + i][j] = matrix.getData(i, j);
  233.  
  234.             }
  235.  
  236.         }
  237.  
  238.         return new Matrix(conca);
  239.  
  240.     }
  241.  
  242.  
  243.  
  244.  
  245.     /**
  246.      * @param row:
  247.      *            row index starting from 1
  248.      * @return: Returns an array (int[]) containing the requested row. Returns null if the row does not exist.
  249.      */
  250.  
  251.     public int[] getRow(int row) {
  252.  
  253.         if (row < 0 || row > this.getRows()) {
  254.  
  255.             return null;
  256.  
  257.         }
  258.  
  259.         int[] rowsa = new int[this.getRows()];
  260.  
  261.         rowsa = this.matrix[row-1];
  262.  
  263.         return rowsa;
  264.  
  265.     }
  266.  
  267.  
  268.  
  269.  
  270.     /**
  271.      * @param col:
  272.      *            column index starting from 1
  273.      * @return : Returns an array (int[]) containing the requested column. Returns null if the row does not exist.
  274.      */
  275.  
  276.     public int[] getCol(int col) {
  277.  
  278.         if (col <= 0 || col > this.getCols()) {
  279.  
  280.             return null;
  281.  
  282.         }
  283.  
  284.         int[] colna = new int[this.getRows()];
  285.  
  286.         for (int i = 0; i < this.getRows(); i++) {
  287.  
  288.             colna[i] = this.matrix[i][col-1];
  289.  
  290.         }
  291.  
  292.         return colna;
  293.  
  294.    
  295.  
  296.     }
  297.  
  298.  
  299.  
  300.     /**
  301.      * @return : Returns a String representation of the Matrix.
  302.      */
  303.  
  304.     public String toString(){
  305.  
  306.         String result ="";
  307.  
  308.         String ls = System.getProperty("line.separator");
  309.  
  310.    
  311.  
  312.         for (int i = 0; i < this.getRows(); i++){
  313.  
  314.             for (int j = 0; j < this.getCols(); j++)
  315.  
  316.                 result = result.concat(Integer.toString(this.getData(i,j)) + " ");
  317.  
  318.             result = result.concat(ls);
  319.  
  320.         }
  321.  
  322.         return result;     
  323.  
  324.     }
  325.  
  326.  
  327.  
  328.     public static void main(String[] args){
  329.  
  330.         int[][] dataA = {{2, 2},{3, 3}};
  331.  
  332.         int[][] dataB = {{1, 1},{1, 1}};
  333.  
  334.    
  335.  
  336.         Matrix matrixA = new Matrix(dataA);
  337.  
  338.         Matrix matrixB = new Matrix(dataB);
  339.  
  340.    
  341.  
  342.         System.out.println("A+B");
  343.  
  344.         System.out.println(matrixA.add(matrixB).toString());
  345.  
  346.         System.out.println("A-B");
  347.  
  348.         System.out.println(matrixA.sub(matrixB).toString());
  349.  
  350.         System.out.println("transpose(A)");
  351.  
  352.         System.out.println(matrixA.transpose().toString());
  353.  
  354.         System.out.println("A concat B");
  355.  
  356.         System.out.println(matrixA.concat(matrixB).toString());
  357.  
  358.         System.out.println("B concat A");
  359.  
  360.         System.out.println(matrixB.concat(matrixA).toString());
  361.  
  362.         System.out.println("(A concat B) + (B concat A)");
  363.  
  364.         System.out.println(matrixA.concat(matrixB).add(matrixB.concat(matrixA)).toString());
  365.  
  366.         System.out.println("A+B-B");
  367.  
  368.         System.out.println(matrixA.add(matrixB).sub(matrixB).toString());
  369.  
  370.     }
  371.  
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement