Untitled
unknown
plain_text
a year ago
2.1 kB
17
Indexable
package lab1;
public class Matrix {
private double[][] data;
/**
* Default constructor defines a 2 X 2 matrix with zero values
*/
public Matrix() {
data = new double[2][2];
}
public Matrix(double[][] arr) {
this();
if (isMatrix(arr) == true) {
int rows = arr.length;
int columns = arr[0].length;
data = new double[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
data[i][j] = arr[i][j];
}
}
public Matrix(int r, int c) {
if (r > 0 && c > 0)
data = new double[r][c];
else
throw new RuntimeException("Invalid number of rows and columns");
}
public Matrix add(Matrix om, Matrix sm) {
Matrix nm = new Matrix(this.getRows(), this.getColumns());
if (!(nm.sameDimensions(om))) {
for (int i = 0; i < getRows(); i++)
for (int j = 0; j < getColumns(); j++)
nm[i][j] += om[i][j];
} else {
return null;
}
return null;
}
public Matrix subt(Matrix om) {
return null;
}
public boolean equals(Matrix om) {
return false;
}
public Matrix mult(Matrix om) {
return null;
}
public Matrix scalarMult(int s) {
Matrix nm = new Matrix(this.getRows(), this.getColumns());
for (int i = 0; i < getRows(); i++)
for (int j = 0; j < getColumns(); j++)
nm.data[i][j] = s * this.data[i][j];
return nm;
}
public Matrix transpose() {
return null;
}
public String toString() {
String str = new String();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < getColumns(); j++)
str += data[i][j] + " ";
str += "\n";
}
return str;
}
private boolean sameDimensions(Matrix om) {
if (this.getRows() == om.getRows() && this.getColumns() == om.getColumns())
return true;
return false;
}
public boolean isMatrix(double[][] arr) {
if (arr == null)
return false;
for (int i = 1; i < arr.length; i++)
if (arr[0].length != arr[i].length)
return false;
return true;
}
public int getRows() {
return this.data.length;
}
public int getColumns() {
return this.data[0].length;
}
}
Editor is loading...
Leave a Comment