Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- * Class designed for the creation, storage, and modification of Matrices
- * @author Uliz.io
- * @version 4.2
- * Still to be added:
- * Multiplication of Two Matrices
- * Transposing
- * Proper Error Handling
- */
- public class Matrix {
- /*
- * Basic Components of a Matrix
- * Matrix itself, height, and width
- */
- private double[][] Matrix;
- int height;
- int width;
- public Matrix (int mheight, int mwidth) {
- /*
- Constructor Class
- @param height = number of rows
- @param width = number of columns
- */
- double[][] Matrix = new double[mheight][mwidth];
- height = mheight;
- width = mwidth;
- }
- public int getHeight() {
- /*
- * Returns the height of a given matrix.
- */
- return height;
- }
- public int getWidth() {
- /*
- * Returns the width of a given matrix.
- */
- return width;
- }
- public void set (int hpos, int wpos, double value) {
- /*
- Set the value of a specific cell
- within the matrix
- */
- try {
- /*
- Throw exceptions if you screwed up your inputs
- */
- if(hpos < 0 || hpos > Matrix.length) {
- throw new MatrixException("Matrix height is out of bounds.");
- }
- if(wpos < 0 || wpos > Matrix.length){
- throw new MatrixException("Matrix width is out of bounds.");
- }
- else {
- //Otherwise, set appropriate value
- Matrix[hpos][wpos] = value;
- }
- }
- catch(MatrixException e) {
- }
- }
- public double get (int hpos, int wpos) {
- /*
- Get the value of a specific cell
- within the Matrix
- */
- double d = 0;
- try {
- /*
- Throw exceptions if you screwed up your inputs
- */
- if(hpos < 0 || hpos > Matrix.length) {
- throw new MatrixException("Matrix height is out of bounds.");
- }
- if(wpos < 0 || wpos > Matrix.length){
- throw new MatrixException("Matrix width is out of bounds.");
- }
- else {
- d = Matrix[hpos][wpos];
- }
- }
- catch(MatrixException e) {
- }
- return d;
- }
- public static Matrix add (Matrix A, Matrix B) {
- /*
- Overlay two matrices and add each index
- Returns a third matrix
- */
- //Begin by constructing new matrix with same size as old ones
- Matrix C = new Matrix(A.height, A.width);
- try {
- if(A.height != B.height || A.width != B.width) {
- //Verify that Matrices are in fact same size
- throw new MatrixException("Matrix sizes are mismatched.");
- }
- else {
- //Add Matrices progressively traversing rows and columns
- for(int i = 0; i < A.width; i++) {
- for(int x = 0; x < B.height; x++) {
- C.set(x, i, A.get(x, i) + B.get(x, i));
- }
- }
- }
- }
- catch(MatrixException e) {
- }
- return C;
- }
- public static Matrix sub (Matrix A, Matrix B) {
- /*
- Overlay two matrices and subtract the indices
- Returns a third matrix
- Essentially identical to addition, but
- */
- //Begin by constructing new matrix with same size as old ones
- Matrix C = new Matrix(A.height, A.width);
- try {
- if(A.height != B.height || A.width != B.width) {
- //Verify that Matrices are in fact same size
- throw new MatrixException("Matrix sizes are mismatched.");
- }
- else {
- //Subtract Matrices progressively traversing rows and columns
- for(int i = 0; i < A.width; i++) {
- for(int x = 0; x < B.height; x++) {
- C.set(x, i, A.get(x, i) - B.get(x, i));
- }
- }
- }
- }
- catch(MatrixException e) {
- }
- return C;
- }
- public static Matrix mult (Matrix A, Matrix B) {
- /*
- * Multiply two Matrices together
- */
- Matrix C = new Matrix(A.height, A.width);
- try {
- if(A.getHeight() != B.getWidth()) {
- throw new MatrixException("Matrices are not of proper dimensions.");
- }
- if (A == null || B == null) {
- }
- else {
- //For each row
- for(int i = 0; i < A.width; i++){
- //For each corresponding column
- for(int j = 0; j < B.height; j++){
- //Carry out multiplication and append new value to new matrix
- for(int y = 0; y < A.height; y++) {
- C.set(i, j, A.get(i, y) * B.get(y, j));
- }
- }
- }
- }
- }
- catch(MatrixException e) {
- }
- return C;
- }
- public static Matrix mult (Matrix A, double multiplier) {
- /*
- Multiply every value of the matrix by a predetermined
- factor.
- */
- Matrix C = new Matrix(A.height, A.width);
- int z = A.getHeight();
- try {
- if(A == null) {
- //Verify that Matrix exists and that code didn't eat it
- throw new MatrixException("Matrix does not exist.");
- }
- else {
- //Multiply each value of the Matrix by the multiplier
- for(int i = 0; i < A.width; i++) {
- for(int x = 0; x < z; x++) {
- C.set(x, i, A.get(x, i) * multiplier);
- }
- }
- }
- }
- catch(MatrixException e) {
- }
- return C;
- }
- public static Matrix transpose (Matrix A) {
- /*
- * Method to "transpose" a Matrix (See Wikipedia for operation explanation)
- * Essentially flip the matrix across the x and y axis, and rotate 90
- */
- //Swap dimensions since we will be flipping the Matrix
- Matrix C = new Matrix(A.width, A.height);
- try {
- if(A == null) {
- throw new MatrixException("Matrix does not exist.");
- }
- else {
- for(int z = 0; z < A.getHeight(); z++){
- for(int y = 0; y < A.getWidth(); y++){
- C.set(z, y, A.get(y, z));
- }
- }
- }
- }
- catch(MatrixException e) {
- }
- return C;
- }
- public static class MatrixException extends Exception {
- public MatrixException() { super(); }
- public MatrixException (String s) { super (s); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement