Sahil-Pattni
10/20/2016 - 5:32 PM

## Matrix Lab

Matrix Lab

``````/**
* Created by spattni on 10/6/16.
*/
public class Matrix {

public Matrix() {
}

public boolean isSymmetric(int[][] matrix) {
if (matrix.length != matrix[0].length) {
return false;
}
int i, j;
boolean isTrue = false;

for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == matrix[j][i]) {
isTrue = true;
}

else {
isTrue = false;
break;
}
}
if (isTrue == false) break;
}

return isTrue;
}

public boolean isDiagonal(int[][] matrix) {
if (matrix.length != matrix[0].length) {
return false;
}
int i,j;
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
if (i != j) {
if (matrix[i][j] != 0) {
return false;
}
}
}
}

return true;
}

public boolean isIdentity(int[][] matrix) {
if (matrix.length != matrix[0].length) {
return false;
}

int i,j;
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
if (i != j  && matrix[i][j] != 0) {
return false;
}

if (i == j && matrix[i][j] != 1)
return false;
}
}

return true;
}

public boolean isUpperTriangular(int[][] matrix) {
if (matrix.length != matrix[0].length) {
return false;
}
int i,j;

for (i = 0; i < matrix.length; i++) {
for (j = 0; j < i; j++) {
if (matrix[i][j] != 0) {
return false;
}
}
}
return true;
}

public boolean isTridiagonal(int[][] matrix) {
if (matrix.length != matrix[0].length) {
return false;
}

int i,j;

for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
if (i != j && i+1 != j && i-1 != j) {
if (matrix[i][j] != 0) {
return false;
}
}
}
}

return true;
}

public static void main(String[] args) {
int[][] matrix = { {1, 4, 0, 0},
{3, 4, 1, 0},
{0, 2, 3, 4},
{0, 0, 1, 3}};
Matrix m = new Matrix();

//----------prints out matrix----------//
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
//-----------------------------------//

System.out.println(m.isTridiagonal(matrix));
}
}
``````