Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Lab1;
- public class Matrix {
- double[]data;
- int rows;
- int cols;
- Matrix(int rows, int cols){
- this.rows = rows;
- this.cols = cols;
- data = new double[rows*cols];
- this.fill(0);
- }
- // 2.1
- Matrix(double[][] d) {
- int longest = 0;
- for(int i = 0; i < d.length; i++){
- if (d[i].length > longest){
- longest = d[i].length;
- }
- }
- this.rows = d.length;
- this.cols = longest;
- data = new double[rows*cols];
- for(int i =0; i < d.length ; i++){
- int j = 0;
- while(j < d[i].length){
- data[i * cols + j] = d[i][j];
- j++;
- }
- while(j < cols){
- data[i * cols + j] = 0;
- j++;
- }
- }
- }
- // fill method:
- private void fill(double value){
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- this.set(i,j,value);
- }
- }
- }
- // 2.2
- public double[][] asArray(){
- double[][] result = new double[rows][cols];
- for(int i=0; i< rows;i++) {
- for (int j = 0; j < cols; j++) {
- result[i][j] = data[i*cols + j];
- }
- }
- return result;
- }
- // 2.3
- public double get(int r,int c){
- // + try/except clausure
- return data[r * cols + c];
- }
- public void set (int r,int c, double value){
- data[r * cols + c] = value;
- }
- // 2.4
- public String toString(){
- StringBuilder buf = new StringBuilder();
- buf.append("[\n");
- for(int i=0;i<rows;i++){
- buf.append(" [ ");
- for(int j=0;j<cols;j++){
- buf.append(data[i * cols + j]);
- buf.append(" ");
- }
- buf.append("]\n");
- }
- buf.append("]");
- return buf.toString();
- }
- // 2.5
- public void reshape(int newRows,int newCols){
- if(rows*cols != newRows*newCols)
- throw new RuntimeException(String.format("%d x %d matrix can't be reshaped to %d x %d",rows,cols,newRows,newCols));
- this.rows = newRows;
- this.cols = newCols;
- }
- // 2.6
- public int[] shape(){
- int[] result = new int[]{this.rows,this.cols};
- return result;
- }
- // 2.7 & 2.8
- // add:
- public Matrix add(Matrix other){
- if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
- throw new RuntimeException("ERROR");
- }
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) + other.get(i,j));
- }
- }
- return result;
- }
- public Matrix add(double w) {
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) + w);
- }
- }
- return result;
- }
- // substitution:
- public Matrix sub(Matrix other){
- if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
- throw new RuntimeException("ERROR");
- }
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) - other.get(i,j));
- }
- }
- return result;
- }
- public Matrix sub(double w) {
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) - w);
- }
- }
- return result;
- }
- // multiplication:
- public Matrix mul(Matrix other){
- if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
- throw new RuntimeException("ERROR");
- }
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) * other.get(i,j));
- }
- }
- return result;
- }
- public Matrix mul(double w){
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) * w);
- }
- }
- return result;
- }
- // division:
- public Matrix div(Matrix other){
- if (this.rows != other.shape()[0] || this.cols != other.shape()[1]){
- throw new RuntimeException("ERROR");
- }
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) / other.get(i,j));
- }
- }
- return result;
- }
- public Matrix div(double w){
- Matrix result = new Matrix(this.rows,this.cols);
- for(int i=0;i<this.rows;i++){
- for(int j=0;j<this.cols;j++){
- result.set(i,j,this.get(i,j) / w);
- }
- }
- return result;
- }
- // matrix multiplication
- // public Matrix dot(Matrix other){
- // if (this.cols != other.shape()[0]){
- // throw new RuntimeException("ERROR");
- // }
- //
- // Matrix result = new Matrix(this.rows, other.shape()[1]);
- // result.fill(0);
- //
- // for(int i=0;i<this.cols;i++){
- // for(int j=0;j<this.cols()[0]){
- //
- // }
- // }
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement