Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. package module5;
  2.  
  3. public class Matrix {
  4.    
  5.     // Instance variable to store matrix contents
  6.     private double[][] contents;
  7.    
  8.     // Constructor
  9.     public Matrix(double[][] m){
  10.         // Set the contents variable to matrix's argument
  11.         this.contents = m;
  12.     }
  13.    
  14.     // Number of rows - Non static
  15.     public int nRows(){
  16.         return this.contents.length;
  17.     }
  18.    
  19.     // Number of columns (in first row) - Non static
  20.     public int nCols(){
  21.         return this.contents[0].length;
  22.     }
  23.    
  24.     // Is current matrix square? - Non static
  25.     public boolean isSquare(){
  26.         if(this.nRows() == this.nCols()){
  27.             return true;
  28.         } else {
  29.             return false;
  30.         }
  31.     }
  32.    
  33.     // Create a new matrix containing diagonal elements of original - Non static
  34.     public Matrix diagonal() throws Exception {
  35.         // Determine if matrix is square
  36.         if (!this.isSquare()) {
  37.             throw new Exception("Matrix is not square");
  38.         } else {
  39.             // Create a new array (with dimensions of current matrix) to store the diagonal values
  40.             double[][] a1 = new double[this.nCols()][this.nRows()];
  41.            
  42.             for(int i = 0; i < this.nCols(); i++){
  43.                 // Step through the diagonal elements only (0,0 then 1,1 etc)
  44.                 a1[i][i] = this.contents[i][i];
  45.             }
  46.             // Create a new matrix using the diagonal array contents
  47.             return new Matrix(a1);
  48.         }
  49.     }
  50.    
  51.     // Add two matrices - Non static
  52.     public Matrix add(Matrix m2) throws Exception {
  53.         // Determine if the rank of the two matrices match
  54.         if(this.nCols() != m2.nCols()) {
  55.             throw new Exception("Number of coloumns do not match");
  56.         } else if(this.nRows() != m2.nRows()) {
  57.             throw new Exception("Number of rows do not match");
  58.         } else {
  59.             // Create a new array (with dimensions of current array) to store the addition
  60.             double[][] m3 = new double[this.nCols()][this.nRows()];
  61.            
  62.             // Cycle addition over each row
  63.             for(int x = 0; x < this.nRows(); x++){
  64.                 // Within each row, cycle addition over each column
  65.                 for(int y = 0; y < this.nCols(); y++){
  66.                     // Fill the new array with the new added elements
  67.                     m3[x][y] = this.contents[x][y] + m2.contents[x][y];
  68.                 }
  69.             }
  70.             // Return a new matrix using the added array contents
  71.             return new Matrix(m3);
  72.         }
  73.     }
  74.    
  75.     // Add two matrices - Static
  76.     public static Matrix add(Matrix m1, Matrix m2) throws Exception {
  77.         // Determine if the rank of the two matrices match
  78.         if(m1.nCols() != m2.nCols()){
  79.             throw new Exception("Number of coloumns do not match");
  80.         } else if(m1.nRows() != m2.nRows()) {
  81.             throw new Exception("Number of rows do not match");
  82.         } else {
  83.             // Create a new array (with dimensions of current array) to store the addition
  84.             double[][] m3 = new double[m1.nCols()][m1.nRows()];
  85.            
  86.             // Cycle addition over each row
  87.             for(int x = 0; x < m1.nRows(); x++){
  88.                 // Within each row, cycle addition over each column
  89.                 for(int y = 0; y < m1.nCols(); y++){
  90.                     // Fill the new array with the new added elements
  91.                     m3[x][y] = m1.contents[x][y] + m2.contents[x][y];
  92.                 }
  93.             }
  94.             // Return a new matrix using the added array contents
  95.             return new Matrix(m3);
  96.         }
  97.     }
  98.    
  99.     // Compare if two matrices are equal - Non static
  100.     public boolean equals(Matrix m2){
  101.         // Cycle comparison over each row
  102.         for(int x = 0; x < this.nRows(); x++) {
  103.             // Within each row, cycle comparison over each column
  104.             for(int y = 0; y < this.nCols(); y++) {
  105.                 // If the current element does not equal the compared element, return false
  106.                 if(this.contents[x][y] != m2.contents[x][y]) {
  107.                     return false;
  108.                 }
  109.             }
  110.         }
  111.         // Loop has completed without returning false, so matrices must match. Return true
  112.         return true;
  113.     }
  114.    
  115.     // Compare if two matrices are equal - Static
  116.     public static boolean equals(Matrix m1, Matrix m2){
  117.         // Cycle comparison over each row
  118.         for(int x = 0; x < m1.nRows(); x++){
  119.             // Within each row, cycle comparison over each column
  120.             for(int y = 0; y < m1.nCols(); y++){
  121.                 // If the current element does not equal the compared element, return false
  122.                 if(m1.contents[x][y] != m2.contents[x][y]){
  123.                     return false;
  124.                 }
  125.             }
  126.         }
  127.         // Loop has completed without returning false, so matrices must match. Return true
  128.         return true;
  129.     }
  130.    
  131.     // Create a string representation of the current matrix - Non static
  132.     public String toString(){
  133.         // Initialise an empty string
  134.         String matrixString = "";
  135.        
  136.         // Cycle over each row
  137.         for(int x = 0; x < this.nRows(); x++) {
  138.             // Within each row, cycle over each column
  139.             for(int y = 0; y < this.nCols(); y++) {
  140.                 // Add the current element to the string, then add a space
  141.                 matrixString += this.contents[x][y];
  142.                 matrixString += " ";
  143.             }
  144.             // Add a new line at the end of each row
  145.             matrixString += "\n";
  146.         }
  147.         return matrixString;
  148.     }
  149.    
  150.     public static void main(String[] args) throws Exception {
  151.         // Define the contents of two arrays
  152.         double[][] contents1 = {
  153.             {1.0,2.1,3.2,4.3},
  154.             {11.5,12.6,13.7,14.8},
  155.             {11.9,12.0,13.1,14.2},
  156.             {11.9,12.0,13.1,14.2}
  157.         };
  158.        
  159.         double[][] contents2 = {
  160.                 {5.0,6.1,7.2,4.3},
  161.                 {11.5,2.9,13.0,14.8},
  162.                 {11.9,18.0,1.1,14.2},
  163.                 {1.9,1.0,3.1,12.2}
  164.         };
  165.        
  166.         // Create new Matrix objects with these contents
  167.         Matrix m1 = new Matrix(contents1);
  168.         Matrix m2 = new Matrix(contents2);
  169.        
  170.         // Count number of rows
  171.         System.out.println("Number of rows: "+m1.nRows());
  172.        
  173.         // Count number of columns
  174.         System.out.println("Number of columns: "+m1.nCols());
  175.        
  176.         // Determine if matrix is square
  177.         System.out.println("Is the matrix square? "+m1.isSquare());
  178.        
  179.         // Create a matrix of only diagonal elements
  180.         System.out.println("Diagonal matrix: "+m1.diagonal());
  181.        
  182.         // Add two matrices - non static
  183.         System.out.println("Addition of two matrices (non static): "+m1.add(m2));
  184.        
  185.         // Add two matrices - static
  186.         System.out.println("Addition of two matrices (static): " +Matrix.add(m1, m2));
  187.        
  188.         // Determine if two matrices are equal - non static
  189.         System.out.println("Are two matrices equal (non static)? "+m1.equals(m2));
  190.        
  191.         // Determine if two matrices are equal - static
  192.         System.out.println("Are two matrices equal (static)? "+Matrix.equals(m1, m2));
  193.        
  194.         // Produce a string representation of a matrix
  195.         System.out.println("String representation of matrix: "+m1.toString());
  196.     }
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement