Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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];
- }
- Matrix(double[][] d) {
- this.rows = d.length;
- int max = 0;
- for (int i = 0; i < rows; i++) {
- int len = d[i].length;
- if (len > max) max = len;
- }
- this.cols = max;
- data = new double[rows*cols];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- if (j < d[i].length) {
- data[i*cols + j] = d[i][j];
- }
- else {
- data[i*cols + j] = 0;
- }
- }
- }
- }
- double[][] asArray() {
- double[][] returnArray = new double[rows][cols];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- returnArray[i][j] = data[i*cols + j];
- }
- }
- return returnArray;
- }
- double get(int r, int c) {
- return data[r*cols + c];
- }
- void set(int r, int c, double value) {
- data[r*cols + c] = value;
- }
- public String toString(){
- StringBuilder buf = new StringBuilder();
- buf.append("[");
- for(int i=0; i<rows; i++) {
- buf.append("[");
- for (int j = 0; j < cols; j++) {
- buf.append(Double.toString(data[i * cols + j]));
- if (j + 1 < cols) buf.append(", ");
- else buf.append("]");
- }
- if (i + 1 < rows) buf.append(", ");
- }
- buf.append("]");
- return buf.toString();
- }
- 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));
- else {
- this.rows = newRows;
- this.cols = newCols;
- }
- }
- int[] shape(){
- int [] tab = new int[2];
- tab[0] = rows;
- tab[1] = cols;
- return tab;
- }
- Matrix add(Matrix m) {
- if(rows != m.rows && cols != m.cols)
- throw new RuntimeException(String.format("Matrixes have different shapes"));
- else {
- Matrix temp = new Matrix(rows, cols);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- temp.data[i * cols + j] = this.get(i, j) + m.get(i, j);
- }
- }
- return temp;
- }
- }
- Matrix sub(Matrix m) {
- if(rows != m.rows && cols != m.cols)
- throw new RuntimeException(String.format("Matrixes have different shapes"));
- else {
- Matrix temp = new Matrix(rows, cols);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- temp.data[i * cols + j] = this.get(i, j) - m.get(i, j);
- }
- }
- return temp;
- }
- }
- Matrix mul(Matrix m) {
- if(rows != m.rows && cols != m.cols)
- throw new RuntimeException(String.format("Matrixes have different shapes"));
- else {
- Matrix temp = new Matrix(rows, cols);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- temp.data[i * cols + j] = this.get(i, j) * m.get(i, j);
- }
- }
- return temp;
- }
- }
- Matrix div(Matrix m) {
- if(rows != m.rows && cols != m.cols)
- throw new RuntimeException(String.format("Matrixes have different shapes"));
- else {
- Matrix temp = new Matrix(rows, cols);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- temp.data[i * cols + j] = this.get(i, j) / m.get(i, j);
- }
- }
- return temp;
- }
- }
- Matrix add(double w) {
- for (int i = 0; i < rows*cols; i++) this.data[i] += w;
- return this;
- }
- Matrix sub(double w) {
- for (int i = 0; i < rows*cols; i++) this.data[i] -= w;
- return this;
- }
- Matrix mul(double w) {
- for (int i = 0; i < rows*cols; i++) this.data[i] *= w;
- return this;
- }
- Matrix div(double w) {
- for (int i = 0; i < rows*cols; i++) this.data[i] /= w;
- return this;
- }
- public static void main(String[] args) {
- double[][] d = {
- {1,2,3},
- {4,5},
- {6},
- {40,50,60},
- };
- Matrix m = new Matrix(d);
- //double[][] array = m.asArray();
- // TESTY
- System.out.println(m.get(2,2));
- m.set(2,2, 15.5);
- System.out.println(m.get(2,2));
- String s = m.toString();
- System.out.println(s);
- int [] shape = m.shape();
- System.out.printf("Macierz ma wymiary %d x %d\n", shape[0], shape[1]);
- m.reshape(2,6);
- String s2 = m.toString();
- System.out.println(s2);
- shape = m.shape();
- System.out.printf("Macierz ma wymiary %d x %d\n", shape[0], shape[1]);
- double[][] t = {
- {1,2},
- {3,4},
- };
- Matrix d1 = new Matrix(t);
- Matrix d2 = new Matrix(t);
- int skalar = 5;
- System.out.println("\nDodawanie macierzy:");
- Matrix result1 = d1.add(d2);
- s = result1.toString();
- System.out.println(s);
- System.out.println("\nOdejmowanie macierzy:");
- Matrix result2 = d1.sub(d2);
- s = result2.toString();
- System.out.println(s);
- System.out.println("\nMnożenie elementów macierzy:");
- Matrix result3 = d1.mul(d2);
- s = result3.toString();
- System.out.println(s);
- System.out.println("\nDzielenie elementów macierzy:");
- Matrix result4 = d1.div(d2);
- s = result4.toString();
- System.out.println(s);
- System.out.printf("\nDodawanie skalara %d do macierzy:\n", skalar);
- Matrix m5 = new Matrix(t);
- Matrix result5 = m5.add(skalar);
- s = result5.toString();
- System.out.println(s);
- System.out.printf("\nOdejmowanie skalara %d od macierzy:\n", skalar);
- Matrix m6 = new Matrix(t);
- Matrix result6 = m6.sub(skalar);
- s = result6.toString();
- System.out.println(s);
- System.out.printf("\nMnożenie elementów macierzy przez skalar %d:\n", skalar);
- Matrix m7 = new Matrix(t);
- Matrix result7 = m7.mul(skalar);
- s = result7.toString();
- System.out.println(s);
- System.out.printf("\nDzielenie elementów macierzy przez skalar %d:\n", skalar);
- Matrix m8 = new Matrix(t);
- Matrix result8 = m8.div(skalar);
- s = result8.toString();
- System.out.println(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement