experiments

All kinds of coding experiments
Log | Files | Refs | Submodules

Matrix.java (2303B)


      1 import java.util.Scanner;
      2 
      3 public class Matrix {
      4     private int columns;
      5     private int rows;
      6     private double[][] matrix;
      7 
      8     public Matrix(int rows, int columns){
      9         this.rows = rows;
     10         this.columns = columns;
     11         this.matrix = new double[rows][columns];
     12         this.initToZero();
     13     }
     14 
     15     public Matrix(int size){
     16         this(size, size);
     17     }
     18 
     19     private void initToZero(){
     20         for (int r = 0; r < this.rows; r++){
     21             for (int c = 0; c < this.columns; c++){
     22                 this.matrix[r][c] = 0.0;
     23             }
     24         }
     25     }
     26 
     27     public void set(int row, int col, double value){
     28         this.matrix[row][col] = value;
     29     }
     30 
     31     public Matrix multiply(Matrix B){
     32         Matrix A = this;    // for readability
     33         if (A.columns != B.rows)
     34             return null;
     35 
     36         Matrix C = new Matrix(A.rows, B.columns);
     37         for (int r = 0; r < rows; r++){
     38             for (int c = 0; c < columns; c++){
     39                 C.matrix[r][c] = 0.0;
     40                 for (int i = 0; i < this.columns; i++){
     41                     C.matrix[r][c] += A.matrix[r][i] * B.matrix[i][c];
     42                 }
     43             }
     44 
     45         }
     46         return C;
     47     }
     48 
     49     @Override
     50     public String toString(){
     51         String toReturn = "";
     52         for (int r = 0; r < this.rows; r++) {
     53             for (int c = 0; c < this.columns; c++) {
     54                 toReturn += String.format("%1.4f \t", this.matrix[r][c]);
     55             }
     56             toReturn += "\n";
     57         }
     58         return toReturn;
     59     }
     60 
     61     public Matrix getTransposed(){
     62         Matrix transposed = new Matrix(columns, rows);
     63         for (int r = 0; r < rows; r++){
     64             for (int c = 0; c < columns; c++){
     65                 transposed.matrix[c][r] = this.matrix[r][c];
     66             }
     67         }
     68         return transposed;
     69     }
     70 
     71     public void insertMatrix(){
     72         Scanner reader = new Scanner(System.in);
     73         System.out.println("Insert the matrix elements col by col, row by row:");
     74         for (int r = 0; r < this.rows; r++){
     75             for (int c = 0; c < this.columns; c++){
     76                 this.matrix[r][c] = Double.parseDouble(reader.next());
     77             }
     78         }
     79     }
     80 
     81     public int getColumns(){
     82         return this.columns;
     83     }
     84 
     85     public int getRows(){
     86         return this.rows;
     87     }
     88 
     89 }