Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. package com.Lab1;
  2.  
  3. public class Matrix {
  4.     double[]data;
  5.     int rows;
  6.     int cols;
  7.  
  8.     Matrix(int rows, int cols){
  9.         this.rows = rows;
  10.         this.cols = cols;
  11.         data = new double[rows*cols];
  12.         this.fill(0);
  13.     }
  14.  
  15.     // 2.1
  16.     Matrix(double[][] d) {
  17.         int longest = 0;
  18.         for(int i = 0; i < d.length; i++){
  19.             if (d[i].length > longest){
  20.                 longest = d[i].length;
  21.             }
  22.         }
  23.         this.rows = d.length;
  24.         this.cols = longest;
  25.         data = new double[rows*cols];
  26.  
  27.         for(int i =0; i < d.length ; i++){
  28.             int j = 0;
  29.             while(j < d[i].length){
  30.                 data[i * cols + j] = d[i][j];
  31.                 j++;
  32.             }
  33.             while(j < cols){
  34.                 data[i * cols + j] = 0;
  35.                 j++;
  36.             }
  37.         }
  38.     }
  39.  
  40.     // fill method:
  41.     private void fill(double value){
  42.         for(int i=0;i<this.rows;i++){
  43.             for(int j=0;j<this.cols;j++){
  44.                 this.set(i,j,value);
  45.             }
  46.         }
  47.     }
  48.  
  49.     // 2.2
  50.     public double[][] asArray(){
  51.         double[][] result = new double[rows][cols];
  52.         for(int i=0; i< rows;i++) {
  53.             for (int j = 0; j < cols; j++) {
  54.                 result[i][j] = data[i*cols + j];
  55.             }
  56.         }
  57.         return result;
  58.     }
  59.  
  60.     // 2.3
  61.     public double get(int r,int c){
  62.         // + try/except clausure
  63.         return data[r * cols + c];
  64.     }
  65.  
  66.     public void set (int r,int c, double value){
  67.         data[r * cols + c] = value;
  68.     }
  69.  
  70.     // 2.4
  71.     public String toString(){
  72.         StringBuilder buf = new StringBuilder();
  73.         buf.append("[\n");
  74.         for(int i=0;i<rows;i++){
  75.             buf.append("  [ ");
  76.             for(int j=0;j<cols;j++){
  77.                 buf.append(data[i * cols + j]);
  78.                 buf.append(" ");
  79.             }
  80.             buf.append("]\n");
  81.         }
  82.         buf.append("]");
  83.         return buf.toString();
  84.     }
  85.  
  86.     // 2.5
  87.     public void reshape(int newRows,int newCols){
  88.         if(rows*cols != newRows*newCols)
  89.             throw new RuntimeException(String.format("%d x %d matrix can't be reshaped to %d x %d",rows,cols,newRows,newCols));
  90.         this.rows = newRows;
  91.         this.cols = newCols;
  92.     }
  93.  
  94.     // 2.6
  95.     public int[] shape(){
  96.         int[] result = new int[]{this.rows,this.cols};
  97.         return result;
  98.     }
  99.  
  100.     // 2.7 & 2.8
  101.     // add:
  102.     public Matrix add(Matrix other){
  103.         if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
  104.             throw new RuntimeException("ERROR");
  105.         }
  106.  
  107.         Matrix result = new Matrix(this.rows,this.cols);
  108.         for(int i=0;i<this.rows;i++){
  109.             for(int j=0;j<this.cols;j++){
  110.                 result.set(i,j,this.get(i,j) + other.get(i,j));
  111.             }
  112.         }
  113.         return result;
  114.     }
  115.  
  116.     public Matrix add(double w) {
  117.         Matrix result = new Matrix(this.rows,this.cols);
  118.         for(int i=0;i<this.rows;i++){
  119.             for(int j=0;j<this.cols;j++){
  120.                 result.set(i,j,this.get(i,j) + w);
  121.             }
  122.         }
  123.         return result;
  124.     }
  125.  
  126.     // substitution:
  127.     public Matrix sub(Matrix other){
  128.         if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
  129.             throw new RuntimeException("ERROR");
  130.         }
  131.  
  132.         Matrix result = new Matrix(this.rows,this.cols);
  133.         for(int i=0;i<this.rows;i++){
  134.             for(int j=0;j<this.cols;j++){
  135.                 result.set(i,j,this.get(i,j) - other.get(i,j));
  136.             }
  137.         }
  138.         return result;
  139.     }
  140.  
  141.     public Matrix sub(double w) {
  142.         Matrix result = new Matrix(this.rows,this.cols);
  143.         for(int i=0;i<this.rows;i++){
  144.             for(int j=0;j<this.cols;j++){
  145.                 result.set(i,j,this.get(i,j) - w);
  146.             }
  147.         }
  148.         return result;
  149.     }
  150.  
  151.     // multiplication:
  152.     public Matrix mul(Matrix other){
  153.         if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
  154.             throw new RuntimeException("ERROR");
  155.         }
  156.  
  157.         Matrix result = new Matrix(this.rows,this.cols);
  158.         for(int i=0;i<this.rows;i++){
  159.             for(int j=0;j<this.cols;j++){
  160.                 result.set(i,j,this.get(i,j) * other.get(i,j));
  161.             }
  162.         }
  163.         return result;
  164.     }
  165.  
  166.     public Matrix mul(double w){
  167.         Matrix result = new Matrix(this.rows,this.cols);
  168.         for(int i=0;i<this.rows;i++){
  169.             for(int j=0;j<this.cols;j++){
  170.                 result.set(i,j,this.get(i,j) * w);
  171.             }
  172.         }
  173.         return result;
  174.     }
  175.  
  176.     // division:
  177.     public Matrix div(Matrix other){
  178.         if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
  179.             throw new RuntimeException("ERROR");
  180.         }
  181.  
  182.         Matrix result = new Matrix(this.rows,this.cols);
  183.         for(int i=0;i<this.rows;i++){
  184.             for(int j=0;j<this.cols;j++){
  185.                 result.set(i,j,this.get(i,j) / other.get(i,j));
  186.             }
  187.         }
  188.         return result;
  189.     }
  190.  
  191.     public Matrix div(double w){
  192.         Matrix result = new Matrix(this.rows,this.cols);
  193.         for(int i=0;i<this.rows;i++){
  194.             for(int j=0;j<this.cols;j++){
  195.                 result.set(i,j,this.get(i,j) / w);
  196.             }
  197.         }
  198.         return result;
  199.     }
  200.  
  201.     // matrix multiplication
  202. //    public Matrix dot(Matrix other){
  203. //        if (this.cols != other.shape()[0]){
  204. //            throw new RuntimeException("ERROR");
  205. //        }
  206. //
  207. //        Matrix result = new Matrix(this.rows, other.shape()[1]);
  208. //        result.fill(0);
  209. //
  210. //        for(int i=0;i<this.cols;i++){
  211. //            for(int j=0;j<this.cols()[0]){
  212. //
  213. //            }
  214. //        }
  215. //
  216. //    }
  217.  
  218.  
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement