Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package module5;
- public class Matrix {
- // Instance variable to store matrix contents
- private double[][] contents;
- // Constructor
- public Matrix(double[][] m){
- // Set the contents variable to matrix's argument
- this.contents = m;
- }
- // Number of rows - Non static
- public int nRows(){
- return this.contents.length;
- }
- // Number of columns (in first row) - Non static
- public int nCols(){
- return this.contents[0].length;
- }
- // Is current matrix square? - Non static
- public boolean isSquare(){
- if(this.nRows() == this.nCols()){
- return true;
- } else {
- return false;
- }
- }
- // Create a new matrix containing diagonal elements of original - Non static
- public Matrix diagonal() throws Exception {
- // Determine if matrix is square
- if (!this.isSquare()) {
- throw new Exception("Matrix is not square");
- } else {
- // Create a new array (with dimensions of current matrix) to store the diagonal values
- double[][] a1 = new double[this.nCols()][this.nRows()];
- for(int i = 0; i < this.nCols(); i++){
- // Step through the diagonal elements only (0,0 then 1,1 etc)
- a1[i][i] = this.contents[i][i];
- }
- // Create a new matrix using the diagonal array contents
- return new Matrix(a1);
- }
- }
- // Add two matrices - Non static
- public Matrix add(Matrix m2) throws Exception {
- // Determine if the rank of the two matrices match
- if(this.nCols() != m2.nCols()) {
- throw new Exception("Number of coloumns do not match");
- } else if(this.nRows() != m2.nRows()) {
- throw new Exception("Number of rows do not match");
- } else {
- // Create a new array (with dimensions of current array) to store the addition
- double[][] m3 = new double[this.nCols()][this.nRows()];
- // Cycle addition over each row
- for(int x = 0; x < this.nRows(); x++){
- // Within each row, cycle addition over each column
- for(int y = 0; y < this.nCols(); y++){
- // Fill the new array with the new added elements
- m3[x][y] = this.contents[x][y] + m2.contents[x][y];
- }
- }
- // Return a new matrix using the added array contents
- return new Matrix(m3);
- }
- }
- // Add two matrices - Static
- public static Matrix add(Matrix m1, Matrix m2) throws Exception {
- // Determine if the rank of the two matrices match
- if(m1.nCols() != m2.nCols()){
- throw new Exception("Number of coloumns do not match");
- } else if(m1.nRows() != m2.nRows()) {
- throw new Exception("Number of rows do not match");
- } else {
- // Create a new array (with dimensions of current array) to store the addition
- double[][] m3 = new double[m1.nCols()][m1.nRows()];
- // Cycle addition over each row
- for(int x = 0; x < m1.nRows(); x++){
- // Within each row, cycle addition over each column
- for(int y = 0; y < m1.nCols(); y++){
- // Fill the new array with the new added elements
- m3[x][y] = m1.contents[x][y] + m2.contents[x][y];
- }
- }
- // Return a new matrix using the added array contents
- return new Matrix(m3);
- }
- }
- // Compare if two matrices are equal - Non static
- public boolean equals(Matrix m2){
- // Cycle comparison over each row
- for(int x = 0; x < this.nRows(); x++) {
- // Within each row, cycle comparison over each column
- for(int y = 0; y < this.nCols(); y++) {
- // If the current element does not equal the compared element, return false
- if(this.contents[x][y] != m2.contents[x][y]) {
- return false;
- }
- }
- }
- // Loop has completed without returning false, so matrices must match. Return true
- return true;
- }
- // Compare if two matrices are equal - Static
- public static boolean equals(Matrix m1, Matrix m2){
- // Cycle comparison over each row
- for(int x = 0; x < m1.nRows(); x++){
- // Within each row, cycle comparison over each column
- for(int y = 0; y < m1.nCols(); y++){
- // If the current element does not equal the compared element, return false
- if(m1.contents[x][y] != m2.contents[x][y]){
- return false;
- }
- }
- }
- // Loop has completed without returning false, so matrices must match. Return true
- return true;
- }
- // Create a string representation of the current matrix - Non static
- public String toString(){
- // Initialise an empty string
- String matrixString = "";
- // Cycle over each row
- for(int x = 0; x < this.nRows(); x++) {
- // Within each row, cycle over each column
- for(int y = 0; y < this.nCols(); y++) {
- // Add the current element to the string, then add a space
- matrixString += this.contents[x][y];
- matrixString += " ";
- }
- // Add a new line at the end of each row
- matrixString += "\n";
- }
- return matrixString;
- }
- public static void main(String[] args) throws Exception {
- // Define the contents of two arrays
- double[][] contents1 = {
- {1.0,2.1,3.2,4.3},
- {11.5,12.6,13.7,14.8},
- {11.9,12.0,13.1,14.2},
- {11.9,12.0,13.1,14.2}
- };
- double[][] contents2 = {
- {5.0,6.1,7.2,4.3},
- {11.5,2.9,13.0,14.8},
- {11.9,18.0,1.1,14.2},
- {1.9,1.0,3.1,12.2}
- };
- // Create new Matrix objects with these contents
- Matrix m1 = new Matrix(contents1);
- Matrix m2 = new Matrix(contents2);
- // Count number of rows
- System.out.println("Number of rows: "+m1.nRows());
- // Count number of columns
- System.out.println("Number of columns: "+m1.nCols());
- // Determine if matrix is square
- System.out.println("Is the matrix square? "+m1.isSquare());
- // Create a matrix of only diagonal elements
- System.out.println("Diagonal matrix: "+m1.diagonal());
- // Add two matrices - non static
- System.out.println("Addition of two matrices (non static): "+m1.add(m2));
- // Add two matrices - static
- System.out.println("Addition of two matrices (static): " +Matrix.add(m1, m2));
- // Determine if two matrices are equal - non static
- System.out.println("Are two matrices equal (non static)? "+m1.equals(m2));
- // Determine if two matrices are equal - static
- System.out.println("Are two matrices equal (static)? "+Matrix.equals(m1, m2));
- // Produce a string representation of a matrix
- System.out.println("String representation of matrix: "+m1.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement