Advertisement
NicholasCSW

Matrix

Feb 1st, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.13 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  * Class designed for the creation, storage, and modification of Matrices
  5.  * @author Uliz.io
  6.  * @version 4.2
  7.  * Still to be added:
  8.  * Multiplication of Two Matrices
  9.  * Transposing
  10.  * Proper Error Handling
  11.  */
  12.  
  13. public class Matrix {
  14.  
  15.     /*
  16.      * Basic Components of a Matrix
  17.      * Matrix itself, height, and width
  18.      */
  19.     private double[][] Matrix;
  20.     int height;
  21.     int width;
  22.  
  23.     public Matrix (int mheight, int mwidth) {
  24.         /*
  25.          Constructor Class
  26.          @param height = number of rows
  27.          @param width = number of columns
  28.          */
  29.         double[][] Matrix = new double[mheight][mwidth];
  30.         height = mheight;
  31.         width = mwidth;
  32.     }
  33.     public int getHeight() {
  34.         /*
  35.          * Returns the height of a given matrix.
  36.          */
  37.         return height;
  38.     }
  39.     public int getWidth() {
  40.         /*
  41.          * Returns the width of a given matrix.
  42.          */
  43.         return width;
  44.     }
  45.     public void set (int hpos, int wpos, double value) {
  46.         /*
  47.         Set the value of a specific cell
  48.         within the matrix
  49.          */
  50.         try {
  51.             /*
  52.             Throw exceptions if you screwed up your inputs
  53.              */
  54.             if(hpos < 0 || hpos > Matrix.length) {
  55.                 throw new MatrixException("Matrix height is out of bounds.");
  56.             }
  57.             if(wpos < 0 || wpos > Matrix.length){
  58.                 throw new MatrixException("Matrix width is out of bounds.");
  59.             }
  60.             else {
  61.                 //Otherwise, set appropriate value
  62.                 Matrix[hpos][wpos] = value;
  63.             }
  64.         }
  65.         catch(MatrixException e) {
  66.  
  67.         }
  68.     }
  69.     public double get (int hpos, int wpos) {
  70.         /*
  71.         Get the value of a specific cell
  72.         within the Matrix
  73.          */
  74.  
  75.         double d = 0;
  76.  
  77.         try {
  78.  
  79.             /*
  80.             Throw exceptions if you screwed up your inputs
  81.              */
  82.             if(hpos < 0 || hpos > Matrix.length) {
  83.                 throw new MatrixException("Matrix height is out of bounds.");
  84.             }
  85.             if(wpos < 0 || wpos > Matrix.length){
  86.                 throw new MatrixException("Matrix width is out of bounds.");
  87.             }
  88.             else {
  89.                 d = Matrix[hpos][wpos];
  90.             }
  91.  
  92.         }
  93.         catch(MatrixException e) {
  94.  
  95.         }
  96.  
  97.         return d;
  98.  
  99.     }
  100.     public static Matrix add (Matrix A, Matrix B) {
  101.         /*
  102.         Overlay two matrices and add each index
  103.         Returns a third matrix
  104.          */
  105.  
  106.         //Begin by constructing new matrix with same size as old ones
  107.         Matrix C = new Matrix(A.height, A.width);
  108.  
  109.         try {
  110.             if(A.height != B.height || A.width != B.width) {
  111.                 //Verify that Matrices are in fact same size
  112.                 throw new MatrixException("Matrix sizes are mismatched.");
  113.             }
  114.             else {
  115.                 //Add Matrices progressively traversing rows and columns
  116.                 for(int i = 0; i < A.width; i++) {
  117.                     for(int x = 0; x < B.height; x++) {
  118.                         C.set(x, i, A.get(x, i) + B.get(x, i));
  119.                     }
  120.                 }
  121.             }
  122.  
  123.  
  124.         }
  125.         catch(MatrixException e) {
  126.  
  127.         }
  128.  
  129.  
  130.         return C;
  131.     }
  132.     public static Matrix sub (Matrix A, Matrix B) {
  133.         /*
  134.         Overlay two matrices and subtract the indices
  135.         Returns a third matrix
  136.         Essentially identical to addition, but
  137.          */
  138.         //Begin by constructing new matrix with same size as old ones
  139.         Matrix C = new Matrix(A.height, A.width);
  140.  
  141.         try {
  142.             if(A.height != B.height || A.width != B.width) {
  143.                 //Verify that Matrices are in fact same size
  144.                 throw new MatrixException("Matrix sizes are mismatched.");
  145.             }
  146.             else {
  147.                 //Subtract Matrices progressively traversing rows and columns
  148.                 for(int i = 0; i < A.width; i++) {
  149.                     for(int x = 0; x < B.height; x++) {
  150.                         C.set(x, i, A.get(x, i) - B.get(x, i));
  151.                     }
  152.                 }
  153.             }
  154.  
  155.  
  156.         }
  157.         catch(MatrixException e) {
  158.  
  159.         }
  160.  
  161.  
  162.         return C;
  163.     }
  164.     public static Matrix mult (Matrix A, Matrix B) {
  165.         /*
  166.          * Multiply two Matrices together
  167.          */
  168.  
  169.         Matrix C = new Matrix(A.height, A.width);
  170.        
  171.         try {
  172.             if(A.getHeight() != B.getWidth()) {
  173.                 throw new MatrixException("Matrices are not of proper dimensions.");
  174.             }
  175.             if (A == null || B == null) {
  176.  
  177.             }
  178.             else {
  179.                 //For each row
  180.                 for(int i = 0; i < A.width; i++){
  181.                     //For each corresponding column
  182.                     for(int j = 0; j < B.height; j++){
  183.                         //Carry out multiplication and append new value to new matrix
  184.                         for(int y = 0; y < A.height; y++) {
  185.                             C.set(i, j, A.get(i, y) * B.get(y, j));
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.         catch(MatrixException e) {
  192.  
  193.         }
  194.         return C;
  195.     }
  196.     public static Matrix mult (Matrix A, double multiplier) {
  197.         /*
  198.         Multiply every value of the matrix by a predetermined
  199.         factor.
  200.          */
  201.         Matrix C = new Matrix(A.height, A.width);
  202.  
  203.         int z = A.getHeight();
  204.  
  205.         try {
  206.             if(A == null) {
  207.                 //Verify that Matrix exists and that code didn't eat it
  208.                 throw new MatrixException("Matrix does not exist.");
  209.             }
  210.             else {
  211.                 //Multiply each value of the Matrix by the multiplier
  212.                 for(int i = 0; i < A.width; i++) {
  213.                     for(int x = 0; x < z; x++) {
  214.                         C.set(x, i, A.get(x, i) * multiplier);
  215.                     }
  216.                 }
  217.             }
  218.  
  219.  
  220.         }
  221.         catch(MatrixException e) {
  222.  
  223.         }
  224.  
  225.  
  226.         return C;
  227.     }
  228.     public static Matrix transpose (Matrix A) {
  229.         /*
  230.          * Method to "transpose" a Matrix (See Wikipedia for operation explanation)
  231.          * Essentially flip the matrix across the x and y axis, and rotate 90
  232.          */
  233.        
  234.         //Swap dimensions since we will be flipping the Matrix
  235.         Matrix C = new Matrix(A.width, A.height);
  236.        
  237.         try {
  238.             if(A == null) {
  239.                 throw new MatrixException("Matrix does not exist.");
  240.             }
  241.             else {
  242.                 for(int z = 0; z < A.getHeight(); z++){
  243.                     for(int y = 0; y < A.getWidth(); y++){
  244.                         C.set(z, y, A.get(y, z));
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.         catch(MatrixException e) {
  250.  
  251.         }
  252.         return C;
  253.     }
  254.     public static class MatrixException extends Exception {
  255.  
  256.         public MatrixException() { super(); }
  257.  
  258.         public MatrixException (String s) { super (s); }
  259.  
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement